summaryrefslogtreecommitdiffstats
path: root/toolbox/id.c
blob: bc792887c12ac3c2263c79eca5a7c02450e5e2b0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <pwd.h>
#include <grp.h>

#ifdef HAVE_SELINUX
#include <selinux/selinux.h>
#endif

static void print_uid(uid_t uid)
{
    struct passwd *pw = getpwuid(uid);

    if (pw) {
        printf("%d(%s)", uid, pw->pw_name);
    } else {
        printf("%d",uid);
    }
}

static void print_gid(gid_t gid)
{
    struct group *gr = getgrgid(gid);
    if (gr) {
        printf("%d(%s)", gid, gr->gr_name);
    } else {
        printf("%d",gid);
    }
}

int id_main(int argc, char **argv)
{
    gid_t list[64];
    int n, max;
#ifdef HAVE_SELINUX
    char *secctx;
#endif

    max = getgroups(64, list);
    if (max < 0) max = 0;

    printf("uid=");
    print_uid(getuid());
    printf(" gid=");
    print_gid(getgid());
    if (max) {
        printf(" groups=");
        print_gid(list[0]);
        for(n = 1; n < max; n++) {
            printf(",");
            print_gid(list[n]);
        }
    }
#ifdef HAVE_SELINUX
    if (getcon(&secctx) == 0) {
        printf(" context=%s", secctx);
        free(secctx);
    }
#endif
    printf("\n");
    return 0;
}