userdb-passwd.c revision b71b9e5cd06b1dc13aad122a0729d6eeda5393e7
0N/A/* Copyright (c) 2002-2011 Dovecot authors, see the included COPYING file */
0N/A
0N/A#include "auth-common.h"
0N/A#include "userdb.h"
0N/A
0N/A#ifdef USERDB_PASSWD
0N/A
0N/A#include "ioloop.h"
0N/A#include "ipwd.h"
0N/A#include "time-util.h"
0N/A#include "userdb-template.h"
0N/A
0N/A#define USER_CACHE_KEY "%u"
0N/A#define PASSWD_SLOW_WARN_MSECS (10*1000)
0N/A#define PASSWD_SLOW_MASTER_WARN_MSECS 50
0N/A#define PASSDB_SLOW_MASTER_WARN_COUNT_INTERVAL 100
0N/A#define PASSDB_SLOW_MASTER_WARN_MIN_PERCENTAGE 5
0N/A
0N/Astruct passwd_userdb_module {
0N/A struct userdb_module module;
0N/A struct userdb_template *tmpl;
0N/A
0N/A unsigned int fast_count, slow_count;
0N/A unsigned int slow_warned:1;
0N/A};
0N/A
0N/Astruct passwd_userdb_iterate_context {
0N/A struct userdb_iterate_context ctx;
0N/A struct passwd_userdb_iterate_context *next_waiting;
0N/A};
0N/A
0N/Astatic struct passwd_userdb_iterate_context *cur_userdb_iter = NULL;
0N/Astatic struct timeout *cur_userdb_iter_to = NULL;
0N/A
0N/Astatic void
0N/Apasswd_check_warnings(struct auth_request *auth_request,
0N/A struct passwd_userdb_module *module,
0N/A const struct timeval *start_tv)
0N/A{
0N/A struct timeval end_tv;
0N/A unsigned int msecs, percentage;
0N/A
0N/A if (gettimeofday(&end_tv, NULL) < 0)
0N/A return;
0N/A
0N/A msecs = timeval_diff_msecs(&end_tv, start_tv);
0N/A if (msecs >= PASSWD_SLOW_WARN_MSECS) {
0N/A i_warning("passwd: Lookup for %s took %u secs",
0N/A auth_request->user, msecs/1000);
0N/A return;
0N/A }
0N/A if (worker || module->slow_warned)
0N/A return;
0N/A
0N/A if (msecs < PASSWD_SLOW_MASTER_WARN_MSECS) {
0N/A module->fast_count++;
0N/A return;
0N/A }
0N/A module->slow_count++;
0N/A if (module->fast_count + module->slow_count <
0N/A PASSDB_SLOW_MASTER_WARN_COUNT_INTERVAL)
0N/A return;
0N/A
0N/A percentage = module->slow_count * 100 /
0N/A (module->slow_count + module->fast_count);
if (percentage < PASSDB_SLOW_MASTER_WARN_MIN_PERCENTAGE) {
/* start from beginning */
module->slow_count = module->fast_count = 0;
} else {
i_warning("passwd: %u%% of last %u lookups took over "
"%u milliseconds, "
"you may want to set blocking=yes for userdb",
percentage, PASSDB_SLOW_MASTER_WARN_COUNT_INTERVAL,
PASSWD_SLOW_MASTER_WARN_MSECS);
module->slow_warned = TRUE;
}
}
static void passwd_lookup(struct auth_request *auth_request,
userdb_callback_t *callback)
{
struct userdb_module *_module = auth_request->userdb->userdb;
struct passwd_userdb_module *module =
(struct passwd_userdb_module *)_module;
struct passwd pw;
struct timeval start_tv;
int ret;
auth_request_log_debug(auth_request, "passwd", "lookup");
if (gettimeofday(&start_tv, NULL) < 0)
start_tv.tv_sec = 0;
ret = i_getpwnam(auth_request->user, &pw);
if (start_tv.tv_sec != 0)
passwd_check_warnings(auth_request, module, &start_tv);
switch (ret) {
case -1:
auth_request_log_error(auth_request, "passwd",
"getpwnam() failed: %m");
callback(USERDB_RESULT_INTERNAL_FAILURE, auth_request);
return;
case 0:
auth_request_log_info(auth_request, "passwd", "unknown user");
callback(USERDB_RESULT_USER_UNKNOWN, auth_request);
return;
}
auth_request_set_field(auth_request, "user", pw.pw_name, NULL);
auth_request_init_userdb_reply(auth_request);
auth_request_set_userdb_field(auth_request, "system_groups_user",
pw.pw_name);
auth_request_set_userdb_field(auth_request, "uid", dec2str(pw.pw_uid));
auth_request_set_userdb_field(auth_request, "gid", dec2str(pw.pw_gid));
auth_request_set_userdb_field(auth_request, "home", pw.pw_dir);
userdb_template_export(module->tmpl, auth_request);
callback(USERDB_RESULT_OK, auth_request);
}
static struct userdb_iterate_context *
passwd_iterate_init(struct auth_request *auth_request,
userdb_iter_callback_t *callback, void *context)
{
struct passwd_userdb_iterate_context *ctx;
ctx = i_new(struct passwd_userdb_iterate_context, 1);
ctx->ctx.auth_request = auth_request;
ctx->ctx.callback = callback;
ctx->ctx.context = context;
setpwent();
if (cur_userdb_iter == NULL)
cur_userdb_iter = ctx;
return &ctx->ctx;
}
static void passwd_iterate_next(struct userdb_iterate_context *_ctx)
{
struct passwd_userdb_iterate_context *ctx =
(struct passwd_userdb_iterate_context *)_ctx;
const struct auth_settings *set = _ctx->auth_request->set;
struct passwd *pw;
if (cur_userdb_iter != NULL && cur_userdb_iter != ctx) {
/* we can't support concurrent userdb iteration.
wait until the previous one is done */
ctx->next_waiting = cur_userdb_iter->next_waiting;
cur_userdb_iter->next_waiting = ctx;
return;
}
errno = 0;
while ((pw = getpwent()) != NULL) {
/* skip entries not in valid UID range.
they're users for daemons and such. */
if (pw->pw_uid >= (uid_t)set->first_valid_uid &&
(set->last_valid_uid == 0 ||
pw->pw_uid <= (uid_t)set->last_valid_uid)) {
_ctx->callback(pw->pw_name, _ctx->context);
return;
}
}
if (errno != 0) {
i_error("getpwent() failed: %m");
_ctx->failed = TRUE;
}
_ctx->callback(NULL, _ctx->context);
}
static void passwd_iterate_next_timeout(void *context ATTR_UNUSED)
{
timeout_remove(&cur_userdb_iter_to);
passwd_iterate_next(&cur_userdb_iter->ctx);
}
static int passwd_iterate_deinit(struct userdb_iterate_context *_ctx)
{
struct passwd_userdb_iterate_context *ctx =
(struct passwd_userdb_iterate_context *)_ctx;
int ret = _ctx->failed ? -1 : 0;
cur_userdb_iter = ctx->next_waiting;
i_free(ctx);
if (cur_userdb_iter != NULL) {
cur_userdb_iter_to =
timeout_add(0, passwd_iterate_next_timeout, NULL);
}
return ret;
}
static struct userdb_module *
passwd_passwd_preinit(pool_t pool, const char *args)
{
struct passwd_userdb_module *module;
const char *value;
module = p_new(pool, struct passwd_userdb_module, 1);
module->module.cache_key = USER_CACHE_KEY;
module->tmpl = userdb_template_build(pool, "passwd", args);
if (userdb_template_remove(module->tmpl, "blocking", &value)) {
module->module.blocking = value == NULL ||
strcasecmp(value, "yes") == 0;
}
/* FIXME: backwards compatibility */
if (!userdb_template_is_empty(module->tmpl))
i_warning("userdb passwd: Move templates args to override_fields setting");
return &module->module;
}
struct userdb_module_interface userdb_passwd = {
"passwd",
passwd_passwd_preinit,
NULL,
NULL,
passwd_lookup,
passwd_iterate_init,
passwd_iterate_next,
passwd_iterate_deinit
};
#else
struct userdb_module_interface userdb_passwd = {
.name = "passwd"
};
#endif