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 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929
|
/* Copyright (c) 2002-2013 Pigeonhole authors, see the included COPYING file
*/
#include "lib.h"
#include "str.h"
#include "istream.h"
#include "buffer.h"
#include "eacces-error.h"
#include "home-expand.h"
#include "sieve-limits.h"
#include "sieve-settings.h"
#include "sieve-extensions.h"
#include "sieve-plugins.h"
#include "sieve-script.h"
#include "sieve-script-file.h"
#include "sieve-ast.h"
#include "sieve-binary.h"
#include "sieve-actions.h"
#include "sieve-result.h"
#include "sieve-parser.h"
#include "sieve-validator.h"
#include "sieve-generator.h"
#include "sieve-interpreter.h"
#include "sieve-binary-dumper.h"
#include "sieve.h"
#include "sieve-common.h"
#include "sieve-error-private.h"
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <dirent.h>
/*
* Main Sieve library interface
*/
struct sieve_instance *sieve_init
(const struct sieve_environment *env,
const struct sieve_callbacks *callbacks, void *context, bool debug)
{
struct sieve_instance *svinst;
unsigned long long int uint_setting;
size_t size_setting;
const char *domain;
pool_t pool;
/* Create Sieve engine instance */
pool = pool_alloconly_create("sieve", 8192);
svinst = p_new(pool, struct sieve_instance, 1);
svinst->pool = pool;
svinst->callbacks = callbacks;
svinst->context = context;
svinst->debug = debug;
svinst->base_dir = p_strdup_empty(pool, env->base_dir);
svinst->username = p_strdup_empty(pool, env->username);
svinst->home_dir = p_strdup_empty(pool, env->home_dir);
svinst->flags = env->flags;
svinst->env_location = env->location;
svinst->delivery_phase = env->delivery_phase;
/* Determine domain */
if ( env->domainname != NULL && *(env->domainname) != '\0' ) {
domain = env->domainname;
} else {
/* Fall back to parsing username localpart@domain */
domain = svinst->username == NULL ? NULL :
strchr(svinst->username, '@');
if ( domain == NULL || *(domain+1) == '\0' ) {
/* Fall back to parsing hostname host.domain */
domain = ( env->hostname != NULL ? strchr(env->hostname, '.') : NULL );
if ( domain == NULL || *(domain+1) == '\0'
|| strchr(domain+1, '.') == NULL ) {
/* Fall back to bare hostname */
domain = env->hostname;
} else {
domain++;
}
} else {
domain++;
}
}
svinst->hostname = p_strdup_empty(pool, env->hostname);
svinst->domainname = p_strdup(pool, domain);
sieve_errors_init(svinst);
if ( debug ) {
sieve_sys_debug(svinst, "%s version %s initializing",
PIGEONHOLE_NAME, PIGEONHOLE_VERSION);
}
/* Read limits from configuration */
svinst->max_script_size = SIEVE_DEFAULT_MAX_SCRIPT_SIZE;
svinst->max_actions = SIEVE_DEFAULT_MAX_ACTIONS;
svinst->max_redirects = SIEVE_DEFAULT_MAX_REDIRECTS;
if ( sieve_setting_get_size_value
(svinst, "sieve_max_script_size", &size_setting) ) {
svinst->max_script_size = size_setting;
}
if ( sieve_setting_get_uint_value
(svinst, "sieve_max_actions", &uint_setting) ) {
svinst->max_actions = (unsigned int) uint_setting;
}
if ( sieve_setting_get_uint_value
(svinst, "sieve_max_redirects", &uint_setting) ) {
svinst->max_redirects = (unsigned int) uint_setting;
}
/* Initialize extensions */
if ( !sieve_extensions_init(svinst) ) {
sieve_deinit(&svinst);
return NULL;
}
sieve_plugins_load(svinst, NULL, NULL);
sieve_extensions_configure(svinst);
return svinst;
}
void sieve_deinit(struct sieve_instance **svinst)
{
sieve_plugins_unload(*svinst);
sieve_extensions_deinit(*svinst);
sieve_errors_deinit(*svinst);
pool_unref(&(*svinst)->pool);
*svinst = NULL;
}
void sieve_set_extensions
(struct sieve_instance *svinst, const char *extensions)
{
sieve_extensions_set_string(svinst, extensions, FALSE);
}
const char *sieve_get_capabilities
(struct sieve_instance *svinst, const char *name)
{
if ( name == NULL || *name == '\0' )
return sieve_extensions_get_string(svinst);
return sieve_extension_capabilities_get_string(svinst, name);
}
/*
* Low-level compiler functions
*/
struct sieve_ast *sieve_parse
(struct sieve_script *script, struct sieve_error_handler *ehandler,
enum sieve_error *error_r)
{
struct sieve_parser *parser;
struct sieve_ast *ast = NULL;
/* Parse */
if ( (parser = sieve_parser_create(script, ehandler, error_r)) == NULL )
return NULL;
if ( !sieve_parser_run(parser, &ast) ) {
ast = NULL;
} else
sieve_ast_ref(ast);
sieve_parser_free(&parser);
if ( error_r != NULL ) {
if ( ast == NULL )
*error_r = SIEVE_ERROR_NOT_VALID;
else
*error_r = SIEVE_ERROR_NONE;
}
return ast;
}
bool sieve_validate
(struct sieve_ast *ast, struct sieve_error_handler *ehandler,
enum sieve_compile_flags flags, enum sieve_error *error_r)
{
bool result = TRUE;
struct sieve_validator *validator =
sieve_validator_create(ast, ehandler, flags);
if ( !sieve_validator_run(validator) )
result = FALSE;
sieve_validator_free(&validator);
if ( error_r != NULL ) {
if ( !result )
*error_r = SIEVE_ERROR_NOT_VALID;
else
*error_r = SIEVE_ERROR_NONE;
}
return result;
}
static struct sieve_binary *sieve_generate
(struct sieve_ast *ast, struct sieve_error_handler *ehandler,
enum sieve_compile_flags flags, enum sieve_error *error_r)
{
struct sieve_generator *generator =
sieve_generator_create(ast, ehandler, flags);
struct sieve_binary *sbin = NULL;
sbin = sieve_generator_run(generator, NULL);
sieve_generator_free(&generator);
if ( error_r != NULL ) {
if ( sbin == NULL )
*error_r = SIEVE_ERROR_NOT_VALID;
else
*error_r = SIEVE_ERROR_NONE;
}
return sbin;
}
/*
* Sieve compilation
*/
struct sieve_binary *sieve_compile_script
(struct sieve_script *script, struct sieve_error_handler *ehandler,
enum sieve_compile_flags flags, enum sieve_error *error_r)
{
struct sieve_ast *ast;
struct sieve_binary *sbin;
/* Parse */
if ( (ast = sieve_parse(script, ehandler, error_r)) == NULL ) {
sieve_error(ehandler, sieve_script_name(script), "parse failed");
return NULL;
}
/* Validate */
if ( !sieve_validate(ast, ehandler, flags, error_r) ) {
sieve_error(ehandler, sieve_script_name(script), "validation failed");
sieve_ast_unref(&ast);
return NULL;
}
/* Generate */
if ( (sbin=sieve_generate(ast, ehandler, flags, error_r)) == NULL ) {
sieve_error(ehandler, sieve_script_name(script), "code generation failed");
sieve_ast_unref(&ast);
return NULL;
}
/* Cleanup */
sieve_ast_unref(&ast);
if ( error_r != NULL )
error_r = SIEVE_ERROR_NONE;
return sbin;
}
struct sieve_binary *sieve_compile
(struct sieve_instance *svinst, const char *script_location,
const char *script_name, struct sieve_error_handler *ehandler,
enum sieve_compile_flags flags, enum sieve_error *error_r)
{
struct sieve_script *script;
struct sieve_binary *sbin;
if ( (script = sieve_script_create_open
(svinst, script_location, script_name, ehandler, error_r)) == NULL )
return NULL;
sbin = sieve_compile_script(script, ehandler, flags, error_r);
if ( svinst->debug && sbin != NULL ) {
sieve_sys_debug(svinst, "script `%s' from %s successfully compiled",
sieve_script_name(script), sieve_script_location(script));
}
sieve_script_unref(&script);
return sbin;
}
/*
* Sieve runtime
*/
static int sieve_run
(struct sieve_binary *sbin, struct sieve_result **result,
const struct sieve_message_data *msgdata, const struct sieve_script_env *senv,
struct sieve_error_handler *ehandler, enum sieve_runtime_flags flags)
{
struct sieve_interpreter *interp;
int ret = 0;
/* Create the interpreter */
if ( (interp=sieve_interpreter_create(sbin, msgdata, senv, ehandler, flags))
== NULL )
return SIEVE_EXEC_BIN_CORRUPT;
/* Reset execution status */
if ( senv->exec_status != NULL )
memset(senv->exec_status, 0, sizeof(*senv->exec_status));
/* Create result object */
if ( *result == NULL )
*result = sieve_result_create
(sieve_binary_svinst(sbin), msgdata, senv, ehandler);
else {
sieve_result_set_error_handler(*result, ehandler);
}
/* Run the interpreter */
ret = sieve_interpreter_run(interp, *result);
/* Free the interpreter */
sieve_interpreter_free(&interp);
return ret;
}
/*
* Reading/writing sieve binaries
*/
struct sieve_binary *sieve_load
(struct sieve_instance *svinst, const char *bin_path, enum sieve_error *error_r)
{
return sieve_binary_open(svinst, bin_path, NULL, error_r);
}
struct sieve_binary *sieve_open_script
(struct sieve_script *script, struct sieve_error_handler *ehandler,
enum sieve_compile_flags flags, enum sieve_error *error_r)
{
struct sieve_instance *svinst = sieve_script_svinst(script);
struct sieve_binary *sbin;
T_BEGIN {
/* Then try to open the matching binary */
sbin = sieve_script_binary_load(script, error_r);
if (sbin != NULL) {
/* Ok, it exists; now let's see if it is up to date */
if ( !sieve_binary_up_to_date(sbin, flags) ) {
/* Not up to date */
if ( svinst->debug )
sieve_sys_debug(svinst, "script binary %s is not up-to-date",
sieve_binary_path(sbin));
sieve_binary_unref(&sbin);
sbin = NULL;
}
}
/* If the binary does not exist or is not up-to-date, we need
* to (re-)compile.
*/
if ( sbin != NULL ) {
if ( svinst->debug )
sieve_sys_debug(svinst, "script binary %s successfully loaded",
sieve_binary_path(sbin));
} else {
sbin = sieve_compile_script(script, ehandler, flags, error_r);
if ( sbin != NULL ) {
if ( svinst->debug )
sieve_sys_debug(svinst, "script `%s' from %s successfully compiled",
sieve_script_name(script), sieve_script_location(script));
}
}
} T_END;
return sbin;
}
struct sieve_binary *sieve_open
(struct sieve_instance *svinst, const char *script_location,
const char *script_name, struct sieve_error_handler *ehandler,
enum sieve_compile_flags flags, enum sieve_error *error_r)
{
struct sieve_script *script;
struct sieve_binary *sbin;
/* First open the scriptfile itself */
if ( (script=sieve_script_create_open
(svinst, script_location, script_name, ehandler, error_r)) == NULL ) {
/* Failed */
return NULL;
}
sbin = sieve_open_script(script, ehandler, flags, error_r);
/* Drop script reference, if sbin != NULL it holds a reference of its own.
* Otherwise the script object is freed here.
*/
sieve_script_unref(&script);
return sbin;
}
const char *sieve_get_source(struct sieve_binary *sbin)
{
return sieve_binary_source(sbin);
}
bool sieve_is_loaded(struct sieve_binary *sbin)
{
return sieve_binary_loaded(sbin);
}
int sieve_save_as
(struct sieve_binary *sbin, const char *bin_path, bool update,
mode_t save_mode, enum sieve_error *error_r)
{
if ( bin_path == NULL )
return sieve_save(sbin, update, error_r);
return sieve_binary_save(sbin, bin_path, update, save_mode, error_r);
}
int sieve_save
(struct sieve_binary *sbin, bool update, enum sieve_error *error_r)
{
struct sieve_script *script = sieve_binary_script(sbin);
if ( script == NULL ) {
return sieve_binary_save(sbin, NULL, update, 0600, error_r);
}
return sieve_script_binary_save(script, sbin, update, error_r);
}
void sieve_close(struct sieve_binary **sbin)
{
sieve_binary_unref(sbin);
}
/*
* Debugging
*/
void sieve_dump
(struct sieve_binary *sbin, struct ostream *stream, bool verbose)
{
struct sieve_binary_dumper *dumpr = sieve_binary_dumper_create(sbin);
sieve_binary_dumper_run(dumpr, stream, verbose);
sieve_binary_dumper_free(&dumpr);
}
void sieve_hexdump
(struct sieve_binary *sbin, struct ostream *stream)
{
struct sieve_binary_dumper *dumpr = sieve_binary_dumper_create(sbin);
sieve_binary_dumper_hexdump(dumpr, stream);
sieve_binary_dumper_free(&dumpr);
}
int sieve_test
(struct sieve_binary *sbin, const struct sieve_message_data *msgdata,
const struct sieve_script_env *senv, struct sieve_error_handler *ehandler,
struct ostream *stream, enum sieve_runtime_flags flags, bool *keep)
{
struct sieve_result *result = NULL;
int ret;
if ( keep != NULL ) *keep = FALSE;
/* Run the script */
ret = sieve_run(sbin, &result, msgdata, senv, ehandler, flags);
/* Print result if successful */
if ( ret > 0 ) {
ret = sieve_result_print(result, senv, stream, keep);
} else if ( ret == 0 ) {
if ( keep != NULL ) *keep = TRUE;
}
/* Cleanup */
if ( result != NULL )
sieve_result_unref(&result);
return ret;
}
/*
* Script execution
*/
int sieve_execute
(struct sieve_binary *sbin, const struct sieve_message_data *msgdata,
const struct sieve_script_env *senv, struct sieve_error_handler *ehandler,
enum sieve_runtime_flags flags, bool *keep)
{
struct sieve_result *result = NULL;
int ret;
if ( keep != NULL ) *keep = FALSE;
/* Run the script */
ret = sieve_run(sbin, &result, msgdata, senv, ehandler, flags);
/* Evaluate status and execute the result:
* Strange situations, e.g. currupt binaries, must be handled by the caller.
* In that case no implicit keep is attempted, because the situation may be
* resolved.
*/
if ( ret > 0 ) {
/* Execute result */
ret = sieve_result_execute(result, keep);
} else if ( ret == SIEVE_EXEC_FAILURE ) {
/* Perform implicit keep if script failed with a normal runtime error */
switch ( sieve_result_implicit_keep(result) ) {
case SIEVE_EXEC_OK:
if ( keep != NULL ) *keep = TRUE;
break;
case SIEVE_EXEC_TEMP_FAILURE:
ret = SIEVE_EXEC_TEMP_FAILURE;
break;
default:
ret = SIEVE_EXEC_KEEP_FAILED;
}
}
/* Cleanup */
if ( result != NULL )
sieve_result_unref(&result);
return ret;
}
/*
* Multiscript support
*/
struct sieve_multiscript {
struct sieve_instance *svinst;
struct sieve_result *result;
const struct sieve_message_data *msgdata;
const struct sieve_script_env *scriptenv;
int status;
bool active;
bool keep;
struct ostream *teststream;
};
struct sieve_multiscript *sieve_multiscript_start_execute
(struct sieve_instance *svinst, const struct sieve_message_data *msgdata,
const struct sieve_script_env *senv)
{
pool_t pool;
struct sieve_result *result;
struct sieve_multiscript *mscript;
result = sieve_result_create(svinst, msgdata, senv, NULL);
pool = sieve_result_pool(result);
sieve_result_set_keep_action(result, NULL, NULL);
mscript = p_new(pool, struct sieve_multiscript, 1);
mscript->svinst = svinst;
mscript->result = result;
mscript->msgdata = msgdata;
mscript->scriptenv = senv;
mscript->status = SIEVE_EXEC_OK;
mscript->active = TRUE;
mscript->keep = TRUE;
return mscript;
}
struct sieve_multiscript *sieve_multiscript_start_test
(struct sieve_instance *svinst, const struct sieve_message_data *msgdata,
const struct sieve_script_env *senv, struct ostream *stream)
{
struct sieve_multiscript *mscript =
sieve_multiscript_start_execute(svinst, msgdata, senv);
mscript->teststream = stream;
return mscript;
}
static void sieve_multiscript_test
(struct sieve_multiscript *mscript, struct sieve_error_handler *ehandler,
bool *keep)
{
sieve_result_set_error_handler(mscript->result, ehandler);
if ( mscript->status > 0 ) {
mscript->status = sieve_result_print
(mscript->result, mscript->scriptenv, mscript->teststream, keep);
} else {
if ( keep != NULL ) *keep = TRUE;
}
sieve_result_mark_executed(mscript->result);
}
static void sieve_multiscript_execute
(struct sieve_multiscript *mscript, struct sieve_error_handler *ehandler,
bool *keep)
{
sieve_result_set_error_handler(mscript->result, ehandler);
if ( mscript->status > 0 ) {
mscript->status = sieve_result_execute(mscript->result, keep);
} else {
if ( !sieve_result_implicit_keep(mscript->result) )
mscript->status = SIEVE_EXEC_KEEP_FAILED;
else
if ( keep != NULL ) *keep = TRUE;
}
}
bool sieve_multiscript_run
(struct sieve_multiscript *mscript, struct sieve_binary *sbin,
struct sieve_error_handler *ehandler, enum sieve_runtime_flags flags,
bool final)
{
if ( !mscript->active ) return FALSE;
if ( final )
sieve_result_set_keep_action(mscript->result, NULL, &act_store);
/* Run the script */
mscript->status = sieve_run(sbin, &mscript->result, mscript->msgdata,
mscript->scriptenv, ehandler, flags);
if ( mscript->status >= 0 ) {
mscript->keep = FALSE;
if ( mscript->teststream != NULL )
sieve_multiscript_test(mscript, ehandler, &mscript->keep);
else
sieve_multiscript_execute(mscript, ehandler, &mscript->keep);
mscript->active =
( mscript->active && mscript->keep && !final && mscript->status > 0 );
}
if ( mscript->status <= 0 )
return FALSE;
return mscript->active;
}
int sieve_multiscript_status(struct sieve_multiscript *mscript)
{
return mscript->status;
}
int sieve_multiscript_tempfail(struct sieve_multiscript **mscript,
struct sieve_error_handler *ehandler)
{
struct sieve_result *result = (*mscript)->result;
int ret = (*mscript)->status;
if ( ehandler != NULL )
sieve_result_set_error_handler(result, ehandler);
if ( (*mscript)->active ) {
ret = SIEVE_EXEC_TEMP_FAILURE;
if ( !(*mscript)->teststream && sieve_result_executed(result) ) {
/* Part of the result is already executed, need to fall back to
* to implicit keep (FIXME)
*/
switch ( sieve_result_implicit_keep(result) ) {
case SIEVE_EXEC_OK:
ret = SIEVE_EXEC_FAILURE;
break;
default:
ret = SIEVE_EXEC_KEEP_FAILED;
}
}
}
/* Cleanup */
sieve_result_unref(&result);
*mscript = NULL;
return ret;
}
int sieve_multiscript_finish(struct sieve_multiscript **mscript,
struct sieve_error_handler *ehandler, bool *keep)
{
struct sieve_result *result = (*mscript)->result;
int ret = (*mscript)->status;
if ( ehandler != NULL )
sieve_result_set_error_handler(result, ehandler);
if ( (*mscript)->active ) {
ret = SIEVE_EXEC_FAILURE;
if ( (*mscript)->teststream ) {
(*mscript)->keep = TRUE;
} else {
switch ( sieve_result_implicit_keep(result) ) {
case SIEVE_EXEC_OK:
(*mscript)->keep = TRUE;
break;
case SIEVE_EXEC_TEMP_FAILURE:
if (!sieve_result_executed(result)) {
ret = SIEVE_EXEC_TEMP_FAILURE;
break;
}
/* fall through */
default:
ret = SIEVE_EXEC_KEEP_FAILED;
}
}
}
if ( keep != NULL ) *keep = (*mscript)->keep;
/* Cleanup */
sieve_result_unref(&result);
*mscript = NULL;
return ret;
}
/*
* Configured Limits
*/
unsigned int sieve_max_redirects(struct sieve_instance *svinst)
{
return svinst->max_redirects;
}
unsigned int sieve_max_actions(struct sieve_instance *svinst)
{
return svinst->max_actions;
}
size_t sieve_max_script_size(struct sieve_instance *svinst)
{
return svinst->max_script_size;
}
/*
* Script directory
*/
struct sieve_directory {
struct sieve_instance *svinst;
DIR *dirp;
const char *path;
};
struct sieve_directory *sieve_directory_open
(struct sieve_instance *svinst, const char *path, enum sieve_error *error_r)
{
struct sieve_directory *sdir = NULL;
DIR *dirp;
struct stat st;
if ( error_r != NULL )
*error_r = SIEVE_ERROR_NONE;
if ( (path[0] == '~' && (path[1] == '/' || path[1] == '\0')) ||
(((svinst->flags & SIEVE_FLAG_HOME_RELATIVE) != 0 ) && path[0] != '/') ) {
/* Home-relative path. change to absolute. */
const char *home = sieve_environment_get_homedir(svinst);
if ( home != NULL ) {
if ( path[0] == '~' && (path[1] == '/' || path[1] == '\0') )
path = home_expand_tilde(path, home);
else
path = t_strconcat(home, "/", path, NULL);
} else {
sieve_sys_error(svinst,
"sieve dir path %s is relative to home directory, "
"but home directory is not available.", path);
if ( error_r != NULL )
*error_r = SIEVE_ERROR_TEMP_FAILURE;
return NULL;
}
}
/* Specified path can either be a regular file or a directory */
if ( stat(path, &st) != 0 ) {
switch ( errno ) {
case ENOENT:
if ( error_r != NULL )
*error_r = SIEVE_ERROR_NOT_FOUND;
break;
case EACCES:
sieve_sys_error(svinst, "failed to open sieve dir: %s",
eacces_error_get("stat", path));
if ( error_r != NULL )
*error_r = SIEVE_ERROR_NO_PERMISSION;
break;
default:
sieve_sys_error(svinst, "failed to open sieve dir: stat(%s) failed: %m",
path);
if ( error_r != NULL )
*error_r = SIEVE_ERROR_TEMP_FAILURE;
break;
}
return NULL;
}
if ( S_ISDIR(st.st_mode) ) {
/* Open the directory */
if ( (dirp = opendir(path)) == NULL ) {
switch ( errno ) {
case ENOENT:
if ( error_r != NULL )
*error_r = SIEVE_ERROR_NOT_FOUND;
break;
case EACCES:
sieve_sys_error(svinst, "failed to open sieve dir: %s",
eacces_error_get("opendir", path));
if ( error_r != NULL )
*error_r = SIEVE_ERROR_NO_PERMISSION;
break;
default:
sieve_sys_error(svinst, "failed to open sieve dir: opendir(%s) failed: "
"%m", path);
if ( error_r != NULL )
*error_r = SIEVE_ERROR_TEMP_FAILURE;
break;
}
return NULL;
}
/* Create object */
sdir = t_new(struct sieve_directory, 1);
sdir->path = path;
sdir->dirp = dirp;
} else {
sdir = t_new(struct sieve_directory, 1);
sdir->path = path;
sdir->dirp = NULL;
}
sdir->svinst = svinst;
return sdir;
}
const char *sieve_directory_get_scriptfile(struct sieve_directory *sdir)
{
const char *script = NULL;
struct dirent *dp;
if ( sdir->dirp != NULL ) {
while ( script == NULL ) {
const char *file;
struct stat st;
errno = 0;
if ( (dp = readdir(sdir->dirp)) == NULL ) {
if ( errno != 0 ) {
sieve_sys_error(sdir->svinst, "failed to read sieve dir: "
"readdir(%s) failed: %m", sdir->path);
}
return NULL;
}
if ( !sieve_scriptfile_has_extension(dp->d_name) )
continue;
if ( sdir->path[strlen(sdir->path)-1] == '/' )
file = t_strconcat(sdir->path, dp->d_name, NULL);
else
file = t_strconcat(sdir->path, "/", dp->d_name, NULL);
if ( stat(file, &st) != 0 || !S_ISREG(st.st_mode) )
continue;
script = file;
}
} else {
script = sdir->path;
sdir->path = NULL;
}
return script;
}
void sieve_directory_close(struct sieve_directory **sdir)
{
/* Close the directory */
if ( (*sdir)->dirp != NULL && closedir((*sdir)->dirp) < 0 )
sieve_sys_error((*sdir)->svinst, "failed to close sieve dir: "
"closedir(%s) failed: %m", (*sdir)->path);
*sdir = NULL;
}
|