db-dict-cache-key.c revision bcb4e51a409d94ae670de96afb8483a4f7855294
02c335c23bf5fa225a467c19f2c063fb0dc7b8c3Timo Sirainen/* Copyright (c) 2013-2018 Dovecot authors, see the included COPYING file */
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch#include "auth-common.h"
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch#include "array.h"
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch#include "str.h"
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch#include "var-expand.h"
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch#include "db-dict.h"
a991cfe2157e58ee43bc580f517ce9ef0dfb7acfStephan Bosch
de0181258ab66b527ad8dc7e51a8efa76b4658d0Stephan Boschconst struct db_dict_key *
7384b4e78eaab44693c985192276e31322155e32Stephan Boschdb_dict_set_key_find(const ARRAY_TYPE(db_dict_key) *keys, const char *name)
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch{
0d5c9a80e91a4073d5fd6820e9ddce2755221f64Stephan Bosch const struct db_dict_key *key;
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch
6d573191bea1a64d6046be070487a5705a2d0204Stephan Bosch array_foreach(keys, key) {
30f35cf5d1e1374d7fab4231e86144fc106a8e79Stephan Bosch if (strcmp(key->name, name) == 0)
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch return key;
65c0e43da8cfc730eeb4634f8aa384081bbfa4e7Timo Sirainen }
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch return NULL;
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch}
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch
1d048c5050f03c24251e5af8087e640de21b2d62Timo Sirainen
1d048c5050f03c24251e5af8087e640de21b2d62Timo Sirainenconst char *
1d048c5050f03c24251e5af8087e640de21b2d62Timo Sirainendb_dict_parse_cache_key(const ARRAY_TYPE(db_dict_key) *keys,
1d048c5050f03c24251e5af8087e640de21b2d62Timo Sirainen const ARRAY_TYPE(db_dict_field) *fields,
1d048c5050f03c24251e5af8087e640de21b2d62Timo Sirainen const ARRAY_TYPE(db_dict_key_p) *objects)
1d048c5050f03c24251e5af8087e640de21b2d62Timo Sirainen{
1d048c5050f03c24251e5af8087e640de21b2d62Timo Sirainen const struct db_dict_field *field;
1d048c5050f03c24251e5af8087e640de21b2d62Timo Sirainen const struct db_dict_key *key;
1d048c5050f03c24251e5af8087e640de21b2d62Timo Sirainen const struct db_dict_key *const *keyp;
1d048c5050f03c24251e5af8087e640de21b2d62Timo Sirainen const char *p, *name;
1d048c5050f03c24251e5af8087e640de21b2d62Timo Sirainen unsigned int idx, size;
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch string_t *str = t_str_new(128);
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch array_foreach(fields, field) {
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch for (p = field->value; *p != '\0'; ) {
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch if (*p != '%') {
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch p++;
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch continue;
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch }
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch var_get_key_range(++p, &idx, &size);
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch if (size == 0) {
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch /* broken %variable ending too early */
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch break;
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch }
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch p += idx;
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch if (size > 5 && memcmp(p, "dict:", 5) == 0) {
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch name = t_strcut(t_strndup(p+5, size-5), ':');
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch key = db_dict_set_key_find(keys, name);
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch if (key != NULL)
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch str_printfa(str, "\t%s", key->key);
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch } else if (size == 1) {
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch str_printfa(str, "\t%%%c", p[0]);
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch } else {
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch str_append(str, "\t%{");
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch str_append_n(str, p, size);
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch str_append_c(str, '}');
d6a88217c273cc67f5de93949bdd065a15eeabe0Stephan Bosch }
9145c8b5eda526d05bd4a7ced20f6f6f2ff8df03Stephan Bosch p += size;
9145c8b5eda526d05bd4a7ced20f6f6f2ff8df03Stephan Bosch }
9145c8b5eda526d05bd4a7ced20f6f6f2ff8df03Stephan Bosch }
7abab3b191860a3d77af5192b0649833c8a0c803Stephan Bosch array_foreach(objects, keyp)
7abab3b191860a3d77af5192b0649833c8a0c803Stephan Bosch str_printfa(str, "\t%s", (*keyp)->key);
7abab3b191860a3d77af5192b0649833c8a0c803Stephan Bosch return str_c(str);
7abab3b191860a3d77af5192b0649833c8a0c803Stephan Bosch}
7abab3b191860a3d77af5192b0649833c8a0c803Stephan Bosch