#include "sys_defs.h"
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <pwd.h>
#include <grp.h>
#include "msg.h"
#include "mymalloc.h"
#include "vstring.h"
#include "dict.h"
#include "dict_unix.h"
typedef struct {
DICT dict;
} DICT_UNIX;
static const char *dict_unix_getpwnam(DICT *unused_dict, const char *key)
{
struct passwd *pwd;
static VSTRING *buf;
static int sanity_checked;
dict_errno = 0;
if ((pwd = getpwnam(key)) == 0) {
if (sanity_checked == 0) {
sanity_checked = 1;
errno = 0;
if (getpwuid(0) == 0) {
msg_warn("cannot access UNIX password database: %m");
dict_errno = DICT_ERR_RETRY;
}
}
return (0);
} else {
if (buf == 0)
buf = vstring_alloc(10);
sanity_checked = 1;
vstring_sprintf(buf, "%s:%s:%ld:%ld:%s:%s:%s",
pwd->pw_name, pwd->pw_passwd, (long) pwd->pw_uid,
(long) pwd->pw_gid, pwd->pw_gecos, pwd->pw_dir,
pwd->pw_shell);
return (vstring_str(buf));
}
}
static const char *dict_unix_getgrnam(DICT *unused_dict, const char *key)
{
struct group *grp;
static VSTRING *buf;
char **cpp;
static int sanity_checked;
dict_errno = 0;
if ((grp = getgrnam(key)) == 0) {
if (sanity_checked == 0) {
sanity_checked = 1;
errno = 0;
if (getgrgid(0) == 0) {
msg_warn("cannot access UNIX group database: %m");
dict_errno = DICT_ERR_RETRY;
}
}
return (0);
} else {
if (buf == 0)
buf = vstring_alloc(10);
sanity_checked = 1;
vstring_sprintf(buf, "%s:%s:%ld:",
grp->gr_name, grp->gr_passwd, (long) grp->gr_gid);
for (cpp = grp->gr_mem; *cpp; cpp++) {
vstring_strcat(buf, *cpp);
if (cpp[1])
VSTRING_ADDCH(buf, ',');
}
VSTRING_TERMINATE(buf);
return (vstring_str(buf));
}
}
static void dict_unix_close(DICT *dict)
{
dict_free(dict);
}
DICT *dict_unix_open(const char *map, int unused_flags, int dict_flags)
{
DICT_UNIX *dict_unix;
struct dict_unix_lookup {
char *name;
const char *(*lookup) (DICT *, const char *);
};
static struct dict_unix_lookup dict_unix_lookup[] = {
"passwd.byname", dict_unix_getpwnam,
"group.byname", dict_unix_getgrnam,
0,
};
struct dict_unix_lookup *lp;
dict_errno = 0;
dict_unix = (DICT_UNIX *) dict_alloc(DICT_TYPE_UNIX, map,
sizeof(*dict_unix));
for (lp = dict_unix_lookup; ; lp++) {
if (lp->name == 0)
msg_fatal("dict_unix_open: unknown map name: %s", map);
if (strcmp(map, lp->name) == 0)
break;
}
dict_unix->dict.lookup = lp->lookup;
dict_unix->dict.close = dict_unix_close;
dict_unix->dict.flags = dict_flags | DICT_FLAG_FIXED;
return (DICT_DEBUG (&dict_unix->dict));
}