userdb-passwd.c revision 00bde9ae9eab9e720462bf6ec9a4dd85e88c3bbf
b9f30617c2c96d54acbc4f85ed17b939c4f28916Timo Sirainen/* Copyright (C) 2002-2003 Timo Sirainen */
a64adf62fa33f2463a86f990217b0c9078531a40Timo Sirainen
a64adf62fa33f2463a86f990217b0c9078531a40Timo Sirainen#include "common.h"
a64adf62fa33f2463a86f990217b0c9078531a40Timo Sirainen
a64adf62fa33f2463a86f990217b0c9078531a40Timo Sirainen#ifdef USERDB_PASSWD
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainen
9f0f2de10e4ea0c99052bf4b2bef8179f2536228Timo Sirainen#include "userdb.h"
a64adf62fa33f2463a86f990217b0c9078531a40Timo Sirainen
a64adf62fa33f2463a86f990217b0c9078531a40Timo Sirainen#include <pwd.h>
a64adf62fa33f2463a86f990217b0c9078531a40Timo Sirainen
a64adf62fa33f2463a86f990217b0c9078531a40Timo Sirainen#define USER_CACHE_KEY "%u"
7744586e3e0fd60158abfbb03a233d3bd8d6c48bTimo Sirainen
7744586e3e0fd60158abfbb03a233d3bd8d6c48bTimo Sirainenstatic void passwd_lookup(struct auth_request *auth_request,
a64adf62fa33f2463a86f990217b0c9078531a40Timo Sirainen userdb_callback_t *callback)
d477acb83e14a776ece4ca94dcd1869e75d0c6eeTimo Sirainen{
d477acb83e14a776ece4ca94dcd1869e75d0c6eeTimo Sirainen struct passwd *pw;
9f0f2de10e4ea0c99052bf4b2bef8179f2536228Timo Sirainen struct auth_stream_reply *reply;
9f0f2de10e4ea0c99052bf4b2bef8179f2536228Timo Sirainen
9f0f2de10e4ea0c99052bf4b2bef8179f2536228Timo Sirainen auth_request_log_debug(auth_request, "passwd", "lookup");
9f0f2de10e4ea0c99052bf4b2bef8179f2536228Timo Sirainen
9f0f2de10e4ea0c99052bf4b2bef8179f2536228Timo Sirainen pw = getpwnam(auth_request->user);
9f0f2de10e4ea0c99052bf4b2bef8179f2536228Timo Sirainen if (pw == NULL) {
9f0f2de10e4ea0c99052bf4b2bef8179f2536228Timo Sirainen auth_request_log_info(auth_request, "passwd", "unknown user");
9f0f2de10e4ea0c99052bf4b2bef8179f2536228Timo Sirainen callback(USERDB_RESULT_USER_UNKNOWN, NULL, auth_request);
9f0f2de10e4ea0c99052bf4b2bef8179f2536228Timo Sirainen return;
9f0f2de10e4ea0c99052bf4b2bef8179f2536228Timo Sirainen }
9f0f2de10e4ea0c99052bf4b2bef8179f2536228Timo Sirainen
9f0f2de10e4ea0c99052bf4b2bef8179f2536228Timo Sirainen if (strcasecmp(pw->pw_name, auth_request->user) != 0) {
9f0f2de10e4ea0c99052bf4b2bef8179f2536228Timo Sirainen /* try to catch broken NSS implementations (nss_ldap) */
9f0f2de10e4ea0c99052bf4b2bef8179f2536228Timo Sirainen i_fatal("BROKEN NSS IMPLEMENTATION: "
9f0f2de10e4ea0c99052bf4b2bef8179f2536228Timo Sirainen "getpwnam() lookup returned different user than was "
9f0f2de10e4ea0c99052bf4b2bef8179f2536228Timo Sirainen "requested (%s != %s).",
9f0f2de10e4ea0c99052bf4b2bef8179f2536228Timo Sirainen pw->pw_name, auth_request->user);
9f0f2de10e4ea0c99052bf4b2bef8179f2536228Timo Sirainen }
9f0f2de10e4ea0c99052bf4b2bef8179f2536228Timo Sirainen
9f0f2de10e4ea0c99052bf4b2bef8179f2536228Timo Sirainen reply = auth_stream_reply_init(auth_request);
9f0f2de10e4ea0c99052bf4b2bef8179f2536228Timo Sirainen auth_stream_reply_add(reply, NULL, pw->pw_name);
9f0f2de10e4ea0c99052bf4b2bef8179f2536228Timo Sirainen auth_stream_reply_add(reply, "system_user", pw->pw_name);
9f0f2de10e4ea0c99052bf4b2bef8179f2536228Timo Sirainen auth_stream_reply_add(reply, "uid", dec2str(pw->pw_uid));
9f0f2de10e4ea0c99052bf4b2bef8179f2536228Timo Sirainen auth_stream_reply_add(reply, "gid", dec2str(pw->pw_gid));
9f0f2de10e4ea0c99052bf4b2bef8179f2536228Timo Sirainen auth_stream_reply_add(reply, "home", pw->pw_dir);
9f0f2de10e4ea0c99052bf4b2bef8179f2536228Timo Sirainen
9f0f2de10e4ea0c99052bf4b2bef8179f2536228Timo Sirainen callback(USERDB_RESULT_OK, reply, auth_request);
9f0f2de10e4ea0c99052bf4b2bef8179f2536228Timo Sirainen}
9f0f2de10e4ea0c99052bf4b2bef8179f2536228Timo Sirainen
9f0f2de10e4ea0c99052bf4b2bef8179f2536228Timo Sirainenstatic void passwd_passwd_init(struct userdb_module *module,
9f0f2de10e4ea0c99052bf4b2bef8179f2536228Timo Sirainen const char *args)
9f0f2de10e4ea0c99052bf4b2bef8179f2536228Timo Sirainen{
9f0f2de10e4ea0c99052bf4b2bef8179f2536228Timo Sirainen if (strcmp(args, "blocking=yes") == 0)
9f0f2de10e4ea0c99052bf4b2bef8179f2536228Timo Sirainen module->blocking = TRUE;
9f0f2de10e4ea0c99052bf4b2bef8179f2536228Timo Sirainen module->cache_key = USER_CACHE_KEY;
9f0f2de10e4ea0c99052bf4b2bef8179f2536228Timo Sirainen}
9f0f2de10e4ea0c99052bf4b2bef8179f2536228Timo Sirainen
9f0f2de10e4ea0c99052bf4b2bef8179f2536228Timo Sirainenstruct userdb_module_interface userdb_passwd = {
9f0f2de10e4ea0c99052bf4b2bef8179f2536228Timo Sirainen "passwd",
9f0f2de10e4ea0c99052bf4b2bef8179f2536228Timo Sirainen
9f0f2de10e4ea0c99052bf4b2bef8179f2536228Timo Sirainen NULL,
9f0f2de10e4ea0c99052bf4b2bef8179f2536228Timo Sirainen passwd_passwd_init,
9f0f2de10e4ea0c99052bf4b2bef8179f2536228Timo Sirainen NULL,
9f0f2de10e4ea0c99052bf4b2bef8179f2536228Timo Sirainen
9f0f2de10e4ea0c99052bf4b2bef8179f2536228Timo Sirainen passwd_lookup
9f0f2de10e4ea0c99052bf4b2bef8179f2536228Timo Sirainen};
9f0f2de10e4ea0c99052bf4b2bef8179f2536228Timo Sirainen
9f0f2de10e4ea0c99052bf4b2bef8179f2536228Timo Sirainen#endif
9f0f2de10e4ea0c99052bf4b2bef8179f2536228Timo Sirainen