changeset 4034:b533c52196e3 HEAD

Updated passwd-file format to allow specifying any key=value fields. Since the value fields may contain ':' characters, this changes the file format a bit. Previously the last two fields were "flags" and "mail". Flags has been empty for last 3 years however, so the new format is used if the flags field contains something. In that case there is no separate mail field, it can instead be specified by adding userdb_mail=<mail> parameter.
author Timo Sirainen <timo.sirainen@movial.fi>
date Mon, 20 Feb 2006 16:16:00 +0200
parents 7dbfff239206
children 335ac59efffd
files src/auth/db-passwd-file.c src/auth/db-passwd-file.h src/auth/passdb-passwd-file.c src/auth/userdb-passwd-file.c
diffstat 4 files changed, 72 insertions(+), 26 deletions(-) [+]
line wrap: on
line diff
--- a/src/auth/db-passwd-file.c	Mon Feb 20 16:12:36 2006 +0200
+++ b/src/auth/db-passwd-file.c	Mon Feb 20 16:16:00 2006 +0200
@@ -23,9 +23,9 @@
 static void passwd_file_add(struct passwd_file *pw, const char *username,
 			    const char *pass, const char *const *args)
 {
-	/* args = uid, gid, user info, home dir, shell, flags, mail */
+	/* args = uid, gid, user info, home dir, shell, extra_fields */
 	struct passwd_user *pu;
-	const char *p;
+	const char *p, *extra_fields = NULL;
 
 	if (hash_lookup(pw->users, username) != NULL) {
 		i_error("passwd-file %s: User %s exists more than once",
@@ -100,25 +100,25 @@
 	if (*args != NULL)
 		args++;
 
-	/* flags */
-	if (*args != NULL) {
-		/* no flags currently */
+	if (*args != NULL && **args == '\0') {
+		/* old format, this field is empty and next field may
+		   contain MAIL */
 		args++;
-	}
+		if (*args != NULL && **args != '\0') {
+			extra_fields =
+                                t_strconcat("userdb_mail=",
+                                            t_strarray_join(args, ":"), NULL);
+		}
+	} else if (*args != NULL) {
+		/* new format, contains a space separated list of
+		   extra fields */
+                extra_fields = t_strarray_join(args, ":");
+        }
 
-	/* rest is MAIL environment */
-	if (*args != NULL) {
-		string_t *str = t_str_new(100);
-		str_append(str, *args);
-		args++;
-
-		while (*args != NULL) {
-			str_append_c(str, ':');
-			str_append(str, *args);
-			args++;
-		}
-		pu->mail = p_strdup_empty(pw->pool, str_c(str));
-	}
+        if (extra_fields != NULL) {
+                pu->extra_fields =
+                        p_strsplit_spaces(pw->pool, extra_fields, " ");
+        }
 
 	hash_insert(pw->users, pu->user_realm, pu);
 }
--- a/src/auth/db-passwd-file.h	Mon Feb 20 16:12:36 2006 +0200
+++ b/src/auth/db-passwd-file.h	Mon Feb 20 16:16:00 2006 +0200
@@ -9,9 +9,8 @@
 	gid_t gid;
 
 	char *home;
-	char *mail;
-
-	char *password;
+        char *password;
+        char **extra_fields;
 };
 
 struct passwd_file {
--- a/src/auth/passdb-passwd-file.c	Mon Feb 20 16:12:36 2006 +0200
+++ b/src/auth/passdb-passwd-file.c	Mon Feb 20 16:16:00 2006 +0200
@@ -4,6 +4,8 @@
 
 #ifdef PASSDB_PASSWD_FILE
 
+#include "str.h"
+#include "var-expand.h"
 #include "passdb.h"
 #include "password-scheme.h"
 #include "db-passwd-file.h"
@@ -26,8 +28,11 @@
 	struct passwd_file_passdb_module *module =
 		(struct passwd_file_passdb_module *)_module;
 	struct passwd_user *pu;
-	const char *scheme, *crypted_pass;
-	int ret;
+        const struct var_expand_table *table;
+	const char *scheme, *crypted_pass, *key, *value;
+	string_t *str;
+	char **p;
+        int ret;
 
 	pu = db_passwd_file_lookup(module->pwf, request);
 	if (pu == NULL) {
@@ -43,7 +48,26 @@
 	if (crypted_pass != NULL) {
 		auth_request_set_field(request, "password",
 				       crypted_pass, scheme);
+        }
+
+	t_push();
+	str = t_str_new(512);
+	table = auth_request_get_var_expand_table(request, NULL);
+
+	for (p = pu->extra_fields; *p != NULL; p++) {
+		value = strchr(*p, '=');
+		if (value != NULL) {
+			key = t_strdup_until(*p, value);
+			str_truncate(str, 0);
+			var_expand(str, value + 1, table);
+			value = str_c(str);
+		} else {
+			key = *p;
+			value = "";
+		}
+		auth_request_set_field(request, key, value, NULL);
 	}
+	t_pop();
 
 	ret = auth_request_password_verify(request, password, crypted_pass,
 					   scheme, "passwd-file");
--- a/src/auth/userdb-passwd-file.c	Mon Feb 20 16:12:36 2006 +0200
+++ b/src/auth/userdb-passwd-file.c	Mon Feb 20 16:16:00 2006 +0200
@@ -5,6 +5,7 @@
 #ifdef USERDB_PASSWD_FILE
 
 #include "str.h"
+#include "var-expand.h"
 #include "userdb.h"
 #include "db-passwd-file.h"
 
@@ -23,6 +24,10 @@
 		(struct passwd_file_userdb_module *)_module;
 	struct auth_stream_reply *reply;
 	struct passwd_user *pu;
+        const struct var_expand_table *table;
+	string_t *str;
+	const char *key, *value;
+	char **p;
 
 	pu = db_passwd_file_lookup(module->pwf, auth_request);
 	if (pu == NULL) {
@@ -37,8 +42,26 @@
 
 	if (pu->home != NULL)
 		auth_stream_reply_add(reply, "home", pu->home);
-	if (pu->mail != NULL)
-		auth_stream_reply_add(reply, "mail", pu->mail);
+
+	t_push();
+	str = t_str_new(512);
+	table = auth_request_get_var_expand_table(auth_request, NULL);
+
+	for (p = pu->extra_fields; *p != NULL; p++) {
+		if (strncmp(*p, "userdb_", 7) != 0)
+			continue;
+
+		key = *p + 7;
+		value = strchr(key, '=');
+		if (value != NULL) {
+			key = t_strdup_until(key, value);
+			str_truncate(str, 0);
+			var_expand(str, value + 1, table);
+			value = str_c(str);
+		}
+		auth_stream_reply_add(reply, key, value);
+	}
+	t_pop();
 
 	callback(reply, auth_request);
 }