Home | History | Annotate | Download | only in toolbox
      1 #include <stdio.h>
      2 #include <stdlib.h>
      3 #include <unistd.h>
      4 #include <sys/types.h>
      5 #include <pwd.h>
      6 #include <grp.h>
      7 #include <selinux/selinux.h>
      8 
      9 static void print_uid(uid_t uid)
     10 {
     11     struct passwd *pw = getpwuid(uid);
     12 
     13     if (pw) {
     14         printf("%d(%s)", uid, pw->pw_name);
     15     } else {
     16         printf("%d",uid);
     17     }
     18 }
     19 
     20 static void print_gid(gid_t gid)
     21 {
     22     struct group *gr = getgrgid(gid);
     23     if (gr) {
     24         printf("%d(%s)", gid, gr->gr_name);
     25     } else {
     26         printf("%d",gid);
     27     }
     28 }
     29 
     30 int id_main(int argc, char **argv)
     31 {
     32     gid_t list[64];
     33     int n, max;
     34     char *secctx;
     35 
     36     max = getgroups(64, list);
     37     if (max < 0) max = 0;
     38 
     39     printf("uid=");
     40     print_uid(getuid());
     41     printf(" gid=");
     42     print_gid(getgid());
     43     if (max) {
     44         printf(" groups=");
     45         print_gid(list[0]);
     46         for(n = 1; n < max; n++) {
     47             printf(",");
     48             print_gid(list[n]);
     49         }
     50     }
     51     if (getcon(&secctx) == 0) {
     52         printf(" context=%s", secctx);
     53         free(secctx);
     54     }
     55     printf("\n");
     56     return 0;
     57 }
     58