userdb-template.c revision 65a1c01472ebf88b9f5bbd6636827902936d5497
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen/* Copyright (c) 2003-2011 Dovecot authors, see the included COPYING file */
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen#include "auth-common.h"
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen#include "array.h"
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen#include "str.h"
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen#include "var-expand.h"
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen#include "userdb.h"
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen#include "userdb-template.h"
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainenstruct userdb_template {
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen ARRAY_DEFINE(args, const char *);
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen};
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainenstruct userdb_template *
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainenuserdb_template_build(pool_t pool, const char *userdb_name, const char *args)
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen{
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen struct userdb_template *tmpl;
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen const char *const *tmp, *key, *value, *nonull_value;
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen uid_t uid;
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen gid_t gid;
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen tmpl = p_new(pool, struct userdb_template, 1);
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen tmp = t_strsplit_spaces(args, " ");
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen p_array_init(&tmpl->args, pool, str_array_length(tmp));
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen for (; *tmp != NULL; tmp++) {
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen value = strchr(*tmp, '=');
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen if (value == NULL)
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen key = *tmp;
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen else
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen key = t_strdup_until(*tmp, value++);
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen nonull_value = value == NULL ? "" : value;
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen if (strcasecmp(key, "uid") == 0) {
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen uid = userdb_parse_uid(NULL, nonull_value);
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen if (uid == (uid_t)-1) {
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen i_fatal("%s userdb: Invalid uid: %s",
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen userdb_name, nonull_value);
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen }
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen value = dec2str(uid);
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen } else if (strcasecmp(key, "gid") == 0) {
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen gid = userdb_parse_gid(NULL, nonull_value);
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen if (gid == (gid_t)-1) {
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen i_fatal("%s userdb: Invalid gid: %s",
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen userdb_name, nonull_value);
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen }
25757faf029c369a8318349dafe952e2358df1d8Timo Sirainen value = dec2str(gid);
} else if (*key == '\0') {
i_fatal("%s userdb: Empty key (=%s)",
userdb_name, nonull_value);
}
key = p_strdup(pool, key);
value = p_strdup(pool, value);
array_append(&tmpl->args, &key, 1);
array_append(&tmpl->args, &value, 1);
}
return tmpl;
}
void userdb_template_export(struct userdb_template *tmpl,
struct auth_request *auth_request)
{
const struct var_expand_table *table;
string_t *str;
const char *const *args, *value;
unsigned int i, count;
str = t_str_new(256);
table = auth_request_get_var_expand_table(auth_request, NULL);
args = array_get(&tmpl->args, &count);
i_assert((count % 2) == 0);
for (i = 0; i < count; i += 2) {
if (args[i+1] == NULL)
value = NULL;
else {
str_truncate(str, 0);
var_expand(str, args[i+1], table);
value = str_c(str);
}
auth_request_set_userdb_field(auth_request, args[i], value);
}
}
bool userdb_template_remove(struct userdb_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;
}
bool userdb_template_is_empty(struct userdb_template *tmpl)
{
return array_count(&tmpl->args) == 0;
}