2009-03-04 04:28:42 +01:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2008 The Android Open Source Project
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
2013-09-08 02:10:29 +02:00
|
|
|
#include <stdlib.h>
|
2009-03-04 04:28:42 +01:00
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <string.h>
|
2013-09-08 02:10:29 +02:00
|
|
|
#include <inttypes.h>
|
|
|
|
|
|
|
|
#include <selinux/selinux.h>
|
|
|
|
#include <selinux/label.h>
|
2009-03-04 04:28:42 +01:00
|
|
|
|
|
|
|
#include "private/android_filesystem_config.h"
|
2020-03-05 00:24:23 +01:00
|
|
|
#include "private/fs_config.h"
|
2009-03-04 04:28:42 +01:00
|
|
|
|
|
|
|
// This program takes a list of files and directories (indicated by a
|
|
|
|
// trailing slash) on the stdin, and prints to stdout each input
|
|
|
|
// filename along with its desired uid, gid, and mode (in octal).
|
|
|
|
// The leading slash should be stripped from the input.
|
|
|
|
//
|
2013-09-08 02:10:29 +02:00
|
|
|
// After the first 4 columns, optional key=value pairs are emitted
|
|
|
|
// for each file. Currently, the following keys are supported:
|
|
|
|
// * -S: selabel=[selinux_label]
|
|
|
|
// * -C: capabilities=[hex capabilities value]
|
|
|
|
//
|
2009-03-04 04:28:42 +01:00
|
|
|
// Example input:
|
|
|
|
//
|
2013-09-08 02:10:29 +02:00
|
|
|
// system/etc/dbus.conf
|
|
|
|
// data/app/
|
2009-03-04 04:28:42 +01:00
|
|
|
//
|
|
|
|
// Output:
|
|
|
|
//
|
2013-09-08 02:10:29 +02:00
|
|
|
// system/etc/dbus.conf 1002 1002 440
|
|
|
|
// data/app 1000 1000 771
|
|
|
|
//
|
|
|
|
// or if, for example, -S is used:
|
|
|
|
//
|
|
|
|
// system/etc/dbus.conf 1002 1002 440 selabel=u:object_r:system_file:s0
|
|
|
|
// data/app 1000 1000 771 selabel=u:object_r:apk_data_file:s0
|
2009-03-04 04:28:42 +01:00
|
|
|
//
|
|
|
|
// Note that the output will omit the trailing slash from
|
|
|
|
// directories.
|
|
|
|
|
2013-09-08 02:10:29 +02:00
|
|
|
static struct selabel_handle* get_sehnd(const char* context_file) {
|
|
|
|
struct selinux_opt seopts[] = { { SELABEL_OPT_PATH, context_file } };
|
|
|
|
struct selabel_handle* sehnd = selabel_open(SELABEL_CTX_FILE, seopts, 1);
|
|
|
|
|
|
|
|
if (!sehnd) {
|
|
|
|
perror("error running selabel_open");
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
return sehnd;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void usage() {
|
2018-02-07 18:49:52 +01:00
|
|
|
fprintf(stderr, "Usage: fs_config [-D product_out_path] [-S context_file] [-R root] [-C]\n");
|
2013-09-08 02:10:29 +02:00
|
|
|
}
|
|
|
|
|
2009-03-04 04:28:42 +01:00
|
|
|
int main(int argc, char** argv) {
|
|
|
|
char buffer[1024];
|
2013-09-08 02:10:29 +02:00
|
|
|
const char* context_file = NULL;
|
2015-07-09 18:54:55 +02:00
|
|
|
const char* product_out_path = NULL;
|
2018-02-07 18:49:52 +01:00
|
|
|
char* root_path = NULL;
|
2013-09-08 02:10:29 +02:00
|
|
|
struct selabel_handle* sehnd = NULL;
|
|
|
|
int print_capabilities = 0;
|
|
|
|
int opt;
|
2018-02-07 18:49:52 +01:00
|
|
|
while((opt = getopt(argc, argv, "CS:R:D:")) != -1) {
|
2013-09-08 02:10:29 +02:00
|
|
|
switch(opt) {
|
|
|
|
case 'C':
|
|
|
|
print_capabilities = 1;
|
|
|
|
break;
|
|
|
|
case 'S':
|
|
|
|
context_file = optarg;
|
|
|
|
break;
|
2018-02-07 18:49:52 +01:00
|
|
|
case 'R':
|
|
|
|
root_path = optarg;
|
|
|
|
break;
|
2015-07-09 18:54:55 +02:00
|
|
|
case 'D':
|
|
|
|
product_out_path = optarg;
|
|
|
|
break;
|
2013-09-08 02:10:29 +02:00
|
|
|
default:
|
|
|
|
usage();
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (context_file != NULL) {
|
|
|
|
sehnd = get_sehnd(context_file);
|
|
|
|
}
|
2009-03-04 04:28:42 +01:00
|
|
|
|
2018-02-07 18:49:52 +01:00
|
|
|
if (root_path != NULL) {
|
|
|
|
size_t root_len = strlen(root_path);
|
|
|
|
/* Trim any trailing slashes from the root path. */
|
|
|
|
while (root_len && root_path[--root_len] == '/') {
|
|
|
|
root_path[root_len] = '\0';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-03-04 04:28:42 +01:00
|
|
|
while (fgets(buffer, 1023, stdin) != NULL) {
|
|
|
|
int is_dir = 0;
|
|
|
|
int i;
|
|
|
|
for (i = 0; i < 1024 && buffer[i]; ++i) {
|
|
|
|
switch (buffer[i]) {
|
|
|
|
case '\n':
|
|
|
|
buffer[i-is_dir] = '\0';
|
2016-06-24 11:18:53 +02:00
|
|
|
if (i == 0) {
|
|
|
|
is_dir = 1; // empty line is considered as root directory
|
|
|
|
}
|
2009-03-04 04:28:42 +01:00
|
|
|
i = 1025;
|
|
|
|
break;
|
|
|
|
case '/':
|
|
|
|
is_dir = 1;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
is_dir = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned uid = 0, gid = 0, mode = 0;
|
2013-02-20 21:43:51 +01:00
|
|
|
uint64_t capabilities;
|
2015-07-09 18:54:55 +02:00
|
|
|
fs_config(buffer, is_dir, product_out_path, &uid, &gid, &mode, &capabilities);
|
2018-02-07 18:49:52 +01:00
|
|
|
if (root_path != NULL && strcmp(buffer, root_path) == 0) {
|
|
|
|
/* The root of the filesystem needs to be an empty string. */
|
|
|
|
strcpy(buffer, "");
|
|
|
|
}
|
2013-09-08 02:10:29 +02:00
|
|
|
printf("%s %d %d %o", buffer, uid, gid, mode);
|
|
|
|
|
|
|
|
if (sehnd != NULL) {
|
|
|
|
size_t buffer_strlen = strnlen(buffer, sizeof(buffer));
|
|
|
|
if (buffer_strlen >= sizeof(buffer)) {
|
|
|
|
fprintf(stderr, "non null terminated buffer, aborting\n");
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
size_t full_name_size = buffer_strlen + 2;
|
|
|
|
char* full_name = (char*) malloc(full_name_size);
|
|
|
|
if (full_name == NULL) {
|
|
|
|
perror("malloc");
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
|
|
|
full_name[0] = '/';
|
|
|
|
strncpy(full_name + 1, buffer, full_name_size - 1);
|
|
|
|
full_name[full_name_size - 1] = '\0';
|
|
|
|
|
|
|
|
char* secontext;
|
|
|
|
if (selabel_lookup(sehnd, &secontext, full_name, ( mode | (is_dir ? S_IFDIR : S_IFREG)))) {
|
|
|
|
secontext = strdup("u:object_r:unlabeled:s0");
|
|
|
|
}
|
|
|
|
|
|
|
|
printf(" selabel=%s", secontext);
|
|
|
|
free(full_name);
|
|
|
|
freecon(secontext);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (print_capabilities) {
|
|
|
|
printf(" capabilities=0x%" PRIx64, capabilities);
|
|
|
|
}
|
|
|
|
|
|
|
|
printf("\n");
|
2009-03-04 04:28:42 +01:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|