blob: 60c323800d4a10d4fda0563e72f1d23177703872 [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 Kralevichfbbd7952013-07-17 17:43:09 -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>
23
Nick Kralevichfbbd7952013-07-17 17:43:09 -070024#include <selinux/selinux.h>
25#include <selinux/label.h>
26#include <selinux/android.h>
27
The Android Open Source Project88b60792009-03-03 19:28:42 -080028#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//
35// Example input:
36//
Nick Kralevichfbbd7952013-07-17 17:43:09 -070037// system/etc/dbus.conf
38// data/app/
The Android Open Source Project88b60792009-03-03 19:28:42 -080039//
40// Output:
41//
Nick Kralevichfbbd7952013-07-17 17:43:09 -070042// system/etc/dbus.conf 1002 1002 440
43// data/app 1000 1000 771
44//
45// or if -S is used:
46//
47// system/etc/dbus.conf 1002 1002 440 u:object_r:system_file:s0
48// data/app 1000 1000 771 u:object_r:apk_data_file:s0
The Android Open Source Project88b60792009-03-03 19:28:42 -080049//
50// Note that the output will omit the trailing slash from
51// directories.
52
Nick Kralevichfbbd7952013-07-17 17:43:09 -070053static struct selabel_handle* get_sehnd(const char* context_file) {
54 struct selinux_opt seopts[] = { { SELABEL_OPT_PATH, context_file } };
55 struct selabel_handle* sehnd = selabel_open(SELABEL_CTX_FILE, seopts, 1);
56
57 if (!sehnd) {
58 perror("error running selabel_open");
59 exit(EXIT_FAILURE);
60 }
61 return sehnd;
62}
63
64static void usage() {
65 fprintf(stderr, "Usage: fs_config [-S context_file]\n");
66}
67
The Android Open Source Project88b60792009-03-03 19:28:42 -080068int main(int argc, char** argv) {
69 char buffer[1024];
Nick Kralevichfbbd7952013-07-17 17:43:09 -070070 const char* context_file = NULL;
71 struct selabel_handle* sehnd = NULL;
72 int opt;
73 while((opt = getopt(argc, argv, "S:")) != -1) {
74 switch(opt) {
75 case 'S':
76 context_file = optarg;
77 break;
78 default:
79 usage();
80 exit(EXIT_FAILURE);
81 }
82 }
83
84 if (context_file != NULL) {
85 sehnd = get_sehnd(context_file);
86 }
The Android Open Source Project88b60792009-03-03 19:28:42 -080087
88 while (fgets(buffer, 1023, stdin) != NULL) {
89 int is_dir = 0;
90 int i;
91 for (i = 0; i < 1024 && buffer[i]; ++i) {
92 switch (buffer[i]) {
93 case '\n':
94 buffer[i-is_dir] = '\0';
95 i = 1025;
96 break;
97 case '/':
98 is_dir = 1;
99 break;
100 default:
101 is_dir = 0;
102 break;
103 }
104 }
105
106 unsigned uid = 0, gid = 0, mode = 0;
Nick Kralevichfa798092013-02-20 12:43:51 -0800107 uint64_t capabilities;
108 fs_config(buffer, is_dir, &uid, &gid, &mode, &capabilities);
Nick Kralevichfbbd7952013-07-17 17:43:09 -0700109 printf("%s %d %d %o", buffer, uid, gid, mode);
110
111 if (sehnd != NULL) {
112 size_t buffer_strlen = strnlen(buffer, sizeof(buffer));
113 if (buffer_strlen >= sizeof(buffer)) {
114 fprintf(stderr, "non null terminated buffer, aborting\n");
115 exit(EXIT_FAILURE);
116 }
117 size_t full_name_size = buffer_strlen + 2;
118 char* full_name = (char*) malloc(full_name_size);
119 if (full_name == NULL) {
120 perror("malloc");
121 exit(EXIT_FAILURE);
122 }
123
124 full_name[0] = '/';
125 strncpy(full_name + 1, buffer, full_name_size - 1);
126 full_name[full_name_size - 1] = '\0';
127
128 char* secontext;
129 if (selabel_lookup(sehnd, &secontext, full_name, ( mode | (is_dir ? S_IFDIR : S_IFREG)))) {
130 secontext = strdup("u:object_r:unlabeled:s0");
131 }
132
133 printf(" %s", secontext);
134 free(full_name);
135 freecon(secontext);
136 }
137 printf("\n");
The Android Open Source Project88b60792009-03-03 19:28:42 -0800138 }
139 return 0;
140}