passdb-template.c revision 0f5dc4da3982053036be65190e44bf28a67b1ca2
02c335c23bf5fa225a467c19f2c063fb0dc7b8c3Timo Sirainen/* Copyright (c) 2003-2016 Dovecot authors, see the included COPYING file */
65a1c01472ebf88b9f5bbd6636827902936d5497Timo Sirainen
65a1c01472ebf88b9f5bbd6636827902936d5497Timo Sirainen#include "auth-common.h"
65a1c01472ebf88b9f5bbd6636827902936d5497Timo Sirainen#include "array.h"
65a1c01472ebf88b9f5bbd6636827902936d5497Timo Sirainen#include "str.h"
65a1c01472ebf88b9f5bbd6636827902936d5497Timo Sirainen#include "passdb.h"
65a1c01472ebf88b9f5bbd6636827902936d5497Timo Sirainen#include "passdb-template.h"
65a1c01472ebf88b9f5bbd6636827902936d5497Timo Sirainen
65a1c01472ebf88b9f5bbd6636827902936d5497Timo Sirainenstruct passdb_template {
4ee00532a265bdfb38539d811fcd12d51210ac35Timo Sirainen ARRAY(const char *) args;
65a1c01472ebf88b9f5bbd6636827902936d5497Timo Sirainen};
65a1c01472ebf88b9f5bbd6636827902936d5497Timo Sirainen
65a1c01472ebf88b9f5bbd6636827902936d5497Timo Sirainenstruct passdb_template *passdb_template_build(pool_t pool, const char *args)
65a1c01472ebf88b9f5bbd6636827902936d5497Timo Sirainen{
65a1c01472ebf88b9f5bbd6636827902936d5497Timo Sirainen struct passdb_template *tmpl;
65a1c01472ebf88b9f5bbd6636827902936d5497Timo Sirainen const char *const *tmp, *key, *value;
65a1c01472ebf88b9f5bbd6636827902936d5497Timo Sirainen
65a1c01472ebf88b9f5bbd6636827902936d5497Timo Sirainen tmpl = p_new(pool, struct passdb_template, 1);
65a1c01472ebf88b9f5bbd6636827902936d5497Timo Sirainen
65a1c01472ebf88b9f5bbd6636827902936d5497Timo Sirainen tmp = t_strsplit_spaces(args, " ");
65a1c01472ebf88b9f5bbd6636827902936d5497Timo Sirainen p_array_init(&tmpl->args, pool, str_array_length(tmp));
65a1c01472ebf88b9f5bbd6636827902936d5497Timo Sirainen
65a1c01472ebf88b9f5bbd6636827902936d5497Timo Sirainen for (; *tmp != NULL; tmp++) {
65a1c01472ebf88b9f5bbd6636827902936d5497Timo Sirainen value = strchr(*tmp, '=');
65a1c01472ebf88b9f5bbd6636827902936d5497Timo Sirainen if (value == NULL)
65a1c01472ebf88b9f5bbd6636827902936d5497Timo Sirainen key = *tmp;
65a1c01472ebf88b9f5bbd6636827902936d5497Timo Sirainen else
65a1c01472ebf88b9f5bbd6636827902936d5497Timo Sirainen key = t_strdup_until(*tmp, value++);
65a1c01472ebf88b9f5bbd6636827902936d5497Timo Sirainen
65a1c01472ebf88b9f5bbd6636827902936d5497Timo Sirainen key = p_strdup(pool, key);
65a1c01472ebf88b9f5bbd6636827902936d5497Timo Sirainen value = p_strdup(pool, value);
65a1c01472ebf88b9f5bbd6636827902936d5497Timo Sirainen array_append(&tmpl->args, &key, 1);
65a1c01472ebf88b9f5bbd6636827902936d5497Timo Sirainen array_append(&tmpl->args, &value, 1);
65a1c01472ebf88b9f5bbd6636827902936d5497Timo Sirainen }
65a1c01472ebf88b9f5bbd6636827902936d5497Timo Sirainen return tmpl;
65a1c01472ebf88b9f5bbd6636827902936d5497Timo Sirainen}
65a1c01472ebf88b9f5bbd6636827902936d5497Timo Sirainen
0f5dc4da3982053036be65190e44bf28a67b1ca2Timo Sirainenint passdb_template_export(struct passdb_template *tmpl,
0f5dc4da3982053036be65190e44bf28a67b1ca2Timo Sirainen struct auth_request *auth_request,
0f5dc4da3982053036be65190e44bf28a67b1ca2Timo Sirainen const char **error_r)
65a1c01472ebf88b9f5bbd6636827902936d5497Timo Sirainen{
65a1c01472ebf88b9f5bbd6636827902936d5497Timo Sirainen const struct var_expand_table *table;
65a1c01472ebf88b9f5bbd6636827902936d5497Timo Sirainen string_t *str;
65a1c01472ebf88b9f5bbd6636827902936d5497Timo Sirainen const char *const *args, *value;
65a1c01472ebf88b9f5bbd6636827902936d5497Timo Sirainen unsigned int i, count;
65a1c01472ebf88b9f5bbd6636827902936d5497Timo Sirainen
80e80d414f8fa748d2d10c40db423922968a11ccTimo Sirainen if (passdb_template_is_empty(tmpl))
0f5dc4da3982053036be65190e44bf28a67b1ca2Timo Sirainen return 0;
80e80d414f8fa748d2d10c40db423922968a11ccTimo Sirainen
65a1c01472ebf88b9f5bbd6636827902936d5497Timo Sirainen str = t_str_new(256);
65a1c01472ebf88b9f5bbd6636827902936d5497Timo Sirainen table = auth_request_get_var_expand_table(auth_request, NULL);
65a1c01472ebf88b9f5bbd6636827902936d5497Timo Sirainen
65a1c01472ebf88b9f5bbd6636827902936d5497Timo Sirainen args = array_get(&tmpl->args, &count);
65a1c01472ebf88b9f5bbd6636827902936d5497Timo Sirainen i_assert((count % 2) == 0);
65a1c01472ebf88b9f5bbd6636827902936d5497Timo Sirainen for (i = 0; i < count; i += 2) {
65a1c01472ebf88b9f5bbd6636827902936d5497Timo Sirainen if (args[i+1] == NULL)
d9fe7936864be4fa9e1784dc7dbf451a81f4ab9cTimo Sirainen value = "";
65a1c01472ebf88b9f5bbd6636827902936d5497Timo Sirainen else {
65a1c01472ebf88b9f5bbd6636827902936d5497Timo Sirainen str_truncate(str, 0);
0f5dc4da3982053036be65190e44bf28a67b1ca2Timo Sirainen if (auth_request_var_expand_with_table(str, args[i+1],
0f5dc4da3982053036be65190e44bf28a67b1ca2Timo Sirainen auth_request, table, NULL, error_r) <= 0)
0f5dc4da3982053036be65190e44bf28a67b1ca2Timo Sirainen return -1;
65a1c01472ebf88b9f5bbd6636827902936d5497Timo Sirainen value = str_c(str);
65a1c01472ebf88b9f5bbd6636827902936d5497Timo Sirainen }
65a1c01472ebf88b9f5bbd6636827902936d5497Timo Sirainen auth_request_set_field(auth_request, args[i], value,
65a1c01472ebf88b9f5bbd6636827902936d5497Timo Sirainen STATIC_PASS_SCHEME);
65a1c01472ebf88b9f5bbd6636827902936d5497Timo Sirainen }
0f5dc4da3982053036be65190e44bf28a67b1ca2Timo Sirainen return 0;
65a1c01472ebf88b9f5bbd6636827902936d5497Timo Sirainen}
65a1c01472ebf88b9f5bbd6636827902936d5497Timo Sirainen
65a1c01472ebf88b9f5bbd6636827902936d5497Timo Sirainenbool passdb_template_remove(struct passdb_template *tmpl,
65a1c01472ebf88b9f5bbd6636827902936d5497Timo Sirainen const char *key, const char **value_r)
65a1c01472ebf88b9f5bbd6636827902936d5497Timo Sirainen{
65a1c01472ebf88b9f5bbd6636827902936d5497Timo Sirainen const char *const *args;
65a1c01472ebf88b9f5bbd6636827902936d5497Timo Sirainen unsigned int i, count;
65a1c01472ebf88b9f5bbd6636827902936d5497Timo Sirainen
65a1c01472ebf88b9f5bbd6636827902936d5497Timo Sirainen args = array_get(&tmpl->args, &count);
65a1c01472ebf88b9f5bbd6636827902936d5497Timo Sirainen i_assert((count % 2) == 0);
65a1c01472ebf88b9f5bbd6636827902936d5497Timo Sirainen for (i = 0; i < count; i += 2) {
65a1c01472ebf88b9f5bbd6636827902936d5497Timo Sirainen if (strcmp(args[i], key) == 0) {
65a1c01472ebf88b9f5bbd6636827902936d5497Timo Sirainen *value_r = args[i+1];
65a1c01472ebf88b9f5bbd6636827902936d5497Timo Sirainen array_delete(&tmpl->args, i, 2);
65a1c01472ebf88b9f5bbd6636827902936d5497Timo Sirainen return TRUE;
65a1c01472ebf88b9f5bbd6636827902936d5497Timo Sirainen }
65a1c01472ebf88b9f5bbd6636827902936d5497Timo Sirainen }
65a1c01472ebf88b9f5bbd6636827902936d5497Timo Sirainen return FALSE;
65a1c01472ebf88b9f5bbd6636827902936d5497Timo Sirainen}
80e80d414f8fa748d2d10c40db423922968a11ccTimo Sirainen
80e80d414f8fa748d2d10c40db423922968a11ccTimo Sirainenbool passdb_template_is_empty(struct passdb_template *tmpl)
80e80d414f8fa748d2d10c40db423922968a11ccTimo Sirainen{
80e80d414f8fa748d2d10c40db423922968a11ccTimo Sirainen return array_count(&tmpl->args) == 0;
80e80d414f8fa748d2d10c40db423922968a11ccTimo Sirainen}