#include <errno.h>
#include <pwd.h>
#include "smtpd.h"
static int table_getpwnam_config(struct table *);
static int table_getpwnam_lookup(struct table *, enum table_service, const char *,
char **);
struct table_backend table_backend_getpwnam = {
.name = "getpwnam",
.services = K_USERINFO,
.config = table_getpwnam_config,
.add = NULL,
.dump = NULL,
.open = NULL,
.update = NULL,
.close = NULL,
.lookup = table_getpwnam_lookup,
.fetch = NULL,
};
static int
table_getpwnam_config(struct table *table)
{
if (table->t_config[0])
return 0;
return 1;
}
static int
table_getpwnam_lookup(struct table *table, enum table_service kind, const char *key,
char **dst)
{
struct passwd *pw;
if (kind != K_USERINFO)
return -1;
errno = 0;
do {
pw = getpwnam(key);
} while (pw == NULL && errno == EINTR);
if (pw == NULL) {
if (errno)
return -1;
return 0;
}
if (dst == NULL)
return 1;
if (asprintf(dst, "%d:%d:%s",
pw->pw_uid,
pw->pw_gid,
pw->pw_dir) == -1) {
*dst = NULL;
return -1;
}
return (1);
}