#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>

int main(int argc, char ** argv)
{
  int res = 0;
  enum rsbac_security_level_t seclevel;
  char name[80];
  char * progname;
  int verbose = 0;
  
  progname = argv[0];
  if ((argc > 1) && (!strcmp("-v",argv[1])) )
    {
      verbose=1;
      argv++;
      argc--;
    }  
  if (argc >= 3)
    {
      seclevel=strtol(argv[1],0,0);
      if(verbose)
        printf("%s: executing %s with maximum_sec_level %i\n",
               progname,argv[2],seclevel);
      res = rsbac_mac_set_max_seclevel(seclevel);
      if(res)
        {
          if((res > -RSBAC_EPERM) || (res < -RSBAC_EDECISIONMISMATCH))
            fprintf(stderr,"%s: error: %s\n", progname, strerror(-res));
          else
            {
              get_error_name(name,res);
              fprintf(stderr,"%s: error: %s\n", progname, name);
            }
          exit(1);
        }
      res = execvp(argv[2],&argv[2]);
      if(res)
        {
          if((res > -RSBAC_EPERM) || (res < -RSBAC_EDECISIONMISMATCH))
            fprintf(stderr, "%s: error: %s\n", progname, strerror(-res));
          else
            {
              get_error_name(name,res);
              fprintf(stderr, "%s: error: %s\n", progname, name);
            }
        }
    }
  else
    {
      printf("%s (RSBAC %s)\n***\n", argv[0], VERSION);
      printf("Use: %s [-v] seclevel prog args\n", progname);
      printf("This program will set rsbac_mac_max_seclevel to seclevel and then\n");
      printf("execute prog via execvp()\n");
      printf("-v = verbose\n");
    }
  return (res);
}

