|
Line 0
Link Here
|
|
|
1 |
#include <stdio.h> |
| 2 |
#include <stdlib.h> |
| 3 |
#include <string.h> |
| 4 |
|
| 5 |
#ifdef HAVE_UNISTD_H |
| 6 |
#include <unistd.h> |
| 7 |
#endif |
| 8 |
|
| 9 |
#include <errno.h> |
| 10 |
#include <sys/types.h> |
| 11 |
#include <sys/stat.h> |
| 12 |
#include <sys/uio.h> |
| 13 |
#include <fcntl.h> |
| 14 |
#include <ctype.h> |
| 15 |
#include <time.h> |
| 16 |
#include <syslog.h> |
| 17 |
#include <com_err.h> |
| 18 |
#include <config.h> |
| 19 |
|
| 20 |
#include "global.h" |
| 21 |
#include "util.h" |
| 22 |
#include "xmalloc.h" |
| 23 |
#include "xstrlcpy.h" |
| 24 |
#include "xstrlcat.h" |
| 25 |
#include "mailbox.h" |
| 26 |
#include "imap_err.h" |
| 27 |
#include "sieve_interface.h" |
| 28 |
#include "script.h" |
| 29 |
|
| 30 |
#define TIMSIEVE_FAIL -1 |
| 31 |
#define TIMSIEVE_OK 0 |
| 32 |
#define MAX_FILENAME 1024 |
| 33 |
|
| 34 |
static int get_script_name(char *sievename, size_t buflen, const char *filename); |
| 35 |
static int get_script_dir(char *sieve_script_dir, size_t buflen, char *userid, const char *sieve_dir); |
| 36 |
int autoadd_sieve(char *userid, const char *source_script); |
| 37 |
|
| 38 |
//static void fatal(const char *s, int code); |
| 39 |
static void foo(void); |
| 40 |
static int sieve_notify(void *ac __attribute__((unused)), |
| 41 |
void *interp_context __attribute__((unused)), |
| 42 |
void *script_context __attribute__((unused)), |
| 43 |
void *message_context __attribute__((unused)), |
| 44 |
const char **errmsg __attribute__((unused))); |
| 45 |
static int mysieve_error(int lineno, const char *msg, |
| 46 |
void *i __attribute__((unused)), void *s); |
| 47 |
static int is_script_parsable(FILE *stream, char **errstr, sieve_script_t **ret); |
| 48 |
|
| 49 |
|
| 50 |
sieve_vacation_t vacation2 = { |
| 51 |
0, /* min response */ |
| 52 |
0, /* max response */ |
| 53 |
(sieve_callback *) &foo, /* autorespond() */ |
| 54 |
(sieve_callback *) &foo /* send_response() */ |
| 55 |
}; |
| 56 |
|
| 57 |
|
| 58 |
/* |
| 59 |
* Find the name of the sieve script |
| 60 |
* given the source script and compiled script names |
| 61 |
*/ |
| 62 |
static int get_script_name(char *sievename, size_t buflen, const char *filename) |
| 63 |
{ |
| 64 |
char *p; |
| 65 |
int r; |
| 66 |
|
| 67 |
p = strrchr(filename, '/'); |
| 68 |
if (p == NULL) |
| 69 |
p = (char *) filename; |
| 70 |
else |
| 71 |
p++; |
| 72 |
|
| 73 |
r = strlcpy(sievename, p, buflen) - buflen; |
| 74 |
return (r >= 0 || r == -buflen ? 1 : 0); |
| 75 |
} |
| 76 |
|
| 77 |
|
| 78 |
/* |
| 79 |
* Find the directory where the sieve scripts of the user |
| 80 |
* reside |
| 81 |
*/ |
| 82 |
static int get_script_dir(char *sieve_script_dir, size_t buflen, char *userid, const char *sieve_dir) |
| 83 |
{ |
| 84 |
char *user = NULL, *domain = NULL; |
| 85 |
|
| 86 |
/* Setup the user and the domain */ |
| 87 |
if(config_virtdomains && (domain = strchr(userid, '@'))) { |
| 88 |
user = (char *) xmalloc((domain - userid +1) * sizeof(char)); |
| 89 |
strlcpy(user, userid, domain - userid + 1); |
| 90 |
domain++; |
| 91 |
} else |
| 92 |
user = userid; |
| 93 |
|
| 94 |
/* Find the dir path where the sieve scripts of the user will reside */ |
| 95 |
if (config_virtdomains && domain) { |
| 96 |
if(snprintf(sieve_script_dir, buflen, "%s%s%c/%s/%c/%s/", |
| 97 |
sieve_dir, FNAME_DOMAINDIR, dir_hash_c(domain, config_fulldirhash), domain, dir_hash_c(user,config_fulldirhash), user) >= buflen) { |
| 98 |
free(user); |
| 99 |
return 1; |
| 100 |
} |
| 101 |
} else { |
| 102 |
if(snprintf(sieve_script_dir, buflen, "%s/%c/%s/", |
| 103 |
sieve_dir, dir_hash_c(user,config_fulldirhash), user) >= buflen) |
| 104 |
return 1; |
| 105 |
} |
| 106 |
|
| 107 |
/* Free the xmalloced user memory, reserved above */ |
| 108 |
if(user != userid) |
| 109 |
free(user); |
| 110 |
|
| 111 |
return 0; |
| 112 |
} |
| 113 |
|
| 114 |
int autoadd_sieve(char *userid, const char *source_script) |
| 115 |
{ |
| 116 |
sieve_script_t *s = NULL; |
| 117 |
bytecode_info_t *bc = NULL; |
| 118 |
char *err = NULL; |
| 119 |
FILE *in_stream, *out_fp; |
| 120 |
int out_fd, in_fd, r, k; |
| 121 |
int do_compile = 0; |
| 122 |
const char *sieve_dir = NULL; |
| 123 |
const char *compiled_source_script = NULL; |
| 124 |
char sievename[MAX_FILENAME]; |
| 125 |
char sieve_script_name[MAX_FILENAME]; |
| 126 |
char sieve_script_dir[MAX_FILENAME]; |
| 127 |
char sieve_bcscript_name[MAX_FILENAME]; |
| 128 |
char sieve_default[MAX_FILENAME]; |
| 129 |
char sieve_tmpname[MAX_FILENAME]; |
| 130 |
char sieve_bctmpname[MAX_FILENAME]; |
| 131 |
char sieve_bclink_name[MAX_FILENAME]; |
| 132 |
char buf[4096]; |
| 133 |
mode_t oldmask; |
| 134 |
struct stat statbuf; |
| 135 |
|
| 136 |
/* We don't support using the homedirectory, like timsieved */ |
| 137 |
if (config_getswitch(IMAPOPT_SIEVEUSEHOMEDIR)) { |
| 138 |
syslog(LOG_WARNING,"autocreate_sieve: autocreate_sieve does not work with sieveusehomedir option in imapd.conf"); |
| 139 |
return 1; |
| 140 |
} |
| 141 |
|
| 142 |
/* Check if sievedir is defined in imapd.conf */ |
| 143 |
if(!(sieve_dir = config_getstring(IMAPOPT_SIEVEDIR))) { |
| 144 |
syslog(LOG_WARNING, "autocreate_sieve: sievedir option is not defined. Check imapd.conf"); |
| 145 |
return 1; |
| 146 |
} |
| 147 |
|
| 148 |
/* Check if autocreate_sieve_compiledscript is defined in imapd.conf */ |
| 149 |
if(!(compiled_source_script = config_getstring(IMAPOPT_AUTOCREATE_SIEVE_COMPILEDSCRIPT))) { |
| 150 |
syslog(LOG_WARNING, "autocreate_sieve: autocreate_sieve_compiledscript option is not defined. Compiling it"); |
| 151 |
do_compile = 1; |
| 152 |
} |
| 153 |
|
| 154 |
if(get_script_dir(sieve_script_dir, sizeof(sieve_script_dir), userid, sieve_dir)) { |
| 155 |
syslog(LOG_WARNING, "autocreate_sieve: Cannot find sieve scripts directory"); |
| 156 |
return 1; |
| 157 |
} |
| 158 |
|
| 159 |
if (get_script_name(sievename, sizeof(sievename), source_script)) { |
| 160 |
syslog(LOG_WARNING, "autocreate_sieve: Invalid sieve script %s", source_script); |
| 161 |
return 1; |
| 162 |
} |
| 163 |
|
| 164 |
if(snprintf(sieve_tmpname, sizeof(sieve_tmpname), "%s%s.script.NEW",sieve_script_dir, sievename) >= sizeof(sieve_tmpname)) { |
| 165 |
syslog(LOG_WARNING, "autocreate_sieve: Invalid sieve path %s, %s, %s", sieve_dir, sievename, userid); |
| 166 |
return 1; |
| 167 |
} |
| 168 |
if(snprintf(sieve_bctmpname, sizeof(sieve_bctmpname), "%s%s.bc.NEW",sieve_script_dir, sievename) >= sizeof(sieve_bctmpname)) { |
| 169 |
syslog(LOG_WARNING, "autocreate_sieve: Invalid sieve path %s, %s, %s", sieve_dir, sievename, userid); |
| 170 |
return 1; |
| 171 |
} |
| 172 |
if(snprintf(sieve_script_name, sizeof(sieve_script_name), "%s%s.script",sieve_script_dir, sievename) >= sizeof(sieve_script_name)) { |
| 173 |
syslog(LOG_WARNING, "autocreate_sieve: Invalid sieve path %s, %s, %s", sieve_dir, sievename, userid); |
| 174 |
return 1; |
| 175 |
} |
| 176 |
if(snprintf(sieve_bcscript_name, sizeof(sieve_bcscript_name), "%s%s.bc",sieve_script_dir, sievename) >= sizeof(sieve_bcscript_name)) { |
| 177 |
syslog(LOG_WARNING, "autocreate_sieve: Invalid sieve path %s, %s, %s", sieve_dir, sievename, userid); |
| 178 |
return 1; |
| 179 |
} |
| 180 |
if(snprintf(sieve_default, sizeof(sieve_default), "%s%s",sieve_script_dir,"defaultbc") >= sizeof(sieve_default)) { |
| 181 |
syslog(LOG_WARNING, "autocreate_sieve: Invalid sieve path %s, %s, %s", sieve_dir, sievename, userid); |
| 182 |
return 1; |
| 183 |
} |
| 184 |
if(snprintf(sieve_bclink_name, sizeof(sieve_bclink_name), "%s.bc", sievename) >= sizeof(sieve_bclink_name)) { |
| 185 |
syslog(LOG_WARNING, "autocreate_sieve: Invalid sieve path %s, %s, %s", sieve_dir, sievename, userid); |
| 186 |
return 1; |
| 187 |
} |
| 188 |
|
| 189 |
/* Check if a default sieve filter alrady exists */ |
| 190 |
if(!stat(sieve_default,&statbuf)) { |
| 191 |
syslog(LOG_WARNING,"autocreate_sieve: Default sieve script already exists"); |
| 192 |
fclose(in_stream); |
| 193 |
return 1; |
| 194 |
} |
| 195 |
|
| 196 |
/* Open the source script. if there is a problem with that exit */ |
| 197 |
in_stream = fopen(source_script, "r"); |
| 198 |
if(!in_stream) { |
| 199 |
syslog(LOG_WARNING,"autocreate_sieve: Unable to open sieve script %s. Check permissions",source_script); |
| 200 |
return 1; |
| 201 |
} |
| 202 |
|
| 203 |
|
| 204 |
/* |
| 205 |
* At this point we start the modifications of the filesystem |
| 206 |
*/ |
| 207 |
|
| 208 |
/* Create the directory where the sieve scripts will reside */ |
| 209 |
r = cyrus_mkdir(sieve_script_dir, 0755); |
| 210 |
if(r == -1) { |
| 211 |
/* If this fails we just leave */ |
| 212 |
syslog(LOG_WARNING,"autocreate_sieve: Unable to create directory %s. Check permissions",sieve_script_name); |
| 213 |
return 1; |
| 214 |
} |
| 215 |
|
| 216 |
/* |
| 217 |
* We open the file that will be used as the bc file. If this file exists, overwrite it |
| 218 |
* since something bad has happened. We open the file here so that this error checking is |
| 219 |
* done before we try to open the rest of the files to start copying etc. |
| 220 |
*/ |
| 221 |
out_fd = open(sieve_bctmpname, O_CREAT|O_TRUNC|O_WRONLY, S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH); |
| 222 |
if(out_fd < 0) { |
| 223 |
if(errno == EEXIST) { |
| 224 |
syslog(LOG_WARNING,"autocreate_sieve: File %s already exists. Probaly left over. Ignoring",sieve_bctmpname); |
| 225 |
} else if (errno == EACCES) { |
| 226 |
syslog(LOG_WARNING,"autocreate_sieve: No access to create file %s. Check permissions",sieve_bctmpname); |
| 227 |
fclose(in_stream); |
| 228 |
return 1; |
| 229 |
} else { |
| 230 |
syslog(LOG_WARNING,"autocreate_sieve: Unable to create %s. Unknown error",sieve_bctmpname); |
| 231 |
fclose(in_stream); |
| 232 |
return 1; |
| 233 |
} |
| 234 |
} |
| 235 |
|
| 236 |
if(!do_compile && compiled_source_script && (in_fd = open(compiled_source_script, O_RDONLY)) != -1) { |
| 237 |
while((r = read(in_fd, buf, sizeof(buf))) > 0) { |
| 238 |
if((k=write(out_fd, buf,r)) < 0) { |
| 239 |
syslog(LOG_WARNING, "autocreate_sieve: Error writing to file: %s, error: %d", sieve_bctmpname, errno); |
| 240 |
close(out_fd); |
| 241 |
close(in_fd); |
| 242 |
fclose(in_stream); |
| 243 |
unlink(sieve_bctmpname); |
| 244 |
return 1; |
| 245 |
} |
| 246 |
} |
| 247 |
|
| 248 |
if(r == 0) { /* EOF */ |
| 249 |
close(out_fd); |
| 250 |
close(in_fd); |
| 251 |
} else if (r < 0) { |
| 252 |
syslog(LOG_WARNING, "autocreate_sieve: Error reading compiled script file: %s. Will try to compile it", |
| 253 |
compiled_source_script); |
| 254 |
close(in_fd); |
| 255 |
do_compile = 1; |
| 256 |
if(lseek(out_fd, 0, SEEK_SET)) { |
| 257 |
syslog(LOG_WARNING, "autocreate_sieve: Major IO problem. Aborting"); |
| 258 |
return 1; |
| 259 |
} |
| 260 |
} |
| 261 |
close(in_fd); |
| 262 |
} else { |
| 263 |
if(compiled_source_script) |
| 264 |
syslog(LOG_WARNING,"autocreate_sieve: Problem opening compiled script file: %s. Compiling it", compiled_source_script); |
| 265 |
do_compile = 1; |
| 266 |
} |
| 267 |
|
| 268 |
|
| 269 |
/* Because we failed to open a precompiled bc sieve script, we compile one */ |
| 270 |
if(do_compile) { |
| 271 |
if(is_script_parsable(in_stream,&err, &s) == TIMSIEVE_FAIL) { |
| 272 |
if(err && *err) { |
| 273 |
syslog(LOG_WARNING,"autocreate_sieve: Error while parsing script %s.",err); |
| 274 |
free(err); |
| 275 |
} else |
| 276 |
syslog(LOG_WARNING,"autocreate_sieve: Error while parsing script"); |
| 277 |
|
| 278 |
unlink(sieve_bctmpname); |
| 279 |
fclose(in_stream); |
| 280 |
close(out_fd); |
| 281 |
return 1; |
| 282 |
} |
| 283 |
|
| 284 |
/* generate the bytecode */ |
| 285 |
if(sieve_generate_bytecode(&bc, s) == TIMSIEVE_FAIL) { |
| 286 |
syslog(LOG_WARNING,"autocreate_sieve: problem compiling sieve script"); |
| 287 |
/* removing the copied script and cleaning up memory */ |
| 288 |
unlink(sieve_bctmpname); |
| 289 |
sieve_script_free(&s); |
| 290 |
fclose(in_stream); |
| 291 |
close(out_fd); |
| 292 |
return 1; |
| 293 |
} |
| 294 |
|
| 295 |
if(sieve_emit_bytecode(out_fd, bc) == TIMSIEVE_FAIL) { |
| 296 |
syslog(LOG_WARNING,"autocreate_sieve: problem emiting sieve script"); |
| 297 |
/* removing the copied script and cleaning up memory */ |
| 298 |
unlink(sieve_bctmpname); |
| 299 |
sieve_free_bytecode(&bc); |
| 300 |
sieve_script_free(&s); |
| 301 |
fclose(in_stream); |
| 302 |
close(out_fd); |
| 303 |
return 1; |
| 304 |
} |
| 305 |
|
| 306 |
/* clean up the memory */ |
| 307 |
sieve_free_bytecode(&bc); |
| 308 |
sieve_script_free(&s); |
| 309 |
} |
| 310 |
|
| 311 |
close(out_fd); |
| 312 |
rewind(in_stream); |
| 313 |
|
| 314 |
/* Copy the initial script */ |
| 315 |
oldmask = umask(077); |
| 316 |
if((out_fp = fopen(sieve_tmpname, "w")) == NULL) { |
| 317 |
syslog(LOG_WARNING,"autocreate_sieve: Unable to open %s destination sieve script", sieve_tmpname); |
| 318 |
unlink(sieve_bctmpname); |
| 319 |
umask(oldmask); |
| 320 |
fclose(in_stream); |
| 321 |
return 1; |
| 322 |
} |
| 323 |
umask(oldmask); |
| 324 |
|
| 325 |
while((r = fread(buf,sizeof(char), sizeof(buf), in_stream))) { |
| 326 |
if( fwrite(buf,sizeof(char), r, out_fp) != r) { |
| 327 |
syslog(LOG_WARNING,"autocreate_sieve: Problem writing to sieve script file: %s",sieve_tmpname); |
| 328 |
fclose(out_fp); |
| 329 |
unlink(sieve_tmpname); |
| 330 |
unlink(sieve_bctmpname); |
| 331 |
fclose(in_stream); |
| 332 |
return 1; |
| 333 |
} |
| 334 |
} |
| 335 |
|
| 336 |
if(feof(in_stream)) { |
| 337 |
fclose(out_fp); |
| 338 |
} else { /* ferror */ |
| 339 |
fclose(out_fp); |
| 340 |
unlink(sieve_tmpname); |
| 341 |
unlink(sieve_bctmpname); |
| 342 |
fclose(in_stream); |
| 343 |
return 1; |
| 344 |
} |
| 345 |
|
| 346 |
/* Renaming the necessary stuff */ |
| 347 |
if(rename(sieve_tmpname, sieve_script_name)) { |
| 348 |
unlink(sieve_tmpname); |
| 349 |
unlink(sieve_bctmpname); |
| 350 |
return 1; |
| 351 |
} |
| 352 |
|
| 353 |
if(rename(sieve_bctmpname, sieve_bcscript_name)) { |
| 354 |
unlink(sieve_bctmpname); |
| 355 |
unlink(sieve_bcscript_name); |
| 356 |
return 1; |
| 357 |
} |
| 358 |
|
| 359 |
/* end now with the symlink */ |
| 360 |
if(symlink(sieve_bclink_name, sieve_default)) { |
| 361 |
if(errno != EEXIST) { |
| 362 |
syslog(LOG_WARNING, "autocreate_sieve: problem making the default link."); |
| 363 |
/* Lets delete the files */ |
| 364 |
unlink(sieve_script_name); |
| 365 |
unlink(sieve_bcscript_name); |
| 366 |
} |
| 367 |
} |
| 368 |
|
| 369 |
/* |
| 370 |
* If everything has succeeded AND we have compiled the script AND we have requested |
| 371 |
* to generate the global script so that it is not compiled each time then we create it. |
| 372 |
*/ |
| 373 |
if(do_compile && |
| 374 |
config_getswitch(IMAPOPT_GENERATE_COMPILED_SIEVE_SCRIPT)) { |
| 375 |
|
| 376 |
if(!compiled_source_script) { |
| 377 |
syslog(LOG_WARNING, "autocreate_sieve: To save a compiled sieve script, autocreate_sieve_compiledscript must have been defined in imapd.conf"); |
| 378 |
return 0; |
| 379 |
} |
| 380 |
|
| 381 |
if(snprintf(sieve_tmpname, sizeof(sieve_tmpname), "%s.NEW", compiled_source_script) >= sizeof(sieve_tmpname)) |
| 382 |
return 0; |
| 383 |
|
| 384 |
/* |
| 385 |
* Copy everything from the newly created bc sieve sieve script. |
| 386 |
*/ |
| 387 |
if((in_fd = open(sieve_bcscript_name, O_RDONLY))<0) { |
| 388 |
return 0; |
| 389 |
} |
| 390 |
|
| 391 |
if((out_fd = open(sieve_tmpname, O_CREAT|O_EXCL|O_WRONLY, S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH)) < 0) { |
| 392 |
if(errno == EEXIST) { |
| 393 |
/* Someone is already doing this so just bail out. */ |
| 394 |
syslog(LOG_WARNING, "autocreate_sieve: %s already exists. Some other instance processing it, or it is left over", sieve_tmpname); |
| 395 |
close(in_fd); |
| 396 |
return 0; |
| 397 |
} else if (errno == EACCES) { |
| 398 |
syslog(LOG_WARNING,"autocreate_sieve: No access to create file %s. Check permissions",sieve_tmpname); |
| 399 |
close(in_fd); |
| 400 |
return 0; |
| 401 |
} else { |
| 402 |
syslog(LOG_WARNING,"autocreate_sieve: Unable to create %s",sieve_tmpname); |
| 403 |
close(in_fd); |
| 404 |
return 0; |
| 405 |
} |
| 406 |
} |
| 407 |
|
| 408 |
while((r = read(in_fd, buf, sizeof(buf))) > 0) { |
| 409 |
if((k = write(out_fd,buf,r)) < 0) { |
| 410 |
syslog(LOG_WARNING, "autocreate_sieve: Error writing to file: %s, error: %d", sieve_tmpname, errno); |
| 411 |
close(out_fd); |
| 412 |
close(in_fd); |
| 413 |
unlink(sieve_tmpname); |
| 414 |
return 0; |
| 415 |
} |
| 416 |
} |
| 417 |
|
| 418 |
if(r == 0 ) { /*EOF */ |
| 419 |
close(out_fd); |
| 420 |
close(in_fd); |
| 421 |
} else if (r < 0) { |
| 422 |
syslog(LOG_WARNING, "autocreate_sieve: Error writing to file: %s, error: %d", sieve_tmpname, errno); |
| 423 |
close(out_fd); |
| 424 |
close(in_fd); |
| 425 |
unlink(sieve_tmpname); |
| 426 |
return 0; |
| 427 |
} |
| 428 |
|
| 429 |
/* Rename the temporary created sieve script to its final name. */ |
| 430 |
if(rename(sieve_tmpname, compiled_source_script)) { |
| 431 |
if(errno != EEXIST) { |
| 432 |
unlink(sieve_tmpname); |
| 433 |
unlink(compiled_source_script); |
| 434 |
} |
| 435 |
return 0; |
| 436 |
} |
| 437 |
|
| 438 |
syslog(LOG_NOTICE, "autocreate_sieve: Compiled sieve script was successfully saved in %s", compiled_source_script); |
| 439 |
} |
| 440 |
|
| 441 |
return 0; |
| 442 |
} |
| 443 |
|
| 444 |
/*static void fatal(const char *s, int code) |
| 445 |
{ |
| 446 |
printf("Fatal error: %s (%d)\r\n", s, code); |
| 447 |
exit(1); |
| 448 |
}*/ |
| 449 |
|
| 450 |
/* to make larry's stupid functions happy :) */ |
| 451 |
static void foo(void) |
| 452 |
{ |
| 453 |
fatal("stub function called", 0); |
| 454 |
} |
| 455 |
|
| 456 |
static int sieve_notify(void *ac __attribute__((unused)), |
| 457 |
void *interp_context __attribute__((unused)), |
| 458 |
void *script_context __attribute__((unused)), |
| 459 |
void *message_context __attribute__((unused)), |
| 460 |
const char **errmsg __attribute__((unused))) |
| 461 |
{ |
| 462 |
fatal("stub function called", 0); |
| 463 |
return SIEVE_FAIL; |
| 464 |
} |
| 465 |
|
| 466 |
static int mysieve_error(int lineno, const char *msg, |
| 467 |
void *i __attribute__((unused)), void *s) |
| 468 |
{ |
| 469 |
char buf[1024]; |
| 470 |
char **errstr = (char **) s; |
| 471 |
|
| 472 |
snprintf(buf, 80, "line %d: %s\r\n", lineno, msg); |
| 473 |
*errstr = (char *) xrealloc(*errstr, strlen(*errstr) + strlen(buf) + 30); |
| 474 |
syslog(LOG_DEBUG, "%s", buf); |
| 475 |
strcat(*errstr, buf); |
| 476 |
|
| 477 |
return SIEVE_OK; |
| 478 |
} |
| 479 |
|
| 480 |
/* end the boilerplate */ |
| 481 |
|
| 482 |
/* returns TRUE or FALSE */ |
| 483 |
int is_script_parsable(FILE *stream, char **errstr, sieve_script_t **ret) |
| 484 |
{ |
| 485 |
sieve_interp_t *i; |
| 486 |
sieve_script_t *s; |
| 487 |
int res; |
| 488 |
|
| 489 |
res = sieve_interp_alloc(&i, NULL); |
| 490 |
if (res != SIEVE_OK) { |
| 491 |
syslog(LOG_WARNING, "sieve_interp_alloc() returns %d\n", res); |
| 492 |
return TIMSIEVE_FAIL; |
| 493 |
} |
| 494 |
|
| 495 |
res = sieve_register_redirect(i, (sieve_callback *) &foo); |
| 496 |
if (res != SIEVE_OK) { |
| 497 |
syslog(LOG_WARNING, "sieve_register_redirect() returns %d\n", res); |
| 498 |
return TIMSIEVE_FAIL; |
| 499 |
} |
| 500 |
res = sieve_register_discard(i, (sieve_callback *) &foo); |
| 501 |
if (res != SIEVE_OK) { |
| 502 |
syslog(LOG_WARNING, "sieve_register_discard() returns %d\n", res); |
| 503 |
return TIMSIEVE_FAIL; |
| 504 |
} |
| 505 |
res = sieve_register_reject(i, (sieve_callback *) &foo); |
| 506 |
if (res != SIEVE_OK) { |
| 507 |
syslog(LOG_WARNING, "sieve_register_reject() returns %d\n", res); |
| 508 |
return TIMSIEVE_FAIL; |
| 509 |
} |
| 510 |
res = sieve_register_fileinto(i, (sieve_callback *) &foo); |
| 511 |
if (res != SIEVE_OK) { |
| 512 |
syslog(LOG_WARNING, "sieve_register_fileinto() returns %d\n", res); |
| 513 |
return TIMSIEVE_FAIL; |
| 514 |
} |
| 515 |
res = sieve_register_keep(i, (sieve_callback *) &foo); |
| 516 |
if (res != SIEVE_OK) { |
| 517 |
syslog(LOG_WARNING, "sieve_register_keep() returns %d\n", res); |
| 518 |
return TIMSIEVE_FAIL; |
| 519 |
} |
| 520 |
|
| 521 |
res = sieve_register_imapflags(i, NULL); |
| 522 |
if (res != SIEVE_OK) { |
| 523 |
syslog(LOG_WARNING, "sieve_register_imapflags() returns %d\n", res); |
| 524 |
return TIMSIEVE_FAIL; |
| 525 |
} |
| 526 |
|
| 527 |
res = sieve_register_size(i, (sieve_get_size *) &foo); |
| 528 |
if (res != SIEVE_OK) { |
| 529 |
syslog(LOG_WARNING, "sieve_register_size() returns %d\n", res); |
| 530 |
return TIMSIEVE_FAIL; |
| 531 |
} |
| 532 |
|
| 533 |
res = sieve_register_header(i, (sieve_get_header *) &foo); |
| 534 |
if (res != SIEVE_OK) { |
| 535 |
syslog(LOG_WARNING, "sieve_register_header() returns %d\n", res); |
| 536 |
return TIMSIEVE_FAIL; |
| 537 |
} |
| 538 |
|
| 539 |
res = sieve_register_envelope(i, (sieve_get_envelope *) &foo); |
| 540 |
if (res != SIEVE_OK) { |
| 541 |
syslog(LOG_WARNING, "sieve_register_envelope() returns %d\n", res); |
| 542 |
return TIMSIEVE_FAIL; |
| 543 |
} |
| 544 |
|
| 545 |
res = sieve_register_vacation(i, &vacation2); |
| 546 |
if (res != SIEVE_OK) { |
| 547 |
syslog(LOG_WARNING, "sieve_register_vacation() returns %d\n", res); |
| 548 |
return TIMSIEVE_FAIL; |
| 549 |
} |
| 550 |
|
| 551 |
res = sieve_register_notify(i, &sieve_notify); |
| 552 |
if (res != SIEVE_OK) { |
| 553 |
syslog(LOG_WARNING, "sieve_register_notify() returns %d\n", res); |
| 554 |
return TIMSIEVE_FAIL; |
| 555 |
} |
| 556 |
|
| 557 |
res = sieve_register_parse_error(i, &mysieve_error); |
| 558 |
if (res != SIEVE_OK) { |
| 559 |
syslog(LOG_WARNING, "sieve_register_parse_error() returns %d\n", res); |
| 560 |
return TIMSIEVE_FAIL; |
| 561 |
} |
| 562 |
|
| 563 |
rewind(stream); |
| 564 |
|
| 565 |
*errstr = (char *) xmalloc(20 * sizeof(char)); |
| 566 |
strcpy(*errstr, "script errors:\r\n"); |
| 567 |
|
| 568 |
res = sieve_script_parse(i, stream, errstr, &s); |
| 569 |
|
| 570 |
if (res == SIEVE_OK) { |
| 571 |
if(ret) { |
| 572 |
*ret = s; |
| 573 |
} else { |
| 574 |
sieve_script_free(&s); |
| 575 |
} |
| 576 |
free(*errstr); |
| 577 |
*errstr = NULL; |
| 578 |
} |
| 579 |
|
| 580 |
/* free interpreter */ |
| 581 |
sieve_interp_free(&i); |
| 582 |
|
| 583 |
return (res == SIEVE_OK) ? TIMSIEVE_OK : TIMSIEVE_FAIL; |
| 584 |
} |
| 585 |
|
| 586 |
/* |
| 587 |
* Btw the initial date of this patch is Sep, 02 2004 which is the birthday of |
| 588 |
* Pavlos. Author of cyrusmaster. So consider this patch as his birthday present |
| 589 |
*/ |
| 590 |
|