passdb-template.c revision d9fe7936864be4fa9e1784dc7dbf451a81f4ab9c
159N/A/* Copyright (c) 2003-2011 Dovecot authors, see the included COPYING file */
159N/A
159N/A#include "auth-common.h"
159N/A#include "array.h"
159N/A#include "str.h"
159N/A#include "var-expand.h"
159N/A#include "passdb.h"
159N/A#include "passdb-template.h"
159N/A
159N/Astruct passdb_template {
159N/A ARRAY_DEFINE(args, const char *);
159N/A};
159N/A
159N/Astruct passdb_template *passdb_template_build(pool_t pool, const char *args)
159N/A{
159N/A struct passdb_template *tmpl;
159N/A const char *const *tmp, *key, *value;
159N/A
159N/A tmpl = p_new(pool, struct passdb_template, 1);
159N/A
159N/A tmp = t_strsplit_spaces(args, " ");
159N/A p_array_init(&tmpl->args, pool, str_array_length(tmp));
159N/A
159N/A for (; *tmp != NULL; tmp++) {
159N/A value = strchr(*tmp, '=');
159N/A if (value == NULL)
159N/A key = *tmp;
159N/A else
159N/A key = t_strdup_until(*tmp, value++);
159N/A
159N/A key = p_strdup(pool, key);
159N/A value = p_strdup(pool, value);
159N/A array_append(&tmpl->args, &key, 1);
159N/A array_append(&tmpl->args, &value, 1);
159N/A }
159N/A return tmpl;
159N/A}
159N/A
159N/Avoid passdb_template_export(struct passdb_template *tmpl,
206N/A struct auth_request *auth_request)
159N/A{
159N/A const struct var_expand_table *table;
159N/A string_t *str;
159N/A const char *const *args, *value;
159N/A unsigned int i, count;
159N/A
159N/A str = t_str_new(256);
181N/A table = auth_request_get_var_expand_table(auth_request, NULL);
159N/A
159N/A args = array_get(&tmpl->args, &count);
159N/A i_assert((count % 2) == 0);
159N/A for (i = 0; i < count; i += 2) {
if (args[i+1] == NULL)
value = "";
else {
str_truncate(str, 0);
var_expand(str, args[i+1], table);
value = str_c(str);
}
auth_request_set_field(auth_request, args[i], value,
STATIC_PASS_SCHEME);
}
}
bool passdb_template_remove(struct passdb_template *tmpl,
const char *key, const char **value_r)
{
const char *const *args;
unsigned int i, count;
args = array_get(&tmpl->args, &count);
i_assert((count % 2) == 0);
for (i = 0; i < count; i += 2) {
if (strcmp(args[i], key) == 0) {
*value_r = args[i+1];
array_delete(&tmpl->args, i, 2);
return TRUE;
}
}
return FALSE;
}