dovecot: Added username_format parameter for passwd-file passdb ...

dovecot at dovecot.org dovecot at dovecot.org
Sat Sep 15 15:58:31 EEST 2007


details:   http://hg.dovecot.org/dovecot/rev/9e74c008484a
changeset: 6394:9e74c008484a
user:      Timo Sirainen <tss at iki.fi>
date:      Sat Sep 15 15:58:27 2007 +0300
description:
Added username_format parameter for passwd-file passdb and userdb.

diffstat:

5 files changed, 73 insertions(+), 40 deletions(-)
dovecot-example.conf          |    5 ++--
src/auth/db-passwd-file.c     |   30 ++++++++++++------------
src/auth/db-passwd-file.h     |    8 ++++--
src/auth/passdb-passwd-file.c |   51 +++++++++++++++++++++++++----------------
src/auth/userdb-passwd-file.c |   19 +++++++++++++--

diffs (233 lines):

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 @@ auth default {
   # passwd-like file with specified location
   # <doc/wiki/AuthDatabase.PasswdFile.txt>
   #passdb passwd-file {
-    # [scheme=<default password scheme>] <Path for passwd-file>
+    # [scheme=<default password scheme>] [username_format=<format>]
+    # <Path for passwd-file>
     #args = 
   #}
 
@@ -916,7 +917,7 @@ auth default {
   # passwd-like file with specified location
   # <doc/wiki/AuthDatabase.PasswdFile.txt>
   #userdb passwd-file {
-    # Path for passwd-file
+    # [username_format=<format>] <Path for passwd-file>
     #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 @@ static struct db_passwd_file *db_passwd_
 }
 
 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_passwd_file_init(const char *path, bo
 	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 @@ db_passwd_file_lookup(struct db_passwd_f
 {
 	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 @@ db_passwd_file_lookup(struct db_passwd_f
 		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);
-
-	pu = hash_lookup(pw->users, username);
+			       "lookup: user=%s file=%s",
+			       str_c(username), pw->path);
+
+	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,5 +1,8 @@
 #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;
@@ -30,8 +33,8 @@ struct db_passwd_file {
 	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_f
 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
@@ -10,9 +10,6 @@
 #include "passdb.h"
 #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 *
 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;
 
-	if (strncmp(args, "scheme=", 7) == 0) {
-		scheme = args + 7;
-		p = strchr(scheme, ' ');
-		if (p == NULL)
+	while (*args != '\0') {
+		if (*args == '/')
+			break;
+
+		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 *
 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;


More information about the dovecot-cvs mailing list