blob: 48f300bd8d069493d9aed8665779290be9bd4f40 [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"
29
30// This program takes a list of files and directories (indicated by a
31// trailing slash) on the stdin, and prints to stdout each input
32// filename along with its desired uid, gid, and mode (in octal).
33// The leading slash should be stripped from the input.
34//
Nick Kralevich0eb17d92013-09-07 17:10:29 -070035// After the first 4 columns, optional key=value pairs are emitted
36// for each file. Currently, the following keys are supported:
37// * -S: selabel=[selinux_label]
38// * -C: capabilities=[hex capabilities value]
39//
The Android Open Source Project88b60792009-03-03 19:28:42 -080040// Example input:
41//
Nick Kralevich0eb17d92013-09-07 17:10:29 -070042// system/etc/dbus.conf
43// data/app/
The Android Open Source Project88b60792009-03-03 19:28:42 -080044//
45// Output:
46//
Nick Kralevich0eb17d92013-09-07 17:10:29 -070047// system/etc/dbus.conf 1002 1002 440
48// data/app 1000 1000 771
49//
50// or if, for example, -S is used:
51//
52// system/etc/dbus.conf 1002 1002 440 selabel=u:object_r:system_file:s0
53// data/app 1000 1000 771 selabel=u:object_r:apk_data_file:s0
The Android Open Source Project88b60792009-03-03 19:28:42 -080054//
55// Note that the output will omit the trailing slash from
56// directories.
57
Nick Kralevich0eb17d92013-09-07 17:10:29 -070058static struct selabel_handle* get_sehnd(const char* context_file) {
59 struct selinux_opt seopts[] = { { SELABEL_OPT_PATH, context_file } };
60 struct selabel_handle* sehnd = selabel_open(SELABEL_CTX_FILE, seopts, 1);
61
62 if (!sehnd) {
63 perror("error running selabel_open");
64 exit(EXIT_FAILURE);
65 }
66 return sehnd;
67}
68
69static void usage() {
Thierry Strudel74a81e62015-07-09 09:54:55 -070070 fprintf(stderr, "Usage: fs_config [-D product_out_path] [-S context_file] [-C]\n");
Nick Kralevich0eb17d92013-09-07 17:10:29 -070071}
72
The Android Open Source Project88b60792009-03-03 19:28:42 -080073int main(int argc, char** argv) {
74 char buffer[1024];
Nick Kralevich0eb17d92013-09-07 17:10:29 -070075 const char* context_file = NULL;
Thierry Strudel74a81e62015-07-09 09:54:55 -070076 const char* product_out_path = NULL;
Nick Kralevich0eb17d92013-09-07 17:10:29 -070077 struct selabel_handle* sehnd = NULL;
78 int print_capabilities = 0;
79 int opt;
Thierry Strudel74a81e62015-07-09 09:54:55 -070080 while((opt = getopt(argc, argv, "CS:D:")) != -1) {
Nick Kralevich0eb17d92013-09-07 17:10:29 -070081 switch(opt) {
82 case 'C':
83 print_capabilities = 1;
84 break;
85 case 'S':
86 context_file = optarg;
87 break;
Thierry Strudel74a81e62015-07-09 09:54:55 -070088 case 'D':
89 product_out_path = optarg;
90 break;
Nick Kralevich0eb17d92013-09-07 17:10:29 -070091 default:
92 usage();
93 exit(EXIT_FAILURE);
94 }
95 }
96
97 if (context_file != NULL) {
98 sehnd = get_sehnd(context_file);
99 }
The Android Open Source Project88b60792009-03-03 19:28:42 -0800100
101 while (fgets(buffer, 1023, stdin) != NULL) {
102 int is_dir = 0;
103 int i;
104 for (i = 0; i < 1024 && buffer[i]; ++i) {
105 switch (buffer[i]) {
106 case '\n':
107 buffer[i-is_dir] = '\0';
Gaelle Nassietaa8f2f62016-06-24 11:18:53 +0200108 if (i == 0) {
109 is_dir = 1; // empty line is considered as root directory
110 }
The Android Open Source Project88b60792009-03-03 19:28:42 -0800111 i = 1025;
112 break;
113 case '/':
114 is_dir = 1;
115 break;
116 default:
117 is_dir = 0;
118 break;
119 }
120 }
121
122 unsigned uid = 0, gid = 0, mode = 0;
Nick Kralevichfa798092013-02-20 12:43:51 -0800123 uint64_t capabilities;
Thierry Strudel74a81e62015-07-09 09:54:55 -0700124 fs_config(buffer, is_dir, product_out_path, &uid, &gid, &mode, &capabilities);
Nick Kralevich0eb17d92013-09-07 17:10:29 -0700125 printf("%s %d %d %o", buffer, uid, gid, mode);
126
127 if (sehnd != NULL) {
128 size_t buffer_strlen = strnlen(buffer, sizeof(buffer));
129 if (buffer_strlen >= sizeof(buffer)) {
130 fprintf(stderr, "non null terminated buffer, aborting\n");
131 exit(EXIT_FAILURE);
132 }
133 size_t full_name_size = buffer_strlen + 2;
134 char* full_name = (char*) malloc(full_name_size);
135 if (full_name == NULL) {
136 perror("malloc");
137 exit(EXIT_FAILURE);
138 }
139
140 full_name[0] = '/';
141 strncpy(full_name + 1, buffer, full_name_size - 1);
142 full_name[full_name_size - 1] = '\0';
143
144 char* secontext;
145 if (selabel_lookup(sehnd, &secontext, full_name, ( mode | (is_dir ? S_IFDIR : S_IFREG)))) {
146 secontext = strdup("u:object_r:unlabeled:s0");
147 }
148
149 printf(" selabel=%s", secontext);
150 free(full_name);
151 freecon(secontext);
152 }
153
154 if (print_capabilities) {
155 printf(" capabilities=0x%" PRIx64, capabilities);
156 }
157
158 printf("\n");
The Android Open Source Project88b60792009-03-03 19:28:42 -0800159 }
160 return 0;
161}