From: Timo Sirainen Date: Mon, 31 Oct 2016 18:26:02 +0000 (+0200) Subject: auth: Code cleanup - Move passwd-file extra fields import to its own function. X-Git-Tag: 2.3.0.rc1~2703 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=d5e68154e67819140e513cbd037eda9c1b929c4e;p=thirdparty%2Fdovecot%2Fcore.git auth: Code cleanup - Move passwd-file extra fields import to its own function. This will shrink the diff output for the following var_expand() change. --- diff --git a/src/auth/passdb-passwd-file.c b/src/auth/passdb-passwd-file.c index aaceab9640..8978fd2baa 100644 --- a/src/auth/passdb-passwd-file.c +++ b/src/auth/passdb-passwd-file.c @@ -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 diff --git a/src/auth/userdb-passwd-file.c b/src/auth/userdb-passwd-file.c index 21612eabc7..f2e536c884 100644 --- a/src/auth/userdb-passwd-file.c +++ b/src/auth/userdb-passwd-file.c @@ -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); }