]> git.ipfire.org Git - thirdparty/dovecot/core.git/commitdiff
auth: Code cleanup - Move passwd-file extra fields import to its own function.
authorTimo Sirainen <timo.sirainen@dovecot.fi>
Mon, 31 Oct 2016 18:26:02 +0000 (20:26 +0200)
committerTimo Sirainen <timo.sirainen@dovecot.fi>
Wed, 2 Nov 2016 12:05:41 +0000 (14:05 +0200)
This will shrink the diff output for the following var_expand() change.

src/auth/passdb-passwd-file.c
src/auth/userdb-passwd-file.c

index aaceab96404b45834e3bf658187f7cc2d30dd73a..8978fd2baa71d1130b8f4d31a40231178b1f4be9 100644 (file)
@@ -17,16 +17,37 @@ struct passwd_file_passdb_module {
        const char *username_format;
 };
 
+static void
+passwd_file_add_extra_fields(struct auth_request *request, char *const *fields)
+{
+       string_t *str = t_str_new(512);
+        const struct var_expand_table *table;
+       const char *key, *value;
+       unsigned int i;
+
+       table = auth_request_get_var_expand_table(request, NULL);
+
+       for (i = 0; fields[i] != NULL; i++) {
+               value = strchr(fields[i], '=');
+               if (value != NULL) {
+                       key = t_strdup_until(fields[i], value);
+                       str_truncate(str, 0);
+                       auth_request_var_expand_with_table(str, value + 1,
+                                                          request, table, NULL);
+                       value = str_c(str);
+               } else {
+                       key = fields[i];
+                       value = "";
+               }
+               auth_request_set_field(request, key, value, NULL);
+       }
+}
+
 static void passwd_file_save_results(struct auth_request *request,
                                     const struct passwd_user *pu,
                                     const char **crypted_pass_r,
                                     const char **scheme_r)
 {
-        const struct var_expand_table *table;
-       const char *key, *value;
-       string_t *str;
-       char **p;
-
        *crypted_pass_r = pu->password != NULL ? pu->password : "";
        *scheme_r = password_get_scheme(crypted_pass_r);
        if (*scheme_r == NULL)
@@ -36,25 +57,8 @@ static void passwd_file_save_results(struct auth_request *request,
        auth_request_set_field(request, "password",
                               *crypted_pass_r, *scheme_r);
 
-       if (pu->extra_fields != NULL) {
-               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);
-                               auth_request_var_expand_with_table(str, value + 1,
-                                       request, table, NULL);
-                               value = str_c(str);
-                       } else {
-                               key = *p;
-                               value = "";
-                       }
-                       auth_request_set_field(request, key, value, NULL);
-               }
-       }
+       if (pu->extra_fields != NULL)
+               passwd_file_add_extra_fields(request, pu->extra_fields);
 }
 
 static void
index 21612eabc7ca356eefd1a3f65767b2337f637dc4..f2e536c884e5fc119f18667f4d7bf434c01de633 100644 (file)
@@ -27,6 +27,35 @@ struct passwd_file_userdb_module {
        const char *username_format;
 };
 
+static void
+passwd_file_add_extra_fields(struct auth_request *request, char *const *fields)
+{
+       string_t *str = t_str_new(512);
+        const struct var_expand_table *table;
+       const char *key, *value;
+       unsigned int i;
+
+       table = auth_request_get_var_expand_table(request, NULL);
+
+       for (i = 0; fields[i] != NULL; i++) {
+               if (strncmp(fields[i], "userdb_", 7) != 0)
+                       continue;
+
+               key = fields[i] + 7;
+               value = strchr(key, '=');
+               if (value != NULL) {
+                       key = t_strdup_until(key, value);
+                       str_truncate(str, 0);
+                       auth_request_var_expand_with_table(str, value + 1,
+                                                          request, table, NULL);
+                       value = str_c(str);
+               } else {
+                       value = "";
+               }
+               auth_request_set_userdb_field(request, key, value);
+       }
+}
+
 static void passwd_file_lookup(struct auth_request *auth_request,
                               userdb_callback_t *callback)
 {
@@ -34,10 +63,6 @@ static void passwd_file_lookup(struct auth_request *auth_request,
        struct passwd_file_userdb_module *module =
                (struct passwd_file_userdb_module *)_module;
        struct passwd_user *pu;
-        const struct var_expand_table *table;
-       string_t *str;
-       const char *key, *value;
-       char **p;
        int ret;
 
        ret = db_passwd_file_lookup(module->pwf, auth_request,
@@ -59,28 +84,8 @@ static void passwd_file_lookup(struct auth_request *auth_request,
        if (pu->home != NULL)
                auth_request_set_userdb_field(auth_request, "home", pu->home);
 
-       if (pu->extra_fields != NULL) {
-               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);
-                               auth_request_var_expand_with_table(str, value + 1,
-                                       auth_request, table, NULL);
-                               value = str_c(str);
-                       } else {
-                               value = "";
-                       }
-                       auth_request_set_userdb_field(auth_request, key, value);
-               }
-       }
+       if (pu->extra_fields != NULL)
+               passwd_file_add_extra_fields(auth_request, pu->extra_fields);
 
        callback(USERDB_RESULT_OK, auth_request);
 }