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 
      8 #ifdef HAVE_SELINUX
      9 #include <selinux/selinux.h>
     10 #endif
     11 
     12 static void print_uid(uid_t uid)
     13 {
     14     struct passwd *pw = getpwuid(uid);
     15 
     16     if (pw) {
     17         printf("%d(%s)", uid, pw->pw_name);
     18     } else {
     19         printf("%d",uid);
     20     }
     21 }
     22 
     23 static void print_gid(gid_t gid)
     24 {
     25     struct group *gr = getgrgid(gid);
     26     if (gr) {
     27         printf("%d(%s)", gid, gr->gr_name);
     28     } else {
     29         printf("%d",gid);
     30     }
     31 }
     32 
     33 int id_main(int argc, char **argv)
     34 {
     35     gid_t list[64];
     36     int n, max;
     37 #ifdef HAVE_SELINUX
     38     char *secctx;
     39 #endif
     40 
     41     max = getgroups(64, list);
     42     if (max < 0) max = 0;
     43 
     44     printf("uid=");
     45     print_uid(getuid());
     46     printf(" gid=");
     47     print_gid(getgid());
     48     if (max) {
     49         printf(" groups=");
     50         print_gid(list[0]);
     51         for(n = 1; n < max; n++) {
     52             printf(",");
     53             print_gid(list[n]);
     54         }
     55     }
     56 #ifdef HAVE_SELINUX
     57     if (getcon(&secctx) == 0) {
     58         printf(" context=%s", secctx);
     59         free(secctx);
     60     }
     61 #endif
     62     printf("\n");
     63     return 0;
     64 }
     65