#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <rsbac/types.h>
#include <rsbac/aci_data_structures.h>
#include <rsbac/getname.h>
#include <rsbac/syscalls.h>
#include <rsbac/error.h>

int main(int argc, char ** argv)
{
  int attr_list[RSBAC_PROCESS_NR_ATTRIBUTES] = RSBAC_PROCESS_ATTR_LIST;
  int res = 0;
  char tmp1[80],tmp2[120];
  int j;
  union rsbac_attribute_value_t value;
  union rsbac_target_id_t tid;
  enum rsbac_attribute_t attr;
  
  if (argc == 4)
    {
      attr = get_attribute_nr(argv[2]);
      if(attr == A_none)
        {
          fprintf(stderr, "%s: Invalid Attribute %s!\n", argv[0], argv[2]);
          exit(1);
        }
      switch(attr)
        {
          case A_owner: value.owner = strtol(argv[3],0,0);
                        break;
          default: value.dummy = strtol(argv[3],0,0);
        }
      tid.process = strtol(argv[1],0,0);
      res = rsbac_set_attr(T_PROCESS, &tid, attr, &value);
      if(res)
        {
          if((res > -RSBAC_EPERM) || (res < -RSBAC_EDECISIONMISMATCH))
            {
              fprintf(stderr, "%s: Error: %s\n", argv[0], strerror(-res));
            }
          else
            {
              get_error_name(tmp1,res);
              fprintf(stderr, "%s: error: %s\n", argv[0], tmp1);
            }
          exit(1);
        }
      else
        exit(0);
    }
  else
    {
      printf("%s (RSBAC %s)\n***\n", argv[0], VERSION);
      printf("Use: %s process-id attribute value\n", argv[0]);  
      printf("- attribute (string) and value (integer) = see following list:\n");
      for (j=0;j<RSBAC_PROCESS_NR_ATTRIBUTES;j++)
        {
          get_attribute_name(tmp1,attr_list[j]);
          get_attribute_param(tmp2,attr_list[j]);
          printf("%s \t%s\n",tmp1,tmp2);
        }
    }
  exit(1);
}
