Home | History | Annotate | Download | only in fs_config
      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 #include <inttypes.h>
     24 
     25 #include <selinux/selinux.h>
     26 #include <selinux/label.h>
     27 #include <selinux/android.h>
     28 
     29 #include "private/android_filesystem_config.h"
     30 
     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 //
     36 // 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 //
     41 // Example input:
     42 //
     43 //      system/etc/dbus.conf
     44 //      data/app/
     45 //
     46 // Output:
     47 //
     48 //      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
     55 //
     56 // Note that the output will omit the trailing slash from
     57 // directories.
     58 
     59 static 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 
     70 static void usage() {
     71   fprintf(stderr, "Usage: fs_config [-S context_file] [-C]\n");
     72 }
     73 
     74 int main(int argc, char** argv) {
     75   char buffer[1024];
     76   const char* context_file = NULL;
     77   struct selabel_handle* sehnd = NULL;
     78   int print_capabilities = 0;
     79   int opt;
     80   while((opt = getopt(argc, argv, "CS:")) != -1) {
     81     switch(opt) {
     82     case 'C':
     83       print_capabilities = 1;
     84       break;
     85     case 'S':
     86       context_file = optarg;
     87       break;
     88     default:
     89       usage();
     90       exit(EXIT_FAILURE);
     91     }
     92   }
     93 
     94   if (context_file != NULL) {
     95     sehnd = get_sehnd(context_file);
     96   }
     97 
     98   while (fgets(buffer, 1023, stdin) != NULL) {
     99     int is_dir = 0;
    100     int i;
    101     for (i = 0; i < 1024 && buffer[i]; ++i) {
    102       switch (buffer[i]) {
    103         case '\n':
    104           buffer[i-is_dir] = '\0';
    105           i = 1025;
    106           break;
    107         case '/':
    108           is_dir = 1;
    109           break;
    110         default:
    111           is_dir = 0;
    112           break;
    113       }
    114     }
    115 
    116     unsigned uid = 0, gid = 0, mode = 0;
    117     uint64_t capabilities;
    118     fs_config(buffer, is_dir, &uid, &gid, &mode, &capabilities);
    119     printf("%s %d %d %o", buffer, uid, gid, mode);
    120 
    121     if (sehnd != NULL) {
    122       size_t buffer_strlen = strnlen(buffer, sizeof(buffer));
    123       if (buffer_strlen >= sizeof(buffer)) {
    124         fprintf(stderr, "non null terminated buffer, aborting\n");
    125         exit(EXIT_FAILURE);
    126       }
    127       size_t full_name_size = buffer_strlen + 2;
    128       char* full_name = (char*) malloc(full_name_size);
    129       if (full_name == NULL) {
    130         perror("malloc");
    131         exit(EXIT_FAILURE);
    132       }
    133 
    134       full_name[0] = '/';
    135       strncpy(full_name + 1, buffer, full_name_size - 1);
    136       full_name[full_name_size - 1] = '\0';
    137 
    138       char* secontext;
    139       if (selabel_lookup(sehnd, &secontext, full_name, ( mode | (is_dir ? S_IFDIR : S_IFREG)))) {
    140         secontext = strdup("u:object_r:unlabeled:s0");
    141       }
    142 
    143       printf(" selabel=%s", secontext);
    144       free(full_name);
    145       freecon(secontext);
    146     }
    147 
    148     if (print_capabilities) {
    149       printf(" capabilities=0x%" PRIx64, capabilities);
    150     }
    151 
    152     printf("\n");
    153   }
    154   return 0;
    155 }
    156