1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335
|
/* Copyright (c) 2002-2018 Pigeonhole authors, see the included COPYING file
*/
#include "lib.h"
#include "str.h"
#include "strfuncs.h"
#include "istream.h"
#include "dict.h"
#include "sieve-common.h"
#include "sieve-error.h"
#include "sieve-dump.h"
#include "sieve-binary.h"
#include "sieve-dict-storage.h"
/*
* Script dict implementation
*/
static struct sieve_dict_script *sieve_dict_script_alloc(void)
{
struct sieve_dict_script *dscript;
pool_t pool;
pool = pool_alloconly_create("sieve_dict_script", 1024);
dscript = p_new(pool, struct sieve_dict_script, 1);
dscript->script = sieve_dict_script;
dscript->script.pool = pool;
return dscript;
}
struct sieve_dict_script *sieve_dict_script_init
(struct sieve_dict_storage *dstorage, const char *name)
{
struct sieve_storage *storage = &dstorage->storage;
struct sieve_dict_script *dscript = NULL;
const char *location;
if ( name == NULL ) {
name = SIEVE_DICT_SCRIPT_DEFAULT;
location = storage->location;
} else {
location = t_strconcat
(storage->location, ";name=", name, NULL);
}
dscript = sieve_dict_script_alloc();
sieve_script_init(&dscript->script,
storage, &sieve_dict_script, location, name);
return dscript;
}
static void sieve_dict_script_destroy(struct sieve_script *script)
{
struct sieve_dict_script *dscript =
(struct sieve_dict_script *)script;
if ( dscript->data_pool != NULL )
pool_unref(&dscript->data_pool);
}
static int sieve_dict_script_open
(struct sieve_script *script, enum sieve_error *error_r)
{
struct sieve_dict_script *dscript =
(struct sieve_dict_script *)script;
struct sieve_storage *storage = script->storage;
struct sieve_dict_storage *dstorage =
(struct sieve_dict_storage *)storage;
const char *name = script->name;
const char *path, *data_id, *error;
int ret;
if ( sieve_dict_storage_get_dict
(dstorage, &dscript->dict, error_r) < 0 )
return -1;
path = t_strconcat
(DICT_SIEVE_NAME_PATH, dict_escape_string(name), NULL);
ret = dict_lookup
(dscript->dict, script->pool, path, &data_id, &error);
if ( ret <= 0 ) {
if ( ret < 0 ) {
sieve_script_set_critical(script,
"Failed to lookup script id from path %s: %s", path, error);
*error_r = SIEVE_ERROR_TEMP_FAILURE;
} else {
sieve_script_sys_debug(script,
"Script `%s' not found at path %s", name, path);
sieve_script_set_error(script,
SIEVE_ERROR_NOT_FOUND,
"Sieve script `%s' not found", name);
*error_r = SIEVE_ERROR_NOT_FOUND;
}
return -1;
}
dscript->data_id = p_strdup(script->pool, data_id);
return 0;
}
static int sieve_dict_script_get_stream
(struct sieve_script *script, struct istream **stream_r,
enum sieve_error *error_r)
{
struct sieve_dict_script *dscript =
(struct sieve_dict_script *)script;
const char *path, *name = script->name, *data, *error;
int ret;
dscript->data_pool =
pool_alloconly_create("sieve_dict_script data pool", 1024);
path = t_strconcat
(DICT_SIEVE_DATA_PATH, dict_escape_string(dscript->data_id), NULL);
ret = dict_lookup
(dscript->dict, dscript->data_pool, path, &data, &error);
if ( ret <= 0 ) {
if ( ret < 0 ) {
sieve_script_set_critical(script,
"Failed to lookup data with id `%s' "
"for script `%s' from path %s: %s",
dscript->data_id, name, path, error);
} else {
sieve_script_set_critical(script,
"Data with id `%s' for script `%s' "
"not found at path %s",
dscript->data_id, name, path);
}
*error_r = SIEVE_ERROR_TEMP_FAILURE;
return -1;
}
dscript->data = p_strdup(script->pool, data);
*stream_r = i_stream_create_from_data(dscript->data, strlen(dscript->data));
return 0;
}
static int sieve_dict_script_binary_read_metadata
(struct sieve_script *script, struct sieve_binary_block *sblock,
sieve_size_t *offset)
{
struct sieve_dict_script *dscript =
(struct sieve_dict_script *)script;
struct sieve_binary *sbin =
sieve_binary_block_get_binary(sblock);
string_t *data_id;
if ( dscript->data_id == NULL &&
sieve_script_open(script, NULL) < 0 )
return 0;
if ( !sieve_binary_read_string(sblock, offset, &data_id) ) {
sieve_script_sys_error(script,
"Binary `%s' has invalid metadata for script `%s'",
sieve_binary_path(sbin), sieve_script_location(script));
return -1;
}
i_assert( dscript->data_id != NULL );
if ( strcmp(str_c(data_id), dscript->data_id) != 0 ) {
sieve_script_sys_debug(script,
"Binary `%s' reports different data ID for script `%s' "
"(`%s' rather than `%s')",
sieve_binary_path(sbin), sieve_script_location(script),
str_c(data_id), dscript->data_id);
return 0;
}
return 1;
}
static void sieve_dict_script_binary_write_metadata
(struct sieve_script *script, struct sieve_binary_block *sblock)
{
struct sieve_dict_script *dscript =
(struct sieve_dict_script *)script;
sieve_binary_emit_cstring(sblock, dscript->data_id);
}
static bool sieve_dict_script_binary_dump_metadata
(struct sieve_script *script ATTR_UNUSED, struct sieve_dumptime_env *denv,
struct sieve_binary_block *sblock, sieve_size_t *offset)
{
string_t *data_id;
if ( !sieve_binary_read_string(sblock, offset, &data_id) )
return FALSE;
sieve_binary_dumpf(denv, "dict.data_id = %s\n", str_c(data_id));
return TRUE;
}
static const char * sieve_dict_script_get_binpath
(struct sieve_dict_script *dscript)
{
struct sieve_script *script = &dscript->script;
struct sieve_storage *storage = script->storage;
if ( dscript->binpath == NULL ) {
if ( storage->bin_dir == NULL )
return NULL;
dscript->binpath = p_strconcat(script->pool,
storage->bin_dir, "/",
sieve_binfile_from_name(script->name), NULL);
}
return dscript->binpath;
}
static struct sieve_binary *sieve_dict_script_binary_load
(struct sieve_script *script, enum sieve_error *error_r)
{
struct sieve_dict_script *dscript =
(struct sieve_dict_script *)script;
if ( sieve_dict_script_get_binpath(dscript) == NULL )
return NULL;
return sieve_binary_open(script->storage->svinst,
dscript->binpath, script, error_r);
}
static int sieve_dict_script_binary_save
(struct sieve_script *script, struct sieve_binary *sbin, bool update,
enum sieve_error *error_r)
{
struct sieve_dict_script *dscript =
(struct sieve_dict_script *)script;
if ( sieve_dict_script_get_binpath(dscript) == NULL )
return 0;
if ( sieve_storage_setup_bindir(script->storage, 0700) < 0 )
return -1;
return sieve_binary_save(sbin,
dscript->binpath, update, 0600, error_r);
}
static bool sieve_dict_script_equals
(const struct sieve_script *script, const struct sieve_script *other)
{
struct sieve_storage *storage = script->storage;
struct sieve_storage *sother = other->storage;
if ( strcmp(storage->location, sother->location) != 0 )
return FALSE;
i_assert( script->name != NULL && other->name != NULL );
return ( strcmp(script->name, other->name) == 0 );
}
const struct sieve_script sieve_dict_script = {
.driver_name = SIEVE_DICT_STORAGE_DRIVER_NAME,
.v = {
.destroy = sieve_dict_script_destroy,
.open = sieve_dict_script_open,
.get_stream = sieve_dict_script_get_stream,
.binary_read_metadata =sieve_dict_script_binary_read_metadata,
.binary_write_metadata = sieve_dict_script_binary_write_metadata,
.binary_dump_metadata = sieve_dict_script_binary_dump_metadata,
.binary_load = sieve_dict_script_binary_load,
.binary_save = sieve_dict_script_binary_save,
.equals = sieve_dict_script_equals
}
};
/*
* Script sequence
*/
struct sieve_dict_script_sequence {
struct sieve_script_sequence seq;
bool done:1;
};
struct sieve_script_sequence *sieve_dict_storage_get_script_sequence
(struct sieve_storage *storage, enum sieve_error *error_r)
{
struct sieve_dict_script_sequence *dseq = NULL;
if ( error_r != NULL )
*error_r = SIEVE_ERROR_NONE;
/* Create sequence object */
dseq = i_new(struct sieve_dict_script_sequence, 1);
sieve_script_sequence_init(&dseq->seq, storage);
return &dseq->seq;
}
struct sieve_script *sieve_dict_script_sequence_next
(struct sieve_script_sequence *seq, enum sieve_error *error_r)
{
struct sieve_dict_script_sequence *dseq =
(struct sieve_dict_script_sequence *)seq;
struct sieve_dict_storage *dstorage =
(struct sieve_dict_storage *)seq->storage;
struct sieve_dict_script *dscript;
if ( error_r != NULL )
*error_r = SIEVE_ERROR_NONE;
if ( dseq->done )
return NULL;
dseq->done = TRUE;
dscript = sieve_dict_script_init
(dstorage, seq->storage->script_name);
if ( sieve_script_open(&dscript->script, error_r) < 0 ) {
struct sieve_script *script = &dscript->script;
sieve_script_unref(&script);
return NULL;
}
return &dscript->script;
}
void sieve_dict_script_sequence_destroy(struct sieve_script_sequence *seq)
{
struct sieve_dict_script_sequence *dseq =
(struct sieve_dict_script_sequence *)seq;
i_free(dseq);
}
|