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 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230
|
/*
* No copyright is claimed. This code is in the public domain; do with
* it what you wish.
*/
#include <stdlib.h>
#include <assert.h>
#include <unistd.h>
#include "c.h"
#include "pwdutils.h"
#include "xalloc.h"
#include "strutils.h"
struct passwd *xgetpwuid(uid_t uid, char **pwdbuf)
{
struct passwd *pwd = NULL, *res = NULL;
int rc;
assert(pwdbuf);
*pwdbuf = xmalloc(UL_GETPW_BUFSIZ);
pwd = xcalloc(1, sizeof(struct passwd));
errno = 0;
rc = getpwuid_r(uid, pwd, *pwdbuf, UL_GETPW_BUFSIZ, &res);
if (rc != 0) {
errno = rc;
goto failed;
}
if (!res) {
errno = EINVAL;
goto failed;
}
return pwd;
failed:
free(pwd);
free(*pwdbuf);
return NULL;
}
/* Returns allocated passwd and allocated pwdbuf for the username or UID passed
* as @str. In case of error returns NULL and sets errno, for unknown user it
* sets errno to EINVAL.
*/
struct passwd *xgetuserpw(const char *str ,char **pwdbuf)
{
struct passwd *pwd = NULL, *res = NULL;
int rc;
uint64_t uid;
assert(pwdbuf);
assert(str);
*pwdbuf = xmalloc(UL_GETPW_BUFSIZ);
pwd = xcalloc(1, sizeof(struct passwd));
/* is @str a UID ? */
rc = ul_strtou64(str, &uid, 10);
if (rc == -ERANGE) {
errno = ERANGE;
goto failed;
}
/* @str is an invalid number, let's assume it is the username */
if (rc == -EINVAL) {
rc = getpwnam_r(str, pwd, *pwdbuf, UL_GETPW_BUFSIZ, &res);
} else {
if (uid > MAX_OF_UINT_TYPE(uid_t)) {
errno = ERANGE;
goto failed;
}
rc = getpwuid_r((uid_t)uid, pwd, *pwdbuf, UL_GETPW_BUFSIZ, &res);
}
if (rc != 0) {
errno = rc;
goto failed;
}
if (!res) {
errno = EINVAL;
goto failed;
}
return pwd;
failed:
free(pwd);
free(*pwdbuf);
return NULL;
}
/* Returns allocated group and allocated grpbuf for the group name or GID passed
* as @str. In case of error returns NULL and sets errno, for unknown group it
* sets errno to EINVAL.
*/
struct group *xgetgroup(const char *str, char **grpbuf)
{
struct group *grp = NULL, *res = NULL;
int rc;
uint64_t gid;
assert(grpbuf);
assert(str);
*grpbuf = xmalloc(UL_GETPW_BUFSIZ);
grp = xcalloc(1, sizeof(struct group));
/* is @str a GID ? */
rc = ul_strtou64(str, &gid, 10);
if (rc == -ERANGE) {
errno = ERANGE;
goto failed;
}
/* @str is an invalid number, let's assume it is the group name */
if (rc == -EINVAL) {
rc = getgrnam_r(str, grp, *grpbuf, UL_GETPW_BUFSIZ, &res);
} else {
if (gid > MAX_OF_UINT_TYPE(gid_t)) {
errno = ERANGE;
goto failed;
}
rc = getgrgid_r((gid_t)gid, grp, *grpbuf, UL_GETPW_BUFSIZ, &res);
}
if (rc != 0) {
errno = rc;
goto failed;
}
if (!res) {
errno = EINVAL;
goto failed;
}
return grp;
failed:
free(grp);
free(*grpbuf);
return NULL;
}
char *xgetlogin(void)
{
struct passwd *pw = NULL;
uid_t ruid;
/* GNU Hurd implementation has an extension where a process can exist in a
* non-conforming environment, and thus be outside the realms of POSIX
* process identifiers; on this platform, getuid() fails with a status of
* (uid_t)(-1) and sets errno if a program is run from a non-conforming
* environment.
*
* http://austingroupbugs.net/view.php?id=511
*
* The same implementation is useful for other systems, since getlogin(3)
* shouldn't be used as actual identification.
*/
errno = 0;
ruid = getuid();
if (errno == 0)
pw = getpwuid(ruid);
if (pw && pw->pw_name && *pw->pw_name)
return xstrdup(pw->pw_name);
return NULL;
}
/*
* Return a pointer to a `struct group` for a matching group name or GID.
*/
struct group *ul_getgrp_str(const char *str)
{
int rc;
uint64_t gid;
rc = ul_strtou64(str, &gid, 10);
if (rc == -ERANGE)
return NULL;
if (rc == -EINVAL)
return getgrnam(str);
if (gid > MAX_OF_UINT_TYPE(gid_t))
return NULL;
return getgrgid((gid_t)gid);
}
/*
* Return a pointer to a `struct passwd` for a matching username or UID.
*/
struct passwd *ul_getuserpw_str(const char *str)
{
int rc;
uint64_t uid;
rc = ul_strtou64(str, &uid, 10);
if (rc == -ERANGE)
return NULL;
if (rc == -EINVAL)
return getpwnam(str);
if (uid > MAX_OF_UINT_TYPE(uid_t))
return NULL;
return getpwuid((uid_t)uid);
}
#ifdef TEST_PROGRAM
int main(int argc, char *argv[])
{
char *buf = NULL;
struct passwd *pwd = NULL;
if (argc != 2) {
fprintf(stderr, "usage: %s <username>\n", argv[0]);
return EXIT_FAILURE;
}
pwd = xgetuserpw(argv[1], &buf);
if (!pwd)
err(EXIT_FAILURE, "failed to get %s pwd entry", argv[1]);
printf("Username: %s\n", pwd->pw_name);
printf("UID: %d\n", pwd->pw_uid);
printf("HOME: %s\n", pwd->pw_dir);
printf("GECO: %s\n", pwd->pw_gecos);
free(pwd);
free(buf);
printf("Current: %s\n", (buf = xgetlogin()));
free(buf);
return EXIT_SUCCESS;
}
#endif /* TEST_PROGRAM */
|