File: posix.c

package info (click to toggle)
pwdb 0.54preD-3
  • links: PTS
  • area: main
  • in suites: hamm, slink
  • size: 644 kB
  • ctags: 797
  • sloc: ansic: 8,459; makefile: 346; sh: 204
file content (100 lines) | stat: -rw-r--r-- 1,910 bytes parent folder | download | duplicates (2)
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#include <stdio.h>
#include <limits.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <grp.h>
#include <pwd.h>

#include <pwdb/pwdb_public.h>

/*
 * prototype to shut off compiler warnings
 */

void pgroup(gid_t gid);
void puser(uid_t uid);
void pallg(void);

/* grp numbers in decimal with name */

void pgroup(gid_t gid)
{
    struct group *grp;

    grp = getgrgid(gid);
    if (grp == NULL) {
	fprintf(stderr, "gid(%ld) is an unknown group\n", (long int) gid);
    } else {
	printf("gid(%ld) = %s\nmembers:\n"
	       , (long int) grp->gr_gid, grp->gr_name);
	while (grp->gr_mem && *grp->gr_mem) {
	    printf("\t%s\n", *grp->gr_mem);
	    (grp->gr_mem)++;
	}
    }
}

/* uid in decimal with name */

void puser(uid_t uid)
{
    struct passwd *pwd;

    pwd = getpwuid(uid);
    if (pwd == NULL) {
	fprintf(stderr, "uid(%ld) is not known\n", (long int) uid);
    } else {
	printf("uid(%ld) = %s runs %s from %s\n"
		, (long int) pwd->pw_uid, pwd->pw_name
		, pwd->pw_shell, pwd->pw_dir);
	pgroup(pwd->pw_gid);
    }
}

/* list all groups */

void pallg(void)
{
    int ngs, i;
    gid_t *gids=NULL;

    ngs = getgroups(0, NULL);
    gids = calloc(ngs, sizeof(*gids));
    ngs = getgroups(ngs, gids);
    printf("Supplementary groups:\n");
    for (i=0; i<ngs; ++i) {
	pgroup(gids[i]);
    }
    printf("---------\n");
    free(gids);
}

void main(void)
{
    const char *user;
    struct passwd *pwd;

    /* identify user invoking this program */
    /* - logname */
    user = getlogin();

    pwd = getpwnam(user);
    if (pwd == NULL) {
	fprintf(stderr, "cannot identify log-name user %s\n", user);
    } else {
	printf("log-name = %s >\n", user);
	puser(pwd->pw_uid);
	printf("--\n");
    }

    /* - current user */
    printf("Invoking user >\n");
    puser(getuid());

    /* - (e)uid and (e)user */
    printf("Effective user >\n");
    puser(geteuid());

    exit(0);
}