userdb.c revision 98a711be68ba64e1cabf8cacc150af44421e2ac9
bcb4e51a409d94ae670de96afb8483a4f7855294Stephan Bosch/* Copyright (C) 2002-2003 Timo Sirainen */
02aedbc20af0160091670233383d228f10b168afTimo Sirainen
02aedbc20af0160091670233383d228f10b168afTimo Sirainen#include "common.h"
02aedbc20af0160091670233383d228f10b168afTimo Sirainen#include "auth-module.h"
75af6e5ca2c4685e2316d27364ac7b00def7fed7Timo Sirainen#include "userdb.h"
75af6e5ca2c4685e2316d27364ac7b00def7fed7Timo Sirainen
02aedbc20af0160091670233383d228f10b168afTimo Sirainen#include <stdlib.h>
02aedbc20af0160091670233383d228f10b168afTimo Sirainen
02aedbc20af0160091670233383d228f10b168afTimo Sirainen#ifdef HAVE_MODULES
02aedbc20af0160091670233383d228f10b168afTimo Sirainenstatic struct auth_module *userdb_module = NULL;
02aedbc20af0160091670233383d228f10b168afTimo Sirainen#endif
02aedbc20af0160091670233383d228f10b168afTimo Sirainen
02aedbc20af0160091670233383d228f10b168afTimo Sirainenstruct userdb_module *userdb;
02aedbc20af0160091670233383d228f10b168afTimo Sirainen
02aedbc20af0160091670233383d228f10b168afTimo Sirainenvoid userdb_init(void)
75af6e5ca2c4685e2316d27364ac7b00def7fed7Timo Sirainen{
02aedbc20af0160091670233383d228f10b168afTimo Sirainen const char *name, *args;
02aedbc20af0160091670233383d228f10b168afTimo Sirainen
76537b1991e7815c7a867a997f7fa2b3c17412d4Aki Tuomi userdb = NULL;
02aedbc20af0160091670233383d228f10b168afTimo Sirainen
02aedbc20af0160091670233383d228f10b168afTimo Sirainen name = getenv("USERDB");
02aedbc20af0160091670233383d228f10b168afTimo Sirainen if (name == NULL)
02aedbc20af0160091670233383d228f10b168afTimo Sirainen i_fatal("USERDB environment is unset");
02aedbc20af0160091670233383d228f10b168afTimo Sirainen
02aedbc20af0160091670233383d228f10b168afTimo Sirainen args = strchr(name, ' ');
02aedbc20af0160091670233383d228f10b168afTimo Sirainen name = t_strcut(name, ' ');
02aedbc20af0160091670233383d228f10b168afTimo Sirainen
02aedbc20af0160091670233383d228f10b168afTimo Sirainen#ifdef USERDB_PASSWD
02aedbc20af0160091670233383d228f10b168afTimo Sirainen if (strcasecmp(name, "passwd") == 0)
02aedbc20af0160091670233383d228f10b168afTimo Sirainen userdb = &userdb_passwd;
02aedbc20af0160091670233383d228f10b168afTimo Sirainen#endif
02aedbc20af0160091670233383d228f10b168afTimo Sirainen#ifdef USERDB_PASSWD_FILE
02aedbc20af0160091670233383d228f10b168afTimo Sirainen if (strcasecmp(name, "passwd-file") == 0)
02aedbc20af0160091670233383d228f10b168afTimo Sirainen userdb = &userdb_passwd_file;
02aedbc20af0160091670233383d228f10b168afTimo Sirainen#endif
02aedbc20af0160091670233383d228f10b168afTimo Sirainen#ifdef USERDB_STATIC
02aedbc20af0160091670233383d228f10b168afTimo Sirainen if (strcasecmp(name, "static") == 0)
02aedbc20af0160091670233383d228f10b168afTimo Sirainen userdb = &userdb_static;
75af6e5ca2c4685e2316d27364ac7b00def7fed7Timo Sirainen#endif
75af6e5ca2c4685e2316d27364ac7b00def7fed7Timo Sirainen#ifdef USERDB_VPOPMAIL
75af6e5ca2c4685e2316d27364ac7b00def7fed7Timo Sirainen if (strcasecmp(name, "vpopmail") == 0)
02aedbc20af0160091670233383d228f10b168afTimo Sirainen userdb = &userdb_vpopmail;
02aedbc20af0160091670233383d228f10b168afTimo Sirainen#endif
02aedbc20af0160091670233383d228f10b168afTimo Sirainen#ifdef USERDB_LDAP
02aedbc20af0160091670233383d228f10b168afTimo Sirainen if (strcasecmp(name, "ldap") == 0)
02aedbc20af0160091670233383d228f10b168afTimo Sirainen userdb = &userdb_ldap;
02aedbc20af0160091670233383d228f10b168afTimo Sirainen#endif
02aedbc20af0160091670233383d228f10b168afTimo Sirainen#ifdef USERDB_PGSQL
02aedbc20af0160091670233383d228f10b168afTimo Sirainen if (strcasecmp(name, "pgsql") == 0)
adad68df4602d80639d7f28e4c7550fbafbd8d60Timo Sirainen userdb = &userdb_pgsql;
adad68df4602d80639d7f28e4c7550fbafbd8d60Timo Sirainen#endif
adad68df4602d80639d7f28e4c7550fbafbd8d60Timo Sirainen#ifdef HAVE_MODULES
adad68df4602d80639d7f28e4c7550fbafbd8d60Timo Sirainen userdb_module = userdb != NULL ? NULL : auth_module_open(name);
adad68df4602d80639d7f28e4c7550fbafbd8d60Timo Sirainen if (userdb_module != NULL) {
adad68df4602d80639d7f28e4c7550fbafbd8d60Timo Sirainen userdb = auth_module_sym(userdb_module,
adad68df4602d80639d7f28e4c7550fbafbd8d60Timo Sirainen t_strconcat("userdb_", name, NULL));
adad68df4602d80639d7f28e4c7550fbafbd8d60Timo Sirainen }
adad68df4602d80639d7f28e4c7550fbafbd8d60Timo Sirainen#endif
adad68df4602d80639d7f28e4c7550fbafbd8d60Timo Sirainen
adad68df4602d80639d7f28e4c7550fbafbd8d60Timo Sirainen if (userdb == NULL)
adad68df4602d80639d7f28e4c7550fbafbd8d60Timo Sirainen i_fatal("Unknown userdb type '%s'", name);
adad68df4602d80639d7f28e4c7550fbafbd8d60Timo Sirainen
adad68df4602d80639d7f28e4c7550fbafbd8d60Timo Sirainen /* initialize */
adad68df4602d80639d7f28e4c7550fbafbd8d60Timo Sirainen if (userdb->init != NULL)
adad68df4602d80639d7f28e4c7550fbafbd8d60Timo Sirainen userdb->init(args != NULL ? args+1 : "");
adad68df4602d80639d7f28e4c7550fbafbd8d60Timo Sirainen}
adad68df4602d80639d7f28e4c7550fbafbd8d60Timo Sirainen
adad68df4602d80639d7f28e4c7550fbafbd8d60Timo Sirainenvoid userdb_deinit(void)
adad68df4602d80639d7f28e4c7550fbafbd8d60Timo Sirainen{
adad68df4602d80639d7f28e4c7550fbafbd8d60Timo Sirainen if (userdb != NULL && userdb->deinit != NULL)
adad68df4602d80639d7f28e4c7550fbafbd8d60Timo Sirainen userdb->deinit();
adad68df4602d80639d7f28e4c7550fbafbd8d60Timo Sirainen#ifdef HAVE_MODULES
adad68df4602d80639d7f28e4c7550fbafbd8d60Timo Sirainen if (userdb_module != NULL)
adad68df4602d80639d7f28e4c7550fbafbd8d60Timo Sirainen auth_module_close(userdb_module);
adad68df4602d80639d7f28e4c7550fbafbd8d60Timo Sirainen#endif
adad68df4602d80639d7f28e4c7550fbafbd8d60Timo Sirainen}
adad68df4602d80639d7f28e4c7550fbafbd8d60Timo Sirainen