blob: 2a75add09683ef070819c6717313d79d018d391d [file] [log] [blame]
The Android Open Source Project88b60792009-03-03 19:28:42 -08001/*
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 Kralevich0eb17d92013-09-07 17:10:29 -070018#include <stdlib.h>
The Android Open Source Project88b60792009-03-03 19:28:42 -080019#include <sys/stat.h>
20#include <errno.h>
21#include <unistd.h>
22#include <string.h>
Nick Kralevich0eb17d92013-09-07 17:10:29 -070023#include <inttypes.h>
24
25#include <selinux/selinux.h>
26#include <selinux/label.h>
The Android Open Source Project88b60792009-03-03 19:28:42 -080027
28#include "private/android_filesystem_config.h"
Tom Cherry7cff1fa2020-03-04 15:24:23 -080029#include "private/fs_config.h"
The Android Open Source Project88b60792009-03-03 19:28:42 -080030
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 Kralevich0eb17d92013-09-07 17:10:29 -070036// 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 Project88b60792009-03-03 19:28:42 -080041// Example input:
42//
Nick Kralevich0eb17d92013-09-07 17:10:29 -070043// system/etc/dbus.conf
44// data/app/
The Android Open Source Project88b60792009-03-03 19:28:42 -080045//
46// Output:
47//
Nick Kralevich0eb17d92013-09-07 17:10:29 -070048// 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 Project88b60792009-03-03 19:28:42 -080055//
56// Note that the output will omit the trailing slash from
57// directories.
58
Nick Kralevich0eb17d92013-09-07 17:10:29 -070059static 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
70static void usage() {
Luis Hector Chavez440da2d2018-02-07 09:49:52 -080071 fprintf(stderr, "Usage: fs_config [-D product_out_path] [-S context_file] [-R root] [-C]\n");
Nick Kralevich0eb17d92013-09-07 17:10:29 -070072}
73
The Android Open Source Project88b60792009-03-03 19:28:42 -080074int main(int argc, char** argv) {
75 char buffer[1024];
Nick Kralevich0eb17d92013-09-07 17:10:29 -070076 const char* context_file = NULL;
Thierry Strudel74a81e62015-07-09 09:54:55 -070077 const char* product_out_path = NULL;
Luis Hector Chavez440da2d2018-02-07 09:49:52 -080078 char* root_path = NULL;
Nick Kralevich0eb17d92013-09-07 17:10:29 -070079 struct selabel_handle* sehnd = NULL;
80 int print_capabilities = 0;
81 int opt;
Luis Hector Chavez440da2d2018-02-07 09:49:52 -080082 while((opt = getopt(argc, argv, "CS:R:D:")) != -1) {
Nick Kralevich0eb17d92013-09-07 17:10:29 -070083 switch(opt) {
84 case 'C':
85 print_capabilities = 1;
86 break;
87 case 'S':
88 context_file = optarg;
89 break;
Luis Hector Chavez440da2d2018-02-07 09:49:52 -080090 case 'R':
91 root_path = optarg;
92 break;
Thierry Strudel74a81e62015-07-09 09:54:55 -070093 case 'D':
94 product_out_path = optarg;
95 break;
Nick Kralevich0eb17d92013-09-07 17:10:29 -070096 default:
97 usage();
98 exit(EXIT_FAILURE);
99 }
100 }
101
102 if (context_file != NULL) {
103 sehnd = get_sehnd(context_file);
104 }
The Android Open Source Project88b60792009-03-03 19:28:42 -0800105
Luis Hector Chavez440da2d2018-02-07 09:49:52 -0800106 if (root_path != NULL) {
107 size_t root_len = strlen(root_path);
108 /* Trim any trailing slashes from the root path. */
109 while (root_len && root_path[--root_len] == '/') {
110 root_path[root_len] = '\0';
111 }
112 }
113
The Android Open Source Project88b60792009-03-03 19:28:42 -0800114 while (fgets(buffer, 1023, stdin) != NULL) {
115 int is_dir = 0;
116 int i;
117 for (i = 0; i < 1024 && buffer[i]; ++i) {
118 switch (buffer[i]) {
119 case '\n':
120 buffer[i-is_dir] = '\0';
Gaelle Nassietaa8f2f62016-06-24 11:18:53 +0200121 if (i == 0) {
122 is_dir = 1; // empty line is considered as root directory
123 }
The Android Open Source Project88b60792009-03-03 19:28:42 -0800124 i = 1025;
125 break;
126 case '/':
127 is_dir = 1;
128 break;
129 default:
130 is_dir = 0;
131 break;
132 }
133 }
134
135 unsigned uid = 0, gid = 0, mode = 0;
Nick Kralevichfa798092013-02-20 12:43:51 -0800136 uint64_t capabilities;
Thierry Strudel74a81e62015-07-09 09:54:55 -0700137 fs_config(buffer, is_dir, product_out_path, &uid, &gid, &mode, &capabilities);
Luis Hector Chavez440da2d2018-02-07 09:49:52 -0800138 if (root_path != NULL && strcmp(buffer, root_path) == 0) {
139 /* The root of the filesystem needs to be an empty string. */
140 strcpy(buffer, "");
141 }
Nick Kralevich0eb17d92013-09-07 17:10:29 -0700142 printf("%s %d %d %o", buffer, uid, gid, mode);
143
144 if (sehnd != NULL) {
145 size_t buffer_strlen = strnlen(buffer, sizeof(buffer));
146 if (buffer_strlen >= sizeof(buffer)) {
147 fprintf(stderr, "non null terminated buffer, aborting\n");
148 exit(EXIT_FAILURE);
149 }
150 size_t full_name_size = buffer_strlen + 2;
151 char* full_name = (char*) malloc(full_name_size);
152 if (full_name == NULL) {
153 perror("malloc");
154 exit(EXIT_FAILURE);
155 }
156
157 full_name[0] = '/';
158 strncpy(full_name + 1, buffer, full_name_size - 1);
159 full_name[full_name_size - 1] = '\0';
160
161 char* secontext;
162 if (selabel_lookup(sehnd, &secontext, full_name, ( mode | (is_dir ? S_IFDIR : S_IFREG)))) {
163 secontext = strdup("u:object_r:unlabeled:s0");
164 }
165
166 printf(" selabel=%s", secontext);
167 free(full_name);
168 freecon(secontext);
169 }
170
171 if (print_capabilities) {
172 printf(" capabilities=0x%" PRIx64, capabilities);
173 }
174
175 printf("\n");
The Android Open Source Project88b60792009-03-03 19:28:42 -0800176 }
177 return 0;
178}