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

int main(int argc, char ** argv)
{
  int res = 0;
  char tmp1[80],tmp2[120];
  char *key = (void *) 0;
  int i;
  int id;
  union rsbac_attribute_value_t value;
  enum rsbac_target_t target;
  union rsbac_target_id_t tid;
  enum rsbac_attribute_t attr;
  struct passwd * user_info_p;
  char * progname = argv[0];
  int inherit = 0;
  
  if ((argc > 1) && (!strcmp("-e",argv[1])) )
    {
      inherit=1;
      argv++;
      argc--;
    }  
  
  if (argc > 3)
    {
      printf("%s: %i targets\n\n", progname, argc - 3);
      target = get_target_nr(argv[1]);
      if(   (target != T_PROCESS)
         && (target != T_USER))
        {
          fprintf(stderr, "%s: Invalid Target %s!\n", progname, argv[1]);
          exit(1);
        }
      attr = get_attribute_nr(argv[2]);
      if(attr == A_none)
        {
          fprintf(stderr, "%s: Invalid Attribute %s!\n", progname, argv[2]);
          exit(1);
        }
      for (i=1;i < (argc-2);i++)
        {
          value.dummy = -1;
          if (target == T_PROCESS)
            {
              id = strtol(argv[i+2],0,0);
              printf("Processing process %i, attribute %s (No. %i)\n",
                      id, argv[2], attr);
              tid.process = id;
              inherit = 0;
            }
          else
            {
              if(   (!(user_info_p = getpwnam(argv[i+2])))
                 && (!(user_info_p = getpwuid(strtol(argv[i+2],0,0)))) )
                {
                  fprintf(stderr, "Invalid user %s!\n\n", argv[i+2]);
                  continue;
                }
              printf("Processing user %s (uid %i), attribute %s (No. %i)\n",
                     user_info_p->pw_name, user_info_p->pw_uid, argv[2], attr);
              tid.user = user_info_p->pw_uid;
            }

          res = rsbac_get_attr(target, &tid, attr, &value, inherit);
          if(res)
            {
              if((res > -RSBAC_EPERM) || (res < -RSBAC_EDECISIONMISMATCH))
                fprintf(stderr, "error: %s\n", strerror(-res));
              else
                {
                  get_error_name(tmp1,res);
                  fprintf(stderr, "error: %s\n", tmp1);
                }
            }
          else
            printf("Returned value: %i\n",value.dummy);
        }
    }
  else
    {
      printf("%s (RSBAC %s)\n***\n", progname, VERSION);
      printf("Use: %s target-type attribute user(s)/proc-no.\n\n", progname);  
      printf("- target-type = USER or PROCESS,\n");
      printf("- attribute (string) and returned value = see following list:\n");
      for (i=0;i<15;i++)
        {
          get_attribute_name(tmp1,i);
          get_attribute_param(tmp2,i);
          printf("%s \t%s\n",tmp1,tmp2);
        }
      printf("-- press return --");
      scanf("%c", key);
      for (i=15;i<A_none;i++)
        {
          get_attribute_name(tmp1,i);
          get_attribute_param(tmp2,i);
          printf("%s \t%s\n",tmp1,tmp2);
        }
    }
    
  return (res);
}
