/*************************************************** */
/* Rule Set Based Access Control                     */
/*                                                   */
/* Author and (c) 1999-2014: Amon Ott <ao@rsbac.org> */
/*                                                   */
/* Last modified: 16/Oct/2014                        */
/*************************************************** */

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <linux/kdev_t.h>
#include <rsbac/types.h>
#include <rsbac/syscalls.h>
#include <rsbac/getname.h>
#include <rsbac/error.h>
#include <rsbac/helpers.h>
#include "nls.h"
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

char * progname;

void use(void)
  {
    printf(gettext("%s (RSBAC %s)\n***\n"), progname, VERSION);
    printf(gettext("Use: %s [-v] [-c] [-x] [-n] [-a]\n"), progname);
    printf(gettext("This program will show the RSBAC MAC security levels\n"));
    printf(gettext("and category sets of the calling process.\n"));
    printf(gettext(" -h = this help, -- = no more flags,\n"));
    printf(gettext("-a = show all, -c = show current level and categories\n"));
    printf(gettext("-x = show max, -n = show min level and categories\n"));
  }

int main(int argc, char ** argv)
{
  int res = 0;
  rsbac_security_level_t seclevel;
  rsbac_mac_category_vector_t categories;
  char tmp[RSBAC_MAXNAMELEN];
  int verbose = 0;
  int max = 0;
  int min = 0;
  int current = 0;
  int all = 1;
  u_int stopflags = FALSE;

  locale_init();
  
  progname = argv[0];
  while((argc > 1) && (argv[1][0] == '-') && !stopflags)
    {
      char * pos = argv[1];
      pos++;
      while(*pos)
        {
          switch(*pos)
            {
              case '-':
                stopflags = TRUE;
                break;
              case 'h':
                use();
                return 0;
              case 'v':
                verbose++;
                break;
              case 'x':
                max=1;
                all=0;
                break;
              case 'n':
                min=1;
                all=0;
                break;
              case 'c':
                current=1;
                all=0;
                break;
              case 'a':
                all=1;
                break;
              default:
                use();
                exit (1);
            }
          pos++;
        }
      argv++;
      argc--;
    }
  if(current || all)
    {
      res = rsbac_mac_get_curr_level(&seclevel, &categories);
      error_exit(res);
      printf(gettext("Current level: %u\ncategories: %s\n"),
             seclevel,
             u64tostrmac(tmp, categories));
    }
  if(max || all)
    {
      res = rsbac_mac_get_max_level(&seclevel, &categories);
      error_exit(res);
      printf(gettext("Max level:     %u\ncategories: %s\n"),
             seclevel,
             u64tostrmac(tmp, categories));
    }
  if(min || all)
    {
      res = rsbac_mac_get_min_level(&seclevel, &categories);
      error_exit(res);
      printf(gettext("Min level:     %u\ncategories: %s\n"),
             seclevel,
             u64tostrmac(tmp, categories));
    }
  return 0;
}

