# HG changeset patch # User Timo Sirainen # Date 1189861107 -10800 # Node ID 9e74c008484a390630a6528e4aa44fcb95d0d648 # Parent 777fede4d899aaa045ebb62fae5b984b35b9eb63 Added username_format parameter for passwd-file passdb and userdb. diff -r 777fede4d899 -r 9e74c008484a dovecot-example.conf --- a/dovecot-example.conf Sat Sep 15 15:54:43 2007 +0300 +++ b/dovecot-example.conf Sat Sep 15 15:58:27 2007 +0300 @@ -862,7 +862,8 @@ # passwd-like file with specified location # #passdb passwd-file { - # [scheme=] + # [scheme=] [username_format=] + # #args = #} @@ -916,7 +917,7 @@ # passwd-like file with specified location # #userdb passwd-file { - # Path for passwd-file + # [username_format=] #args = #} diff -r 777fede4d899 -r 9e74c008484a src/auth/db-passwd-file.c --- a/src/auth/db-passwd-file.c Sat Sep 15 15:54:43 2007 +0300 +++ b/src/auth/db-passwd-file.c Sat Sep 15 15:58:27 2007 +0300 @@ -271,7 +271,8 @@ } struct db_passwd_file * -db_passwd_file_init(const char *path, bool userdb, bool debug) +db_passwd_file_init(const char *path, const char *username_format, + bool userdb, bool debug) { struct db_passwd_file *db; const char *p; @@ -288,17 +289,11 @@ db->refcount = 1; db->userdb = userdb; db->debug = debug; + db->username_format = username_format; for (p = path; *p != '\0'; p++) { if (*p == '%' && p[1] != '\0') { - p++; - if (*p == 'd') { - /* drop domains out only if %d is given - without modifiers */ - db->domain_var = TRUE; - } - - if (var_get_key(p) == '%') + if (var_get_key(++p) == '%') percents = TRUE; else db->vars = TRUE; @@ -394,7 +389,9 @@ { struct passwd_file *pw; struct passwd_user *pu; - const char *username, *path; + const struct var_expand_table *table; + string_t *username; + const char *path; if (!db->vars) pw = db->default_file; @@ -427,13 +424,16 @@ return NULL; } - username = !db->domain_var ? request->user : - t_strcut(request->user, '@'); + username = t_str_new(256); + table = auth_request_get_var_expand_table(request, + auth_request_str_escape); + var_expand(username, db->username_format, table); auth_request_log_debug(request, "passwd-file", - "lookup: user=%s file=%s", username, pw->path); + "lookup: user=%s file=%s", + str_c(username), pw->path); - pu = hash_lookup(pw->users, username); + pu = hash_lookup(pw->users, str_c(username)); if (pu == NULL) auth_request_log_info(request, "passwd-file", "unknown user"); t_pop(); diff -r 777fede4d899 -r 9e74c008484a src/auth/db-passwd-file.h --- a/src/auth/db-passwd-file.h Sat Sep 15 15:54:43 2007 +0300 +++ b/src/auth/db-passwd-file.h Sat Sep 15 15:58:27 2007 +0300 @@ -1,6 +1,9 @@ #ifndef __DB_PASSWD_FILE_H #define __DB_PASSWD_FILE_H +#define PASSWD_FILE_DEFAULT_USERNAME_FORMAT "%u" +#define PASSWD_FILE_DEFAULT_SCHEME "CRYPT" + struct passwd_user { uid_t uid; gid_t gid; @@ -30,8 +33,8 @@ char *path; struct hash_table *files; struct passwd_file *default_file; + const char *username_format; - unsigned int domain_var:1; unsigned int vars:1; unsigned int userdb:1; unsigned int debug:1; @@ -41,7 +44,8 @@ db_passwd_file_lookup(struct db_passwd_file *db, struct auth_request *request); struct db_passwd_file * -db_passwd_file_init(const char *path, bool userdb, bool debug); +db_passwd_file_init(const char *path, const char *username_format, + bool userdb, bool debug); void db_passwd_file_parse(struct db_passwd_file *db); void db_passwd_file_unref(struct db_passwd_file **db); diff -r 777fede4d899 -r 9e74c008484a src/auth/passdb-passwd-file.c --- a/src/auth/passdb-passwd-file.c Sat Sep 15 15:54:43 2007 +0300 +++ b/src/auth/passdb-passwd-file.c Sat Sep 15 15:58:27 2007 +0300 @@ -11,9 +11,6 @@ #include "password-scheme.h" #include "db-passwd-file.h" -#define PASSWD_FILE_CACHE_KEY "%u" -#define PASSWD_FILE_DEFAULT_SCHEME "CRYPT" - struct passwd_file_passdb_module { struct passdb_module module; @@ -116,34 +113,50 @@ passwd_file_preinit(struct auth_passdb *auth_passdb, const char *args) { struct passwd_file_passdb_module *module; - const char *p, *scheme = PASSWD_FILE_DEFAULT_SCHEME; + const char *scheme = PASSWD_FILE_DEFAULT_SCHEME; + const char *format = PASSWD_FILE_DEFAULT_USERNAME_FORMAT; + const char *key, *value; + + while (*args != '\0') { + if (*args == '/') + break; - if (strncmp(args, "scheme=", 7) == 0) { - scheme = args + 7; - p = strchr(scheme, ' '); - if (p == NULL) + t_push(); + key = args; + value = strchr(key, '='); + if (value == NULL) { + value = ""; + args = strchr(key, ' '); + } else { + key = t_strdup_until(key, value); + args = strchr(++value, ' '); + if (args != NULL) + value = t_strdup_until(value, args); + } + if (args == NULL) args = ""; - else { - scheme = p_strdup_until(auth_passdb->auth->pool, - scheme, p); - args = p + 1; - } + else + args++; + + if (strcmp(key, "scheme") == 0) + scheme = p_strdup(auth_passdb->auth->pool, value); + else if (strcmp(key, "username_format") == 0) + format = p_strdup(auth_passdb->auth->pool, value); + t_pop(); } module = p_new(auth_passdb->auth->pool, struct passwd_file_passdb_module, 1); module->auth = auth_passdb->auth; - module->pwf = - db_passwd_file_init(args, FALSE, module->auth->verbose_debug); + module->pwf = db_passwd_file_init(args, format, FALSE, + module->auth->verbose_debug); if (!module->pwf->vars) - module->module.cache_key = PASSWD_FILE_CACHE_KEY; + module->module.cache_key = format; else { module->module.cache_key = auth_cache_parse_key(auth_passdb->auth->pool, - t_strconcat(PASSWD_FILE_CACHE_KEY, - module->pwf->path, - NULL)); + t_strconcat(format, module->pwf->path, NULL)); } module->module.default_pass_scheme = scheme; diff -r 777fede4d899 -r 9e74c008484a src/auth/userdb-passwd-file.c --- a/src/auth/userdb-passwd-file.c Sat Sep 15 15:54:43 2007 +0300 +++ b/src/auth/userdb-passwd-file.c Sat Sep 15 15:58:27 2007 +0300 @@ -79,12 +79,27 @@ passwd_file_preinit(struct auth_userdb *auth_userdb, const char *args) { struct passwd_file_userdb_module *module; + const char *format = PASSWD_FILE_DEFAULT_USERNAME_FORMAT; + const char *p; + + if (strncmp(args, "username_format=", 16) == 0) { + args += 16; + p = strchr(args, ' '); + if (p == NULL) { + format = args; + args = ""; + } else { + format = p_strdup_until(auth_userdb->auth->pool, + args, p); + args = p + 1; + } + } module = p_new(auth_userdb->auth->pool, struct passwd_file_userdb_module, 1); module->auth = auth_userdb->auth; - module->pwf = - db_passwd_file_init(args, TRUE, module->auth->verbose_debug); + module->pwf = db_passwd_file_init(args, format, TRUE, + module->auth->verbose_debug); if (!module->pwf->vars) module->module.cache_key = PASSWD_FILE_CACHE_KEY;