fs_config.c revision fbbd79530adc6ddd6bbfb3c5fc60ba5ec0ce5f2d
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>
18#include <stdlib.h>
19#include <sys/stat.h>
20#include <errno.h>
21#include <unistd.h>
22#include <string.h>
23
24#include <selinux/selinux.h>
25#include <selinux/label.h>
26#include <selinux/android.h>
27
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//
35// Example input:
36//
37//      system/etc/dbus.conf
38//      data/app/
39//
40// Output:
41//
42//      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
49//
50// Note that the output will omit the trailing slash from
51// directories.
52
53static 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
68int main(int argc, char** argv) {
69  char buffer[1024];
70  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  }
87
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;
107    uint64_t capabilities;
108    fs_config(buffer, is_dir, &uid, &gid, &mode, &capabilities);
109    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");
138  }
139  return 0;
140}
141