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
|
/* Copyright (c) 2002-2016 Pigeonhole authors, see the included COPYING file
*/
#include "lib.h"
#include "abspath.h"
#include "home-expand.h"
#include "ioloop.h"
#include "mkdir-parents.h"
#include "eacces-error.h"
#include "unlink-old-files.h"
#include "mail-storage-private.h"
#include "realpath.h"
#include "sieve.h"
#include "sieve-common.h"
#include "sieve-settings.h"
#include "sieve-error-private.h"
#include "sieve-settings.h"
#include "sieve-file-storage.h"
#include <stdio.h>
#include <unistd.h>
#include <ctype.h>
#include <utime.h>
#include <sys/time.h>
#define MAX_DIR_CREATE_MODE 0770
/*
* Utility
*/
const char *sieve_file_storage_path_extend
(struct sieve_file_storage *fstorage, const char *filename)
{
const char *path = fstorage->path;
if ( path[strlen(path)-1] == '/' )
return t_strconcat(path, filename, NULL);
return t_strconcat(path, "/", filename , NULL);
}
/*
*
*/
static int sieve_file_storage_stat
(struct sieve_file_storage *fstorage, const char *path,
enum sieve_error *error_r)
{
struct sieve_storage *storage = &fstorage->storage;
struct stat st;
if ( lstat(path, &st) == 0 ) {
fstorage->lnk_st = st;
if ( !S_ISLNK(st.st_mode) || stat(path, &st) == 0 ) {
fstorage->st = st;
return 0;
}
}
switch ( errno ) {
case ENOENT:
sieve_storage_sys_debug(storage,
"Storage path `%s' not found", t_abspath(path));
sieve_storage_set_internal_error(storage); // should be overriden
*error_r = SIEVE_ERROR_NOT_FOUND;
break;
case EACCES:
sieve_storage_set_critical(storage,
"Failed to stat sieve storage path: %s",
eacces_error_get("stat", path));
*error_r = SIEVE_ERROR_NO_PERMISSION;
break;
default:
sieve_storage_set_critical(storage,
"Failed to stat sieve storage path: "
"stat(%s) failed: %m", path);
*error_r = SIEVE_ERROR_TEMP_FAILURE;
break;
}
return -1;
}
static const char *sieve_storage_get_relative_link_path
(const char *active_path, const char *storage_dir)
{
const char *link_path, *p;
size_t pathlen;
/* Determine to what extent the sieve storage and active script
* paths match up. This enables the managed symlink to be short and the
* sieve storages can be moved around without trouble (if the active
* script path is common to the script storage).
*/
p = strrchr(active_path, '/');
if ( p == NULL ) {
link_path = storage_dir;
} else {
pathlen = p - active_path;
if ( strncmp( active_path, storage_dir, pathlen ) == 0 &&
(storage_dir[pathlen] == '/' || storage_dir[pathlen] == '\0') )
{
if ( storage_dir[pathlen] == '\0' )
link_path = "";
else
link_path = storage_dir + pathlen + 1;
} else
link_path = storage_dir;
}
/* Add trailing '/' when link path is not empty
*/
pathlen = strlen(link_path);
if ( pathlen != 0 && link_path[pathlen-1] != '/')
return t_strconcat(link_path, "/", NULL);
return t_strdup(link_path);
}
static mode_t get_dir_mode(mode_t mode)
{
/* Add the execute bit if either read or write bit is set */
if ((mode & 0600) != 0) mode |= 0100;
if ((mode & 0060) != 0) mode |= 0010;
if ((mode & 0006) != 0) mode |= 0001;
return mode;
}
static int mkdir_verify
(struct sieve_storage *storage, const char *dir,
mode_t mode, gid_t gid, const char *gid_origin)
{
struct stat st;
if ( stat(dir, &st) == 0 )
return 0;
if ( errno == EACCES ) {
sieve_storage_sys_error(storage,
"mkdir_verify: %s", eacces_error_get("stat", dir));
return -1;
} else if ( errno != ENOENT ) {
sieve_storage_sys_error(storage,
"mkdir_verify: stat(%s) failed: %m", dir);
return -1;
}
if ( mkdir_parents_chgrp(dir, mode, gid, gid_origin) == 0 ) {
sieve_storage_sys_debug(storage,
"Created storage directory %s", dir);
return 0;
}
switch ( errno ) {
case EEXIST:
return 0;
case ENOENT:
sieve_storage_sys_error(storage,
"Storage was deleted while it was being created");
break;
case EACCES:
sieve_storage_sys_error(storage,
"%s", eacces_error_get_creating("mkdir_parents_chgrp", dir));
break;
default:
sieve_storage_sys_error(storage,
"mkdir_parents_chgrp(%s) failed: %m", dir);
break;
}
return -1;
}
static int check_tmp(struct sieve_storage *storage, const char *path)
{
struct stat st;
/* If tmp/ directory exists, we need to clean it up once in a while */
if ( stat(path, &st) < 0 ) {
if ( errno == ENOENT )
return 0;
if ( errno == EACCES ) {
sieve_storage_sys_error(storage,
"check_tmp: %s", eacces_error_get("stat", path));
return -1;
}
sieve_storage_sys_error(storage,
"check_tmp: stat(%s) failed: %m", path);
return -1;
}
if ( st.st_atime > st.st_ctime + SIEVE_FILE_STORAGE_TMP_DELETE_SECS ) {
/* The directory should be empty. we won't do anything
until ctime changes. */
} else if ( st.st_atime < ioloop_time - SIEVE_FILE_STORAGE_TMP_SCAN_SECS ) {
/* Time to scan */
(void)unlink_old_files(path, "",
ioloop_time - SIEVE_FILE_STORAGE_TMP_DELETE_SECS);
}
return 1;
}
static struct sieve_storage *sieve_file_storage_alloc(void)
{
struct sieve_file_storage *fstorage;
pool_t pool;
pool = pool_alloconly_create("sieve_file_storage", 1024);
fstorage = p_new(pool, struct sieve_file_storage, 1);
fstorage->storage = sieve_file_storage;
fstorage->storage.pool = pool;
return &fstorage->storage;
}
static int sieve_file_storage_get_full_path
(struct sieve_file_storage *fstorage, const char **storage_path,
enum sieve_error *error_r)
{
struct sieve_storage *storage = &fstorage->storage;
struct sieve_instance *svinst = storage->svinst;
const char *path = *storage_path;
/* Get full storage path */
if ( path != NULL &&
((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_storage_set_critical(storage,
"Sieve storage path `%s' is relative to home directory, "
"but home directory is not available.", path);
*error_r = SIEVE_ERROR_TEMP_FAILURE;
return -1;
}
}
*storage_path = path;
return 0;
}
static int sieve_file_storage_get_full_active_path
(struct sieve_file_storage *fstorage, const char **active_path,
enum sieve_error *error_r)
{
struct sieve_storage *storage = &fstorage->storage;
struct sieve_instance *svinst = storage->svinst;
const char *path = *active_path;
if ( path != NULL && *path != '\0' &&
((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_storage_set_critical(storage,
"Sieve storage active script path `%s' is relative to home directory, "
"but home directory is not available.", path);
*error_r = SIEVE_ERROR_TEMP_FAILURE;
return -1;
}
}
*active_path = path;
return 0;
}
static int sieve_file_storage_init_common
(struct sieve_file_storage *fstorage, const char *active_path,
const char *storage_path, bool exists, enum sieve_error *error_r)
ATTR_NULL(2, 3)
{
struct sieve_storage *storage = &fstorage->storage;
const char *tmp_dir, *link_path, *active_fname, *storage_dir;
bool have_link = FALSE;
int ret;
i_assert( storage_path != NULL || active_path != NULL );
fstorage->prev_mtime = (time_t)-1;
/* Get active script path */
if ( sieve_file_storage_get_full_active_path
(fstorage, &active_path, error_r) < 0 )
return -1;
/* Get the filename for the active script link */
active_fname = NULL;
if ( active_path != NULL && *active_path != '\0' ) {
const char *active_dir;
active_fname = strrchr(active_path, '/');
if ( active_fname == NULL ) {
active_fname = active_path;
active_dir = "";
} else {
active_dir = t_strdup_until(active_path, active_fname);
active_fname++;
}
if ( *active_fname == '\0' ) {
/* Link cannot be just a path ending in '/' */
sieve_storage_set_critical(storage,
"Path to %sscript must include the filename (path=%s)",
( storage_path != NULL ? "active link/" : "" ),
active_path);
*error_r = SIEVE_ERROR_TEMP_FAILURE;
return -1;
}
if (t_realpath(active_dir, &active_dir) < 0) {
if (errno != ENOENT) {
sieve_storage_sys_error(storage,
"Failed to normalize active script directory (path=%s): %m",
active_dir);
*error_r = SIEVE_ERROR_TEMP_FAILURE;
return -1;
}
sieve_storage_sys_debug(storage,
"Failed to normalize active script directory (path=%s): "
"Part of the path does not exist (yet)",
active_dir);
} else {
active_path = t_abspath_to(active_fname, active_dir);
}
sieve_storage_sys_debug(storage,
"Using %sSieve script path: %s",
( storage_path != NULL ? "active " : "" ),
active_path);
fstorage->active_path = p_strdup(storage->pool, active_path);
fstorage->active_fname = p_strdup(storage->pool, active_fname);
}
/* Determine storage path */
storage_dir = storage_path;
if ( storage_path != NULL && *storage_path != '\0' ) {
sieve_storage_sys_debug(storage,
"Using script storage path: %s", storage_path);
have_link = TRUE;
} else {
if ((storage->flags & SIEVE_STORAGE_FLAG_READWRITE) != 0 ) {
sieve_storage_set_critical(storage,
"Storage path cannot be empty for write access");
*error_r = SIEVE_ERROR_TEMP_FAILURE;
return -1;
}
storage_path = active_path;
}
i_assert(storage_path != NULL);
/* Prepare for write access */
if ( (storage->flags & SIEVE_STORAGE_FLAG_READWRITE) != 0 ) {
mode_t dir_create_mode, file_create_mode;
gid_t file_create_gid;
const char *file_create_gid_origin;
/* Use safe permission defaults */
file_create_mode = 0600;
dir_create_mode = 0700;
file_create_gid = (gid_t)-1;
file_create_gid_origin = "defaults";
/* Get actual permissions */
if ( exists ) {
file_create_mode = (fstorage->st.st_mode & 0666) | 0600;
dir_create_mode = (fstorage->st.st_mode & 0777) | 0700;
file_create_gid_origin = storage_dir;
if ( !S_ISDIR(fstorage->st.st_mode) ) {
/* We're getting permissions from a file.
Apply +x modes as necessary. */
dir_create_mode = get_dir_mode(dir_create_mode);
}
if (S_ISDIR(fstorage->st.st_mode) &&
(fstorage->st.st_mode & S_ISGID) != 0) {
/* Directory's GID is used automatically for new files */
file_create_gid = (gid_t)-1;
} else if ((fstorage->st.st_mode & 0070) >> 3 ==
(fstorage->st.st_mode & 0007)) {
/* Group has same permissions as world, so don't bother changing it */
file_create_gid = (gid_t)-1;
} else if (getegid() == fstorage->st.st_gid) {
/* Using our own gid, no need to change it */
file_create_gid = (gid_t)-1;
} else {
file_create_gid = fstorage->st.st_gid;
}
}
sieve_storage_sys_debug(storage,
"Using permissions from %s: mode=0%o gid=%ld",
file_create_gid_origin, (int)dir_create_mode,
file_create_gid == (gid_t)-1 ? -1L : (long)file_create_gid);
/*
* Ensure sieve local directory structure exists (full autocreate):
* This currently only consists of a ./tmp direcory
*/
tmp_dir = t_strconcat(storage_path, "/tmp", NULL);
/* Try to find and clean up tmp dir */
if ( (ret=check_tmp(storage, tmp_dir)) < 0 ) {
*error_r = SIEVE_ERROR_TEMP_FAILURE;
return -1;
}
/* Auto-create if necessary */
if ( ret == 0 && mkdir_verify(storage, tmp_dir,
dir_create_mode, file_create_gid, file_create_gid_origin) < 0 ) {
*error_r = SIEVE_ERROR_TEMP_FAILURE;
return -1;
}
fstorage->dir_create_mode = dir_create_mode;
fstorage->file_create_mode = file_create_mode;
fstorage->file_create_gid = file_create_gid;
}
if ( !exists && sieve_file_storage_stat
(fstorage, storage_path, error_r) < 0 )
return -1;
if ( have_link ) {
if ( t_realpath(storage_path, &storage_path) < 0 ) {
sieve_storage_sys_error(storage,
"Failed to normalize storage path (path=%s): %m",
storage_path);
*error_r = SIEVE_ERROR_TEMP_FAILURE;
return -1;
}
if ( active_path != NULL && *active_path != '\0' ) {
/* Get the path to be prefixed to the script name in the symlink
* pointing to the active script.
*/
link_path = sieve_storage_get_relative_link_path
(fstorage->active_path, storage_path);
sieve_storage_sys_debug(storage,
"Relative path to sieve storage in active link: %s",
link_path);
fstorage->link_path = p_strdup(storage->pool, link_path);
}
}
fstorage->path = p_strdup(storage->pool, storage_path);
storage->location = fstorage->path;
return 0;
}
static int sieve_file_storage_init
(struct sieve_storage *storage, const char *const *options,
enum sieve_error *error_r)
{
struct sieve_file_storage *fstorage =
(struct sieve_file_storage *)storage;
const char *storage_path = storage->location;
const char *active_path = "";
bool exists = FALSE;
if ( options != NULL ) {
while ( *options != NULL ) {
const char *option = *options;
if ( strncasecmp(option, "active=", 7) == 0 && option[7] != '\0' ) {
active_path = option+7;
} else {
sieve_storage_set_critical(storage,
"Invalid option `%s'", option);
*error_r = SIEVE_ERROR_TEMP_FAILURE;
return -1;
}
options++;
}
}
/* Get full storage path */
if ( sieve_file_storage_get_full_path
(fstorage, &storage_path, error_r) < 0 )
return -1;
/* Stat storage directory */
if ( storage_path != NULL && *storage_path != '\0' ) {
if ( sieve_file_storage_stat(fstorage, storage_path, error_r) < 0 ) {
if ( *error_r != SIEVE_ERROR_NOT_FOUND )
return -1;
if ( (storage->flags & SIEVE_STORAGE_FLAG_READWRITE) == 0 ) {
/* For backwards compatibility, recognize when storage directory
does not exist while active script exists and is a regular
file. */
if ( active_path == NULL || *active_path == '\0' )
return -1;
if ( sieve_file_storage_get_full_active_path
(fstorage, &active_path, error_r) < 0 )
return -1;
if ( sieve_file_storage_stat
(fstorage, active_path, error_r) < 0 )
return -1;
if ( !S_ISREG(fstorage->lnk_st.st_mode) )
return -1;
sieve_storage_sys_debug(storage,
"Sieve storage path `%s' not found, "
"but the active script `%s' is a regular file, "
"so this is used for backwards compatibility.",
storage_path, active_path);
storage_path = NULL;
}
} else {
exists = TRUE;
if ( !S_ISDIR(fstorage->st.st_mode) ) {
if ( (storage->flags & SIEVE_STORAGE_FLAG_READWRITE) != 0 ) {
sieve_storage_set_critical(storage,
"Sieve storage path `%s' is not a directory, "
"but it is to be opened for write access", storage_path);
*error_r = SIEVE_ERROR_TEMP_FAILURE;
return -1;
}
if ( active_path != NULL && *active_path != '\0' ) {
sieve_storage_sys_warning(storage,
"Explicitly specified active script path `%s' is ignored; "
"storage path `%s' is not a directory",
active_path, storage_path);
}
active_path = storage_path;
storage_path = NULL;
}
}
}
if ( active_path == NULL || *active_path == '\0' ) {
if ( storage->main_storage ||
(storage->flags & SIEVE_STORAGE_FLAG_READWRITE) != 0) {
sieve_storage_sys_debug(storage,
"Active script path is unconfigured; "
"using default (path=%s)", SIEVE_FILE_DEFAULT_PATH);
active_path = SIEVE_FILE_DEFAULT_PATH;
}
}
return sieve_file_storage_init_common
(fstorage, active_path, storage_path, exists, error_r);
}
static void sieve_file_storage_autodetect
(struct sieve_file_storage *fstorage, const char **storage_path_r)
{
struct sieve_storage *storage = &fstorage->storage;
struct sieve_instance *svinst = storage->svinst;
const char *home = sieve_environment_get_homedir(svinst);
int mode = ( (storage->flags & SIEVE_STORAGE_FLAG_READWRITE) != 0 ?
R_OK|W_OK|X_OK : R_OK|X_OK );
sieve_storage_sys_debug(storage,
"Performing auto-detection");
/* We'll need to figure out the storage location ourself.
*
* It's $HOME/sieve or /sieve when (presumed to be) chrooted.
*/
if ( home != NULL && *home != '\0' ) {
if (access(home, mode) == 0) {
/* Use default ~/sieve */
sieve_storage_sys_debug(storage,
"Root exists (%s)", home);
*storage_path_r = t_strconcat(home, "/sieve", NULL);
} else {
/* Don't have required access on the home directory */
sieve_storage_sys_debug(storage,
"access(%s, rwx) failed: %m", home);
}
} else {
sieve_storage_sys_debug(storage,
"HOME is not set");
if (access("/sieve", mode) == 0) {
*storage_path_r = "/sieve";
sieve_storage_sys_debug(storage,
"Directory `/sieve' exists, assuming chroot");
}
}
}
static int sieve_file_storage_do_init_legacy
(struct sieve_file_storage *fstorage, const char *active_path,
const char *storage_path, enum sieve_error *error_r)
{
struct sieve_storage *storage = &fstorage->storage;
bool explicit = FALSE, exists = FALSE;
if ( storage_path == NULL || *storage_path == '\0' ) {
/* Try autodectection */
sieve_file_storage_autodetect(fstorage, &storage_path);
if ( storage_path != NULL && *storage_path != '\0') {
/* Got something: stat it */
if (sieve_file_storage_stat
(fstorage, storage_path, error_r) < 0 ) {
if (*error_r != SIEVE_ERROR_NOT_FOUND) {
/* Error */
return -1;
}
} else if ( S_ISDIR(fstorage->st.st_mode) ) {
/* Success */
exists = TRUE;
}
}
if ( (storage_path == NULL || *storage_path == '\0') &&
(storage->flags & SIEVE_STORAGE_FLAG_READWRITE) != 0 ) {
sieve_storage_set_critical(storage,
"Could not find storage root directory for write access; "
"path was left unconfigured and autodetection failed");
*error_r = SIEVE_ERROR_TEMP_FAILURE;
return -1;
}
} else {
/* Get full storage path */
if ( sieve_file_storage_get_full_path
(fstorage, &storage_path, error_r) < 0 )
return -1;
/* Stat storage directory */
if ( sieve_file_storage_stat(fstorage, storage_path, error_r) < 0 ) {
if ( (*error_r != SIEVE_ERROR_NOT_FOUND) )
return -1;
if ( (storage->flags & SIEVE_STORAGE_FLAG_READWRITE) == 0 )
storage_path = NULL;
} else {
exists = TRUE;
}
/* Storage path must be a directory */
if ( exists && !S_ISDIR(fstorage->st.st_mode) ) {
sieve_storage_set_critical(storage,
"Sieve storage path `%s' configured using sieve_dir "
"is not a directory", storage_path);
*error_r = SIEVE_ERROR_TEMP_FAILURE;
return -1;
}
explicit = TRUE;
}
if ( (active_path == NULL || *active_path == '\0') ) {
if ( storage->main_storage ||
(storage->flags & SIEVE_STORAGE_FLAG_READWRITE) != 0) {
sieve_storage_sys_debug(storage,
"Active script path is unconfigured; "
"using default (path=%s)", SIEVE_FILE_DEFAULT_PATH);
active_path = SIEVE_FILE_DEFAULT_PATH;
} else {
return -1;
}
}
if ( !explicit && !exists &&
active_path != NULL && *active_path != '\0' &&
(storage->flags & SIEVE_STORAGE_FLAG_READWRITE) == 0 )
storage_path = NULL;
if ( sieve_file_storage_init_common
(fstorage, active_path, storage_path, exists, error_r) < 0 )
return -1;
return 0;
}
struct sieve_storage *sieve_file_storage_init_legacy
(struct sieve_instance *svinst, const char *active_path,
const char *storage_path, enum sieve_storage_flags flags,
enum sieve_error *error_r)
{
struct sieve_storage *storage;
struct sieve_file_storage *fstorage;
storage = sieve_storage_alloc
(svinst, &sieve_file_storage, "", flags, TRUE);
fstorage = (struct sieve_file_storage *)storage;
T_BEGIN {
if ( sieve_file_storage_do_init_legacy
(fstorage, active_path, storage_path, error_r) < 0 ) {
sieve_storage_unref(&storage);
storage = NULL;
}
} T_END;
return storage;
}
struct sieve_file_storage *sieve_file_storage_init_from_path
(struct sieve_instance *svinst, const char *path,
enum sieve_storage_flags flags, enum sieve_error *error_r)
{
struct sieve_storage *storage;
struct sieve_file_storage *fstorage;
i_assert( path != NULL );
storage = sieve_storage_alloc
(svinst, &sieve_file_storage, "", flags, FALSE);
fstorage = (struct sieve_file_storage *)storage;
T_BEGIN {
if ( sieve_file_storage_init_common
(fstorage, path, NULL, FALSE, error_r) < 0 ) {
sieve_storage_unref(&storage);
fstorage = NULL;
}
} T_END;
return fstorage;
}
static int sieve_file_storage_is_singular
(struct sieve_storage *storage)
{
struct sieve_file_storage *fstorage =
(struct sieve_file_storage *)storage;
struct stat st;
if ( fstorage->active_path == NULL )
return 1;
/* Stat the file */
if ( lstat(fstorage->active_path, &st) != 0 ) {
if ( errno != ENOENT ) {
sieve_storage_set_critical(storage,
"Failed to stat active sieve script symlink (%s): %m.",
fstorage->active_path);
return -1;
}
return 0;
}
if ( S_ISLNK( st.st_mode ) )
return 0;
if ( !S_ISREG( st.st_mode ) ) {
sieve_storage_set_critical(storage,
"Active sieve script file '%s' is no symlink nor a regular file.",
fstorage->active_path);
return -1;
}
return 1;
}
/*
*
*/
static int sieve_file_storage_get_last_change
(struct sieve_storage *storage, time_t *last_change_r)
{
struct sieve_file_storage *fstorage =
(struct sieve_file_storage *)storage;
struct stat st;
if ( fstorage->prev_mtime == (time_t)-1 ) {
/* Get the storage mtime before we modify it ourself */
if ( stat(fstorage->path, &st) < 0 ) {
if ( errno != ENOENT ) {
sieve_storage_sys_error(storage,
"stat(%s) failed: %m", fstorage->path);
return -1;
}
st.st_mtime = 0;
}
fstorage->prev_mtime = st.st_mtime;
}
if ( last_change_r != NULL )
*last_change_r = fstorage->prev_mtime;
return 0;
}
int sieve_file_storage_pre_modify
(struct sieve_storage *storage)
{
i_assert( (storage->flags & SIEVE_STORAGE_FLAG_READWRITE) != 0 );
return sieve_storage_get_last_change(storage, NULL);
}
static void sieve_file_storage_set_modified
(struct sieve_storage *storage, time_t mtime)
{
struct sieve_file_storage *fstorage =
(struct sieve_file_storage *)storage;
struct utimbuf times;
time_t cur_mtime;
if ( mtime != (time_t)-1 ) {
if ( sieve_storage_get_last_change(storage, &cur_mtime) >= 0 &&
cur_mtime > mtime )
return;
} else {
mtime = ioloop_time;
}
times.actime = mtime;
times.modtime = mtime;
if ( utime(fstorage->path, ×) < 0 ) {
switch ( errno ) {
case ENOENT:
break;
case EACCES:
sieve_storage_sys_error(storage,
"%s", eacces_error_get("utime", fstorage->path));
break;
default:
sieve_storage_sys_error(storage,
"utime(%s) failed: %m", fstorage->path);
}
} else {
fstorage->prev_mtime = mtime;
}
}
/*
* Script access
*/
static struct sieve_script *sieve_file_storage_get_script
(struct sieve_storage *storage, const char *name)
{
struct sieve_file_storage *fstorage =
(struct sieve_file_storage *)storage;
struct sieve_file_script *fscript;
T_BEGIN {
fscript = sieve_file_script_init_from_name(fstorage, name);
} T_END;
return &fscript->script;
}
/*
* Driver definition
*/
const struct sieve_storage sieve_file_storage = {
.driver_name = SIEVE_FILE_STORAGE_DRIVER_NAME,
.version = 0,
.allows_synchronization = TRUE,
.v = {
.alloc = sieve_file_storage_alloc,
.init = sieve_file_storage_init,
.get_last_change = sieve_file_storage_get_last_change,
.set_modified = sieve_file_storage_set_modified,
.is_singular = sieve_file_storage_is_singular,
.get_script = sieve_file_storage_get_script,
.get_script_sequence = sieve_file_storage_get_script_sequence,
.script_sequence_next = sieve_file_script_sequence_next,
.script_sequence_destroy = sieve_file_script_sequence_destroy,
.active_script_get_name = sieve_file_storage_active_script_get_name,
.active_script_open = sieve_file_storage_active_script_open,
.deactivate = sieve_file_storage_deactivate,
.active_script_get_last_change =
sieve_file_storage_active_script_get_last_change,
.list_init = sieve_file_storage_list_init,
.list_next = sieve_file_storage_list_next,
.list_deinit = sieve_file_storage_list_deinit,
.save_init = sieve_file_storage_save_init,
.save_continue = sieve_file_storage_save_continue,
.save_finish = sieve_file_storage_save_finish,
.save_get_tempscript = sieve_file_storage_save_get_tempscript,
.save_cancel = sieve_file_storage_save_cancel,
.save_commit = sieve_file_storage_save_commit,
.save_as = sieve_file_storage_save_as,
.save_as_active = sieve_file_storage_save_as_active,
.quota_havespace = sieve_file_storage_quota_havespace
}
};
|