#include <sys_defs.h>
#include <unistd.h>
#include <string.h>
#ifdef USE_PATHS_H
#include <paths.h>
#endif
#include <errno.h>
#include <mymalloc.h>
#include <htable.h>
#include <binhash.h>
#include <msg.h>
#include "mypwd.h"
#ifdef GETPW_R_NEEDS_POSIX_PTHREAD_SEMANTICS
#define _POSIX_PTHREAD_SEMANTICS
#endif
#include <pwd.h>
static HTABLE *mypwcache_name = 0;
static BINHASH *mypwcache_uid = 0;
static struct mypasswd *last_pwd;
#ifdef HAVE_POSIX_GETPW_R
#define GETPW_R_BUFSIZ 1024
#endif
#define MYPWD_ERROR_DELAY (30)
static struct mypasswd *mypwenter(const struct passwd * pwd)
{
struct mypasswd *mypwd;
if (mypwcache_name == 0) {
mypwcache_name = htable_create(0);
mypwcache_uid = binhash_create(0);
}
mypwd = (struct mypasswd *) mymalloc(sizeof(*mypwd));
mypwd->refcount = 0;
mypwd->pw_name = mystrdup(pwd->pw_name);
mypwd->pw_passwd = mystrdup(pwd->pw_passwd);
mypwd->pw_uid = pwd->pw_uid;
mypwd->pw_gid = pwd->pw_gid;
mypwd->pw_gecos = mystrdup(pwd->pw_gecos);
mypwd->pw_dir = mystrdup(pwd->pw_dir);
mypwd->pw_shell = mystrdup(*pwd->pw_shell ? pwd->pw_shell : _PATH_BSHELL);
htable_enter(mypwcache_name, mypwd->pw_name, (void *) mypwd);
if (binhash_locate(mypwcache_uid, (void *) &mypwd->pw_uid,
sizeof(mypwd->pw_uid)) == 0)
binhash_enter(mypwcache_uid, (void *) &mypwd->pw_uid,
sizeof(mypwd->pw_uid), (void *) mypwd);
return (mypwd);
}
struct mypasswd *mypwuid(uid_t uid)
{
struct mypasswd *mypwd;
while ((errno = mypwuid_err(uid, &mypwd)) != 0) {
msg_warn("getpwuid_r: %m");
sleep(MYPWD_ERROR_DELAY);
}
return (mypwd);
}
int mypwuid_err(uid_t uid, struct mypasswd ** result)
{
struct passwd *pwd;
struct mypasswd *mypwd;
if (last_pwd != 0) {
if (last_pwd->pw_uid != uid) {
mypwfree(last_pwd);
last_pwd = 0;
} else {
*result = mypwd = last_pwd;
mypwd->refcount++;
return (0);
}
}
if ((mypwd = (struct mypasswd *)
binhash_find(mypwcache_uid, (void *) &uid, sizeof(uid))) == 0) {
#ifdef HAVE_POSIX_GETPW_R
char pwstore[GETPW_R_BUFSIZ];
struct passwd pwbuf;
int err;
err = getpwuid_r(uid, &pwbuf, pwstore, sizeof(pwstore), &pwd);
if (err != 0)
return (err);
if (pwd == 0) {
*result = 0;
return (0);
}
#else
if ((pwd = getpwuid(uid)) == 0) {
*result = 0;
return (0);
}
#endif
mypwd = mypwenter(pwd);
}
*result = last_pwd = mypwd;
mypwd->refcount += 2;
return (0);
}
struct mypasswd *mypwnam(const char *name)
{
struct mypasswd *mypwd;
while ((errno = mypwnam_err(name, &mypwd)) != 0) {
msg_warn("getpwnam_r: %m");
sleep(MYPWD_ERROR_DELAY);
}
return (mypwd);
}
int mypwnam_err(const char *name, struct mypasswd ** result)
{
struct passwd *pwd;
struct mypasswd *mypwd;
if (last_pwd != 0) {
if (strcmp(last_pwd->pw_name, name) != 0) {
mypwfree(last_pwd);
last_pwd = 0;
} else {
*result = mypwd = last_pwd;
mypwd->refcount++;
return (0);
}
}
if ((mypwd = (struct mypasswd *) htable_find(mypwcache_name, name)) == 0) {
#ifdef HAVE_POSIX_GETPW_R
char pwstore[GETPW_R_BUFSIZ];
struct passwd pwbuf;
int err;
err = getpwnam_r(name, &pwbuf, pwstore, sizeof(pwstore), &pwd);
if (err != 0)
return (err);
if (pwd == 0) {
*result = 0;
return (0);
}
#else
if ((pwd = getpwnam(name)) == 0) {
*result = 0;
return (0);
}
#endif
mypwd = mypwenter(pwd);
}
*result = last_pwd = mypwd;
mypwd->refcount += 2;
return (0);
}
void mypwfree(struct mypasswd * mypwd)
{
if (mypwd->refcount < 1)
msg_panic("mypwfree: refcount %d", mypwd->refcount);
if (--mypwd->refcount == 0) {
htable_delete(mypwcache_name, mypwd->pw_name, (void (*) (void *)) 0);
if (binhash_locate(mypwcache_uid, (void *) &mypwd->pw_uid,
sizeof(mypwd->pw_uid)))
binhash_delete(mypwcache_uid, (void *) &mypwd->pw_uid,
sizeof(mypwd->pw_uid), (void (*) (void *)) 0);
myfree(mypwd->pw_name);
myfree(mypwd->pw_passwd);
myfree(mypwd->pw_gecos);
myfree(mypwd->pw_dir);
myfree(mypwd->pw_shell);
myfree((void *) mypwd);
}
}
#ifdef TEST
#include <stdlib.h>
#include <ctype.h>
#include <vstream.h>
#include <msg_vstream.h>
int main(int argc, char **argv)
{
struct mypasswd **mypwd;
int i;
msg_vstream_init(argv[0], VSTREAM_ERR);
if (argc == 1)
msg_fatal("usage: %s name or uid ...", argv[0]);
mypwd = (struct mypasswd **) mymalloc((argc + 2) * sizeof(*mypwd));
for (i = 1; i < argc; i++) {
if (ISDIGIT(argv[i][0]))
mypwd[i] = mypwuid(atoi(argv[i]));
else
mypwd[i] = mypwnam(argv[i]);
if (mypwd[i] == 0)
msg_fatal("%s: not found", argv[i]);
msg_info("lookup %s %s/%d refcount=%d name_cache=%d uid_cache=%d",
argv[i], mypwd[i]->pw_name, mypwd[i]->pw_uid,
mypwd[i]->refcount, mypwcache_name->used, mypwcache_uid->used);
}
mypwd[argc] = last_pwd;
for (i = 1; i < argc + 1; i++) {
msg_info("free %s/%d refcount=%d name_cache=%d uid_cache=%d",
mypwd[i]->pw_name, mypwd[i]->pw_uid, mypwd[i]->refcount,
mypwcache_name->used, mypwcache_uid->used);
mypwfree(mypwd[i]);
}
msg_info("name_cache=%d uid_cache=%d",
mypwcache_name->used, mypwcache_uid->used);
myfree((void *) mypwd);
return (0);
}
#endif