]>
Commit | Line | Data |
---|---|---|
3836cd2d KZ |
1 | /* |
2 | * No copyright is claimed. This code is in the public domain; do with | |
3 | * it what you wish. | |
4 | */ | |
4f5f35fc KZ |
5 | #include <stdlib.h> |
6 | ||
7 | #include "c.h" | |
8 | #include "pwdutils.h" | |
9 | #include "xalloc.h" | |
10 | ||
11 | /* Returns allocated passwd and allocated pwdbuf to store passwd strings | |
12 | * fields. In case of error returns NULL and set errno, for unknown user set | |
13 | * errno to EINVAL | |
14 | */ | |
15 | struct passwd *xgetpwnam(const char *username, char **pwdbuf) | |
16 | { | |
17 | struct passwd *pwd = NULL, *res = NULL; | |
18 | int rc; | |
19 | ||
20 | if (!pwdbuf || !username) | |
21 | return NULL; | |
22 | ||
23 | *pwdbuf = xmalloc(UL_GETPW_BUFSIZ); | |
24 | pwd = xcalloc(1, sizeof(struct passwd)); | |
25 | ||
26 | errno = 0; | |
27 | rc = getpwnam_r(username, pwd, *pwdbuf, UL_GETPW_BUFSIZ, &res); | |
28 | if (rc != 0) { | |
29 | errno = rc; | |
30 | goto failed; | |
31 | } | |
32 | if (!res) { | |
33 | errno = EINVAL; | |
34 | goto failed; | |
35 | } | |
36 | return pwd; | |
37 | failed: | |
38 | free(pwd); | |
39 | free(*pwdbuf); | |
40 | return NULL; | |
41 | } | |
42 | ||
dc96ca29 MHB |
43 | /* Returns allocated group and allocated grpbuf to store group strings |
44 | * fields. In case of error returns NULL and set errno, for unknown group set | |
45 | * errno to EINVAL | |
46 | */ | |
47 | struct group *xgetgrnam(const char *groupname, char **grpbuf) | |
48 | { | |
49 | struct group *grp = NULL, *res = NULL; | |
50 | int rc; | |
51 | ||
52 | if (!grpbuf || !groupname) | |
53 | return NULL; | |
54 | ||
55 | *grpbuf = xmalloc(UL_GETPW_BUFSIZ); | |
56 | grp = xcalloc(1, sizeof(struct group)); | |
57 | ||
58 | errno = 0; | |
59 | rc = getgrnam_r(groupname, grp, *grpbuf, UL_GETPW_BUFSIZ, &res); | |
60 | if (rc != 0) { | |
61 | errno = rc; | |
62 | goto failed; | |
63 | } | |
64 | if (!res) { | |
65 | errno = EINVAL; | |
66 | goto failed; | |
67 | } | |
68 | return grp; | |
69 | failed: | |
70 | free(grp); | |
71 | free(*grpbuf); | |
72 | return NULL; | |
73 | } | |
74 | ||
cd083615 QR |
75 | struct passwd *xgetpwuid(uid_t uid, char **pwdbuf) |
76 | { | |
77 | struct passwd *pwd = NULL, *res = NULL; | |
78 | int rc; | |
79 | ||
80 | if (!pwdbuf) | |
81 | return NULL; | |
82 | ||
83 | *pwdbuf = xmalloc(UL_GETPW_BUFSIZ); | |
84 | pwd = xcalloc(1, sizeof(struct passwd)); | |
85 | ||
86 | errno = 0; | |
87 | rc = getpwuid_r(uid, pwd, *pwdbuf, UL_GETPW_BUFSIZ, &res); | |
88 | if (rc != 0) { | |
89 | errno = rc; | |
90 | goto failed; | |
91 | } | |
92 | if (!res) { | |
93 | errno = EINVAL; | |
94 | goto failed; | |
95 | } | |
96 | return pwd; | |
97 | failed: | |
98 | free(pwd); | |
99 | free(*pwdbuf); | |
100 | return NULL; | |
101 | } | |
102 | ||
1742c8d8 KZ |
103 | char *xgetlogin(void) |
104 | { | |
105 | struct passwd *pw = NULL; | |
106 | uid_t ruid; | |
107 | char *user; | |
108 | ||
109 | user = getlogin(); | |
110 | if (user) | |
111 | return xstrdup(user); | |
112 | ||
113 | /* GNU Hurd implementation has an extension where a process can exist in a | |
114 | * non-conforming environment, and thus be outside the realms of POSIX | |
115 | * process identifiers; on this platform, getuid() fails with a status of | |
116 | * (uid_t)(-1) and sets errno if a program is run from a non-conforming | |
117 | * environment. | |
118 | * | |
119 | * http://austingroupbugs.net/view.php?id=511 | |
120 | */ | |
121 | errno = 0; | |
122 | ruid = getuid(); | |
123 | ||
124 | if (errno == 0) | |
125 | pw = getpwuid(ruid); | |
126 | if (pw && pw->pw_name && *pw->pw_name) | |
127 | return xstrdup(pw->pw_name); | |
128 | ||
129 | return NULL; | |
130 | } | |
4f5f35fc KZ |
131 | |
132 | #ifdef TEST_PROGRAM | |
133 | int main(int argc, char *argv[]) | |
134 | { | |
1742c8d8 | 135 | char *buf = NULL; |
4f5f35fc KZ |
136 | struct passwd *pwd = NULL; |
137 | ||
138 | if (argc != 2) { | |
139 | fprintf(stderr, "usage: %s <username>\n", argv[0]); | |
140 | return EXIT_FAILURE; | |
141 | } | |
142 | ||
1742c8d8 | 143 | pwd = xgetpwnam(argv[1], &buf); |
4f5f35fc KZ |
144 | if (!pwd) |
145 | err(EXIT_FAILURE, "failed to get %s pwd entry", argv[1]); | |
146 | ||
147 | printf("Username: %s\n", pwd->pw_name); | |
148 | printf("UID: %d\n", pwd->pw_uid); | |
149 | printf("HOME: %s\n", pwd->pw_dir); | |
150 | printf("GECO: %s\n", pwd->pw_gecos); | |
151 | ||
152 | free(pwd); | |
1742c8d8 KZ |
153 | free(buf); |
154 | ||
155 | printf("Current: %s\n", (buf = xgetlogin())); | |
156 | free(buf); | |
157 | ||
4f5f35fc KZ |
158 | return EXIT_SUCCESS; |
159 | } | |
160 | #endif /* TEST_PROGRAM */ |