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