| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
#include "mew.h" |
| 9 |
|
| 10 |
private char version_message[] = "version 6.8 20180607 Kazu Yamamoto"; |
| 11 |
|
| 12 |
#include <stdio.h> |
| 13 |
#include <stdlib.h> |
| 14 |
#include <stdarg.h> |
| 15 |
#include <ctype.h> |
| 16 |
#include <string.h> |
| 17 |
#include <sys/stat.h> |
| 18 |
#include <sys/types.h> |
| 19 |
#include <signal.h> |
| 20 |
#include <errno.h> |
| 21 |
|
| 22 |
#if TIME_WITH_SYS_TIME |
| 23 |
# include <sys/time.h> |
| 24 |
# include <time.h> |
| 25 |
#else |
| 26 |
# if HAVE_SYS_TIME_H |
| 27 |
# include <sys/time.h> |
| 28 |
# else |
| 29 |
# include <time.h> |
| 30 |
# endif |
| 31 |
#endif |
| 32 |
|
| 33 |
#if HAVE_DIRENT_H |
| 34 |
# include <dirent.h> |
| 35 |
#endif |
| 36 |
|
| 37 |
#if HAVE_FCNTL_H |
| 38 |
# include <fcntl.h> |
| 39 |
#endif |
| 40 |
|
| 41 |
#if HAVE_PWD_H |
| 42 |
# include <pwd.h> |
| 43 |
#endif |
| 44 |
|
| 45 |
#if HAVE_SYS_FILE_H |
| 46 |
# include <sys/file.h> |
| 47 |
#endif |
| 48 |
|
| 49 |
#if HAVE_UNISTD_H |
| 50 |
# include <unistd.h> |
| 51 |
#endif |
| 52 |
|
| 53 |
enum MBOXTYPE { |
| 54 |
T_UNKNOWN, |
| 55 |
T_MAILDIR, |
| 56 |
T_MBOX, |
| 57 |
T_STDIN, |
| 58 |
}; |
| 59 |
|
| 60 |
enum MBOXSTATE { |
| 61 |
ST_UNKNOWN, |
| 62 |
ST_HEADER, |
| 63 |
ST_BODY_AFTER_EMPTY_LINE, |
| 64 |
ST_BODY, |
| 65 |
}; |
| 66 |
|
| 67 |
#define FBUFSIZ (BUFSIZ * 32) |
| 68 |
#ifndef PATH_MAX |
| 69 |
# define PATH_MAX 1024 |
| 70 |
#endif |
| 71 |
|
| 72 |
private char FileBuf[FBUFSIZ]; |
| 73 |
private char Mbox[PATH_MAX]; |
| 74 |
private char MboxLock[PATH_MAX]; |
| 75 |
private char *InboxDir = NULL; |
| 76 |
private char *Suffix = NULL; |
| 77 |
private int Use_Suffix = FALSE; |
| 78 |
private int Suffix_len; |
| 79 |
private int MboxType; |
| 80 |
private int Backup; |
| 81 |
private int GetCur; |
| 82 |
private int UseCL; |
| 83 |
private int PreserveUnixFrom; |
| 84 |
private int CreateMTime = TRUE; |
| 85 |
private int FileMode = S_IRUSR | S_IWUSR; |
| 86 |
private int Exit = 0; |
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
private void error(const char *, ...); |
| 95 |
private void usage(const char *); |
| 96 |
private void help(const char *); |
| 97 |
private void version(const char *); |
| 98 |
private void init_env(int, char **); |
| 99 |
private int get_number(char *); |
| 100 |
private int get_last_seq(void); |
| 101 |
private int compare_string(char **, char **); |
| 102 |
private int copyfile(char *, char *); |
| 103 |
private void movefile(char *, char *, char *, int); |
| 104 |
private int maildir_names(const char *, char **, char **, char **); |
| 105 |
private int new_inbox_file(int, char[]); |
| 106 |
private FILE *open_new_inbox_file(int *, char[]); |
| 107 |
private int get_from_dir(int, char *, char *, int); |
| 108 |
private int process_maildir(int); |
| 109 |
private int lock_mbox(char *); |
| 110 |
private void unlock_mbox(char *); |
| 111 |
private int process_mbox(int); |
| 112 |
private int process_stdin(int); |
| 113 |
private void process(void); |
| 114 |
private int check_mbox_type(const char *); |
| 115 |
private void sanity_check(void); |
| 116 |
|
| 117 |
|
| 118 |
#if !HAVE_FLOCK |
| 119 |
# if HAVE_LOCKF |
| 120 |
# define flock(a, b) lockf(a, b, 0) |
| 121 |
# define LOCK_EX F_LOCK |
| 122 |
# endif |
| 123 |
#endif |
| 124 |
|
| 125 |
#ifndef S_ISDIR |
| 126 |
# define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR) |
| 127 |
#endif |
| 128 |
#ifndef S_ISREG |
| 129 |
# define S_ISREG(m) (((m) & S_IFMT) == S_IFREG) |
| 130 |
#endif |
| 131 |
|
| 132 |
private char * |
| 133 |
Getlogin(void) |
| 134 |
{ |
| 135 |
#ifdef HAVE_GETLOGIN |
| 136 |
{ |
| 137 |
char *user; |
| 138 |
if ((user = getlogin()) != NULL) |
| 139 |
return user; |
| 140 |
} |
| 141 |
#endif |
| 142 |
#ifdef HAVE_GETPWUID |
| 143 |
{ |
| 144 |
struct passwd *pw; |
| 145 |
if ((pw = getpwuid(getuid())) == NULL) |
| 146 |
return NULL; |
| 147 |
else |
| 148 |
return pw->pw_name; |
| 149 |
} |
| 150 |
#endif |
| 151 |
return NULL; |
| 152 |
} |
| 153 |
|
| 154 |
private char * |
| 155 |
Gethomedir(void) |
| 156 |
{ |
| 157 |
char *home; |
| 158 |
|
| 159 |
if ((home = getenv("HOME")) != NULL) |
| 160 |
return home; |
| 161 |
#ifdef HAVE_GETPWUID |
| 162 |
{ |
| 163 |
struct passwd *pw; |
| 164 |
if ((pw = getpwuid(getuid())) != NULL) |
| 165 |
return pw->pw_dir; |
| 166 |
} |
| 167 |
#endif |
| 168 |
return NULL; |
| 169 |
} |
| 170 |
|
| 171 |
private void |
| 172 |
error(const char *fmt, ...) |
| 173 |
{ |
| 174 |
va_list ap; |
| 175 |
if (warn_prog != NULL) |
| 176 |
fprintf(stderr, "%s: ", warn_prog); |
| 177 |
va_start(ap, fmt); |
| 178 |
if (fmt != NULL) |
| 179 |
vfprintf(stderr, fmt, ap); |
| 180 |
va_end(ap); |
| 181 |
fprintf(stderr, "\n"); |
| 182 |
if (strlen(MboxLock) > 0) |
| 183 |
unlock_mbox(MboxLock); |
| 184 |
exit(EXIT_FAILURE); |
| 185 |
} |
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
#define LOCK_SUFFIX ".lock" |
| 195 |
#define MAILDIR "Maildir" |
| 196 |
#define MAILDIR_NEW "new" |
| 197 |
#define MAILDIR_CUR "cur" |
| 198 |
#define MAILDIR_TMP "tmp" |
| 199 |
|
| 200 |
private void |
| 201 |
usage(const char *progname) { |
| 202 |
fprintf(stderr, "Usage: %s [-abchosv] [-d maildir] [-i inboxdir] [-x suffix]\n", progname); |
| 203 |
} |
| 204 |
|
| 205 |
private const char * |
| 206 |
help_message[] = { |
| 207 |
" -h Display this help message.", |
| 208 |
" -v Display the version.", |
| 209 |
" -d <mail> Path to mbox/maildir.", |
| 210 |
" -m <mail> Path to mbox/maildir.", |
| 211 |
" -s Read one mail from stdin instead of mbox/maildir.", |
| 212 |
" -i <inboxdir> Path to inboxdir.", |
| 213 |
" -b Backup mail.", |
| 214 |
" mbox: No truncate mbox file.", |
| 215 |
" maildir: To maildir/cur directory.", |
| 216 |
" -a Retrieve all mail from maildir/{cur,new} directory.", |
| 217 |
" (no backup) (for maildir)", |
| 218 |
" -c Use Content-Length: field. (for mbox)", |
| 219 |
" -u Don't create inboxdir/.mew-mtime file.", |
| 220 |
" -f Preserve Unix From (Envelope Sender). (for mbox)", |
| 221 |
" -p <fmode> Specify file permission. (for mbox)", |
| 222 |
" -o Use the suffix when creating messages.", |
| 223 |
" -x <suffix> Use this suffix.", |
| 224 |
NULL |
| 225 |
}; |
| 226 |
|
| 227 |
private void |
| 228 |
help(const char *progname) { |
| 229 |
const char **p = help_message; |
| 230 |
|
| 231 |
fprintf(stderr, "Help: %s\n\n", progname); |
| 232 |
fprintf(stderr, " Incorporating new mails.\n\n"); |
| 233 |
usage(progname); |
| 234 |
while (*p) fprintf(stderr, "%s\n", *p++); |
| 235 |
} |
| 236 |
|
| 237 |
private void |
| 238 |
version(const char *progname) { |
| 239 |
fprintf(stderr, "%s %s\n", progname, version_message); |
| 240 |
} |
| 241 |
|
| 242 |
private int |
| 243 |
check_mbox_type(const char *path) |
| 244 |
{ |
| 245 |
struct stat sb; |
| 246 |
|
| 247 |
if (stat(path, &sb)) |
| 248 |
return T_UNKNOWN; |
| 249 |
if (S_ISDIR(sb.st_mode)) { |
| 250 |
char* newdir; |
| 251 |
char* curdir; |
| 252 |
|
| 253 |
if (maildir_names(path, &newdir, &curdir, NULL)) |
| 254 |
error("maildir name is not set (%s)", path); |
| 255 |
if (stat(newdir, &sb)) |
| 256 |
return T_UNKNOWN; |
| 257 |
if (!S_ISDIR(sb.st_mode) || access(newdir, R_OK|W_OK|X_OK)) |
| 258 |
return T_UNKNOWN; |
| 259 |
|
| 260 |
if (Backup) { |
| 261 |
if (stat(curdir, &sb)) |
| 262 |
return T_UNKNOWN; |
| 263 |
if (!S_ISDIR(sb.st_mode) || access(curdir, W_OK)) |
| 264 |
return T_UNKNOWN; |
| 265 |
} |
| 266 |
return T_MAILDIR; |
| 267 |
} |
| 268 |
else if (S_ISREG(sb.st_mode)) |
| 269 |
return T_MBOX; |
| 270 |
else |
| 271 |
return T_UNKNOWN; |
| 272 |
} |
| 273 |
|
| 274 |
private const char * |
| 275 |
mbox_path_list[] = { |
| 276 |
"/var/mail/", |
| 277 |
"/var/spool/mail/", |
| 278 |
"/usr/spool/mail/", |
| 279 |
NULL |
| 280 |
}; |
| 281 |
|
| 282 |
private void |
| 283 |
search_mbox_path(void) |
| 284 |
{ |
| 285 |
char *home, *mail, *user; |
| 286 |
|
| 287 |
if ((home = Gethomedir()) != NULL) { |
| 288 |
if (strlen(home) + 9 > PATH_MAX) |
| 289 |
error("pathname too long (%s)", home); |
| 290 |
snprintf(Mbox, sizeof(Mbox), "%s/%s", home, MAILDIR); |
| 291 |
if (check_mbox_type(Mbox) != T_UNKNOWN) |
| 292 |
return; |
| 293 |
} |
| 294 |
if ((mail = getenv("MAIL")) != NULL) { |
| 295 |
if (strlen(mail) + 1 > PATH_MAX) |
| 296 |
error("pathname too long (%s)", mail); |
| 297 |
STRCPY(Mbox, mail); |
| 298 |
if (check_mbox_type(Mbox) != T_UNKNOWN) |
| 299 |
return; |
| 300 |
} |
| 301 |
if ((user = Getlogin()) != NULL) { |
| 302 |
int i; |
| 303 |
for (i = 0; mbox_path_list[i] != NULL; i++) { |
| 304 |
if (strlen(mbox_path_list[i]) + strlen(user) + 1 |
| 305 |
> PATH_MAX) |
| 306 |
error("pathname too long (%s)", user); |
| 307 |
snprintf(Mbox, sizeof(Mbox), "%s%s", mbox_path_list[i], user); |
| 308 |
if (check_mbox_type(Mbox) != T_UNKNOWN) |
| 309 |
return; |
| 310 |
} |
| 311 |
} |
| 312 |
snprintf(Mbox, sizeof(Mbox), "."); |
| 313 |
return; |
| 314 |
} |
| 315 |
|
| 316 |
void |
| 317 |
sig_exit(int signo) |
| 318 |
{ |
| 319 |
Exit = 1; |
| 320 |
} |
| 321 |
|
| 322 |
void |
| 323 |
sig_ignore(int signo) |
| 324 |
{ |
| 325 |
|
| 326 |
} |
| 327 |
|
| 328 |
private void |
| 329 |
set_sighandler(void) |
| 330 |
{ |
| 331 |
#if HAVE_SIGHUP |
| 332 |
if (signal(SIGHUP, sig_ignore) == SIG_ERR) |
| 333 |
error("can't catch SIGHUP\n"); |
| 334 |
#endif |
| 335 |
if (signal(SIGINT, sig_exit) == SIG_ERR) |
| 336 |
error("can't catch SIGINT\n"); |
| 337 |
#if HAVE_SIGHUP |
| 338 |
if (signal(SIGALRM, sig_ignore) == SIG_ERR) |
| 339 |
error("can't catch SIGALRM\n"); |
| 340 |
#endif |
| 341 |
if (signal(SIGTERM, sig_ignore) == SIG_ERR) |
| 342 |
error("can't catch SIGTERM\n"); |
| 343 |
} |
| 344 |
|
| 345 |
private void |
| 346 |
init_env(int argc, char **argv) |
| 347 |
{ |
| 348 |
set_sighandler(); |
| 349 |
STRDUP(InboxDir, "."); |
| 350 |
MboxType = T_UNKNOWN; |
| 351 |
Backup = FALSE; |
| 352 |
UseCL = FALSE; |
| 353 |
search_mbox_path(); |
| 354 |
STRDUP(Suffix, SUFFIX); |
| 355 |
Suffix_len = strlen(Suffix); |
| 356 |
} |
| 357 |
|
| 358 |
private int |
| 359 |
get_number(char *s) |
| 360 |
{ |
| 361 |
int num = 0; |
| 362 |
unsigned char c; |
| 363 |
|
| 364 |
while ((c = *s) != NUL && isdigit(c)) { |
| 365 |
num = num * 10 + c - '0'; |
| 366 |
s++; |
| 367 |
} |
| 368 |
|
| 369 |
if (num == 0) |
| 370 |
return 0; |
| 371 |
else if (*s == NUL) |
| 372 |
return num; |
| 373 |
else if (strncmp(s, Suffix, Suffix_len) == 0 && s[Suffix_len] == NUL) |
| 374 |
return num; |
| 375 |
else |
| 376 |
return 0; |
| 377 |
} |
| 378 |
|
| 379 |
private int |
| 380 |
get_last_seq(void) |
| 381 |
{ |
| 382 |
struct dirent *dp; |
| 383 |
DIR *dirp; |
| 384 |
int last = 0; |
| 385 |
int seq; |
| 386 |
|
| 387 |
if ((dirp = opendir(InboxDir)) == NULL) |
| 388 |
error("opendir(%s)", InboxDir); |
| 389 |
while ((dp = readdir(dirp)) != NULL) { |
| 390 |
if ((seq = get_number(dp->d_name)) == 0) |
| 391 |
continue; |
| 392 |
last = last > seq ? last : seq; |
| 393 |
} |
| 394 |
closedir(dirp); |
| 395 |
return last; |
| 396 |
} |
| 397 |
|
| 398 |
private int |
| 399 |
compare_string(char **i, char **j) |
| 400 |
{ |
| 401 |
return strcmp(*i, *j); |
| 402 |
} |
| 403 |
|
| 404 |
private int |
| 405 |
copyfile(char *src, char *dst) |
| 406 |
{ |
| 407 |
struct timeval tv[2]; |
| 408 |
struct stat sb; |
| 409 |
int srcfd, dstfd; |
| 410 |
ssize_t rlen, wlen; |
| 411 |
|
| 412 |
if ((srcfd = open(src, O_RDONLY, 0)) < 0) |
| 413 |
error("open(%s) for read", src); |
| 414 |
if (fstat(srcfd, &sb)) |
| 415 |
error("fstat(%s)", src); |
| 416 |
if ((dstfd = open(dst, O_EXCL | O_CREAT | O_WRONLY | O_TRUNC, 0)) < 0) |
| 417 |
error("open(%s) for write", dst); |
| 418 |
while ((rlen = read(srcfd, FileBuf, FBUFSIZ)) > 0) { |
| 419 |
if ((wlen = write(dstfd, FileBuf, rlen)) != rlen) { |
| 420 |
if (close(dstfd)) |
| 421 |
goto werr; |
| 422 |
unlink(dst); |
| 423 |
error("write(%s) (read %d bytes/write %d bytes)", |
| 424 |
dst, rlen, wlen); |
| 425 |
} |
| 426 |
} |
| 427 |
if (rlen < 0) { |
| 428 |
if (close(dstfd)) |
| 429 |
goto werr; |
| 430 |
unlink(dst); |
| 431 |
error("read(%s)", src); |
| 432 |
} |
| 433 |
close(srcfd); |
| 434 |
|
| 435 |
tv[0].tv_sec = sb.st_atime; |
| 436 |
tv[0].tv_usec = 0; |
| 437 |
tv[1].tv_sec = sb.st_mtime; |
| 438 |
tv[1].tv_usec = 0; |
| 439 |
#if HAVE_FUTIMES |
| 440 |
if (futimes(dstfd, tv)) |
| 441 |
warning("futimes(%s) failed", dst); |
| 442 |
#endif |
| 443 |
#if HAVE_FCHMOD |
| 444 |
if (fchmod(dstfd, sb.st_mode)) |
| 445 |
warning("fchmod(%s) failed", dst); |
| 446 |
#endif |
| 447 |
close(dstfd); |
| 448 |
#if !HAVE_FUTIMES && HAVE_UTIMES |
| 449 |
if (utimes(dst, tv)) |
| 450 |
warning("utimes(%s) failed", dst); |
| 451 |
#endif |
| 452 |
#if !HAVE_FCHMOD && HAVE_CHMOD |
| 453 |
if (chmod(dst, sb.st_mode)) |
| 454 |
warning("chmod(%s) failed", dst); |
| 455 |
#endif |
| 456 |
return 0; |
| 457 |
|
| 458 |
werr: |
| 459 |
close(srcfd); |
| 460 |
return -1; |
| 461 |
} |
| 462 |
|
| 463 |
private void |
| 464 |
movefile(char *fromfile, char *tofile, char *backupfile, int backup) |
| 465 |
{ |
| 466 |
if (backup && backupfile != NULL) { |
| 467 |
if (copyfile(fromfile, tofile)) |
| 468 |
error("copyfile(%s, %s)", fromfile, tofile); |
| 469 |
if (rename(fromfile, backupfile)) |
| 470 |
error("rename(%s, %s)", fromfile, backupfile); |
| 471 |
} |
| 472 |
else if (backup) { |
| 473 |
if (copyfile(fromfile, tofile)) |
| 474 |
error("copyfile(%s, %s)", fromfile, tofile); |
| 475 |
} |
| 476 |
else { |
| 477 |
if (rename(fromfile, tofile)) { |
| 478 |
if (errno != EXDEV) |
| 479 |
error("rename(%s, %s)", fromfile, tofile); |
| 480 |
if (copyfile(fromfile, tofile)) |
| 481 |
error("copyfile(%s, %s)", fromfile, tofile); |
| 482 |
if (unlink(fromfile)) |
| 483 |
error("unlink(%s)", fromfile); |
| 484 |
} |
| 485 |
} |
| 486 |
} |
| 487 |
|
| 488 |
|
| 489 |
private int |
| 490 |
maildir_names(const char *maildir, char **newdir, char **curdir, char **tmpdir) |
| 491 |
{ |
| 492 |
int len = strlen(maildir) + strlen(MAILDIR_NEW) + 2; |
| 493 |
|
| 494 |
if (maildir == NULL || strlen(maildir) <= 0) |
| 495 |
return -1; |
| 496 |
if (newdir != NULL) { |
| 497 |
MALLOC(*newdir, len); |
| 498 |
snprintf(*newdir, len, "%s/%s", maildir, MAILDIR_NEW); |
| 499 |
} |
| 500 |
if (curdir != NULL) { |
| 501 |
MALLOC(*curdir, len); |
| 502 |
snprintf(*curdir, len, "%s/%s", maildir, MAILDIR_CUR); |
| 503 |
} |
| 504 |
if (tmpdir != NULL) { |
| 505 |
MALLOC(*tmpdir, len); |
| 506 |
snprintf(*tmpdir, len, "%s/%s", maildir, MAILDIR_TMP); |
| 507 |
} |
| 508 |
return 0; |
| 509 |
} |
| 510 |
|
| 511 |
|
| 512 |
private int |
| 513 |
new_inbox_file(int seq, char inboxfile[]) |
| 514 |
{ |
| 515 |
char num[PATH_MAX]; |
| 516 |
char *suffix = (Use_Suffix == YES) ? Suffix : ""; |
| 517 |
|
| 518 |
do { |
| 519 |
snprintf(num, sizeof(num), "%d%s", ++seq, suffix); |
| 520 |
if (strlen(InboxDir) + strlen(num) + 2 > PATH_MAX) |
| 521 |
error("pathname too long (%s/%s)", InboxDir, num); |
| 522 |
snprintf(inboxfile, PATH_MAX, "%s/%s", InboxDir, num); |
| 523 |
if (access(inboxfile, F_OK) && errno == ENOENT) |
| 524 |
break; |
| 525 |
} while (TRUE); |
| 526 |
return seq; |
| 527 |
} |
| 528 |
|
| 529 |
|
| 530 |
private FILE * |
| 531 |
open_new_inbox_file(int *seq, char inboxfile[]) |
| 532 |
{ |
| 533 |
char num[PATH_MAX]; |
| 534 |
int flag = O_EXCL | O_CREAT | O_WRONLY; |
| 535 |
int fd; |
| 536 |
FILE *fp = NULL; |
| 537 |
char *suffix = (Use_Suffix == YES) ? Suffix : ""; |
| 538 |
|
| 539 |
for (;;) { |
| 540 |
snprintf(num, sizeof(num), "%d%s", ++*seq, suffix); |
| 541 |
if (strlen(InboxDir) + strlen(num) + 2 > PATH_MAX) |
| 542 |
error("pathname too long (%s/%s)", InboxDir, num); |
| 543 |
snprintf(inboxfile, PATH_MAX, "%s/%s", InboxDir, num); |
| 544 |
if ((fd = open(inboxfile, flag, FileMode)) >= 0 || |
| 545 |
errno != EEXIST) |
| 546 |
break; |
| 547 |
usleep(rand() % 199); |
| 548 |
} |
| 549 |
if (fd < 0) |
| 550 |
warning("open(%s) for write", inboxfile); |
| 551 |
else { |
| 552 |
if ((fp = fdopen(fd, FDWRITE)) == NULL) |
| 553 |
warning("open(%s) for write", inboxfile); |
| 554 |
} |
| 555 |
return fp; |
| 556 |
} |
| 557 |
|
| 558 |
private int |
| 559 |
get_from_dir(int seq, char *fromdir, char *backupdir, int backup) |
| 560 |
{ |
| 561 |
struct stat sb; |
| 562 |
struct dirent *dp; |
| 563 |
DIR *dirp; |
| 564 |
char mailfile[PATH_MAX]; |
| 565 |
char inboxfile[PATH_MAX]; |
| 566 |
char backupfile[PATH_MAX]; |
| 567 |
char **list; |
| 568 |
int listsize = BUFSIZ; |
| 569 |
int listend = 0; |
| 570 |
int i; |
| 571 |
|
| 572 |
MALLOC(list, sizeof(char *)*listsize); |
| 573 |
if ((dirp = opendir(fromdir)) == NULL) |
| 574 |
error("opendir(%s)", fromdir); |
| 575 |
while ((dp = readdir(dirp)) != NULL) { |
| 576 |
if (strlen(fromdir) + strlen(dp->d_name) + 2 > PATH_MAX) |
| 577 |
error("pathname too long (%s/%s)", |
| 578 |
fromdir, dp->d_name); |
| 579 |
snprintf(mailfile, sizeof(mailfile), "%s/%s", fromdir, dp->d_name); |
| 580 |
if (stat(mailfile, &sb)) |
| 581 |
continue; |
| 582 |
if (!(S_ISREG(sb.st_mode) && (sb.st_mode & S_IRUSR))) |
| 583 |
continue; |
| 584 |
if (listend >= listsize) { |
| 585 |
listsize *= 2; |
| 586 |
if ((list = (char **) |
| 587 |
realloc(list, sizeof(char *)*listsize)) == NULL) |
| 588 |
error("realloc"); |
| 589 |
} |
| 590 |
STRDUP(list[listend++], dp->d_name); |
| 591 |
} |
| 592 |
closedir(dirp); |
| 593 |
|
| 594 |
qsort(list, listend, sizeof(char *), |
| 595 |
(int (*)(const void *, const void *))compare_string); |
| 596 |
|
| 597 |
for (i = 0; i < listend; i++) { |
| 598 |
seq = new_inbox_file(seq, inboxfile); |
| 599 |
if (strlen(fromdir) + strlen(list[i]) + 2 > PATH_MAX) |
| 600 |
error("pathname too long (%s/%s)", |
| 601 |
fromdir, list[i]); |
| 602 |
snprintf(mailfile, sizeof(mailfile), "%s/%s", fromdir, list[i]); |
| 603 |
if (backup && backupdir != NULL) { |
| 604 |
if (strlen(backupdir) + strlen(list[i]) + 6 > PATH_MAX) |
| 605 |
error("pathname too long (%s/%s)", |
| 606 |
backupdir, list[i]); |
| 607 |
snprintf(backupfile, sizeof(backupfile), "%s/%s:2,S", backupdir, list[i]); |
| 608 |
movefile(mailfile, inboxfile, backupfile, backup); |
| 609 |
} |
| 610 |
else |
| 611 |
movefile(mailfile, inboxfile, NULL, backup); |
| 612 |
printf("%d\n", seq); |
| 613 |
} |
| 614 |
return seq; |
| 615 |
} |
| 616 |
|
| 617 |
private int |
| 618 |
process_maildir(int seq) |
| 619 |
{ |
| 620 |
char *newdir, *curdir; |
| 621 |
if (maildir_names(Mbox, &newdir, &curdir, NULL)) |
| 622 |
error("maildir name is not set (%s)", Mbox); |
| 623 |
if (GetCur) |
| 624 |
seq = get_from_dir(seq, curdir, NULL, Backup); |
| 625 |
return get_from_dir(seq, newdir, curdir, Backup); |
| 626 |
} |
| 627 |
|
| 628 |
private int |
| 629 |
lock_mbox(char *lockfile) |
| 630 |
{ |
| 631 |
int fd; |
| 632 |
int retry = 5; |
| 633 |
|
| 634 |
while (TRUE) { |
| 635 |
if ((fd = open(lockfile, O_WRONLY | O_CREAT | O_EXCL, 0666)) < 0) { |
| 636 |
if (errno == EACCES || errno == EROFS) |
| 637 |
return 1; |
| 638 |
else if (errno != EEXIST) |
| 639 |
error("open(%s)", lockfile); |
| 640 |
if (retry-- <= 0) { |
| 641 |
char buf[PATH_MAX]; |
| 642 |
STRCPY(buf, lockfile); |
| 643 |
MboxLock[0] = '\0'; |
| 644 |
error("can't get lock(%s)", buf); |
| 645 |
} else { |
| 646 |
if (warn_prog != NULL) |
| 647 |
fprintf(stderr, "%s: ", warn_prog); |
| 648 |
fprintf(stderr, |
| 649 |
"waiting for lockfile (%s) " |
| 650 |
"released (%d)\n", |
| 651 |
lockfile, retry); |
| 652 |
} |
| 653 |
} |
| 654 |
else { |
| 655 |
|
| 656 |
write(fd, "0", 1); |
| 657 |
close(fd); |
| 658 |
return 0; |
| 659 |
} |
| 660 |
sleep(2); |
| 661 |
} |
| 662 |
} |
| 663 |
|
| 664 |
private void |
| 665 |
unlock_mbox(char *lockfile) |
| 666 |
{ |
| 667 |
if (strlen(lockfile) > 0) |
| 668 |
unlink(lockfile); |
| 669 |
} |
| 670 |
|
| 671 |
private int |
| 672 |
process_mbox(int seq) |
| 673 |
{ |
| 674 |
char inboxfile[PATH_MAX]; |
| 675 |
char emptyline[3]; |
| 676 |
char *ln; |
| 677 |
int srcfd, oflag; |
| 678 |
FILE *srcfp = NULL; |
| 679 |
FILE *dstfp = NULL; |
| 680 |
int state = ST_UNKNOWN; |
| 681 |
int bytes = -1; |
| 682 |
|
| 683 |
if (strlen(Mbox) + strlen(LOCK_SUFFIX) + 1 > PATH_MAX) |
| 684 |
error("pathname too long (%s%s)", Mbox, LOCK_SUFFIX); |
| 685 |
snprintf(MboxLock, sizeof(MboxLock), "%s%s", Mbox, LOCK_SUFFIX); |
| 686 |
if (lock_mbox(MboxLock)) |
| 687 |
MboxLock[0] = '\0'; |
| 688 |
|
| 689 |
oflag = O_RDWR; |
| 690 |
#if defined(O_EXLOCK) |
| 691 |
oflag |= O_EXLOCK; |
| 692 |
#endif |
| 693 |
if ((srcfd = open(Mbox, oflag, 0)) < 0) { |
| 694 |
warning("open(%s) for rw/truncate", Mbox); goto rerr; |
| 695 |
} |
| 696 |
#if !defined(O_EXLOCK) && (HAVE_FLOCK || HAVE_LOCKF) |
| 697 |
if (flock(srcfd, LOCK_EX) < 0) { |
| 698 |
warning("flock(%s)", Mbox); goto rerr; |
| 699 |
} |
| 700 |
#endif |
| 701 |
if ((srcfp = fdopen(srcfd, FDREAD)) == NULL) { |
| 702 |
warning("fdopen(%s) for read", Mbox); goto rerr; |
| 703 |
} |
| 704 |
|
| 705 |
while ((ln = Getline(srcfp)) != NULL) { |
| 706 |
if (Exit) |
| 707 |
goto werr; |
| 708 |
switch (state) { |
| 709 |
case ST_UNKNOWN: |
| 710 |
if (strncmp(ln, "From ", 5) == 0) { |
| 711 |
dstfp = open_new_inbox_file(&seq, inboxfile); |
| 712 |
if (dstfp == NULL) |
| 713 |
goto rerr; |
| 714 |
if (PreserveUnixFrom && |
| 715 |
fputs(ln, dstfp) == EOF) { |
| 716 |
warning("fputs(%s)", inboxfile); |
| 717 |
goto werr; |
| 718 |
} |
| 719 |
state = ST_HEADER; |
| 720 |
} |
| 721 |
break; |
| 722 |
case ST_HEADER: |
| 723 |
if (strlen(ln) < 3 && |
| 724 |
(ln[0] == '\n' || ln[0] == '\r')) { |
| 725 |
STRCPY(emptyline, ln); |
| 726 |
state = ST_BODY_AFTER_EMPTY_LINE; |
| 727 |
break; |
| 728 |
} |
| 729 |
if (fputs(ln, dstfp) == EOF) { |
| 730 |
warning("fputs(%s)", inboxfile); goto werr; |
| 731 |
} |
| 732 |
if (UseCL && |
| 733 |
strncasecmp(ln, "Content-Length", 14) == 0) { |
| 734 |
int i; |
| 735 |
for (i = 14; i < strlen(ln); i++) |
| 736 |
if (isdigit((unsigned char)ln[i])) |
| 737 |
break; |
| 738 |
bytes = atoi(&ln[i]); |
| 739 |
} |
| 740 |
break; |
| 741 |
case ST_BODY_AFTER_EMPTY_LINE: |
| 742 |
if (bytes < 0 && strncmp(ln, "From ", 5) == 0) { |
| 743 |
if (fclose(dstfp)) |
| 744 |
goto rerr; |
| 745 |
printf("%d\n", seq); |
| 746 |
|
| 747 |
dstfp = open_new_inbox_file(&seq, inboxfile); |
| 748 |
if (dstfp == NULL) |
| 749 |
goto rerr; |
| 750 |
if (PreserveUnixFrom && |
| 751 |
fputs(ln, dstfp) == EOF) { |
| 752 |
warning("fputs(%s)", inboxfile); |
| 753 |
goto werr; |
| 754 |
} |
| 755 |
state = ST_HEADER; |
| 756 |
break; |
| 757 |
} |
| 758 |
else if (fputs(emptyline, dstfp) == EOF) |
| 759 |
goto werr; |
| 760 |
|
| 761 |
case ST_BODY: |
| 762 |
if (strlen(ln) < 3 && |
| 763 |
(ln[0] == '\n' || ln[0] == '\r')) { |
| 764 |
STRCPY(emptyline, ln); |
| 765 |
state = ST_BODY_AFTER_EMPTY_LINE; |
| 766 |
} |
| 767 |
else |
| 768 |
state = ST_BODY; |
| 769 |
|
| 770 |
if (state == ST_BODY && fputs(ln, dstfp) == EOF) |
| 771 |
goto werr; |
| 772 |
if (bytes >= 0) { |
| 773 |
bytes -= strlen(ln); |
| 774 |
if (bytes <= 0) { |
| 775 |
if (fclose(dstfp)) |
| 776 |
goto rerr; |
| 777 |
dstfp = NULL; |
| 778 |
printf("%d\n", seq); |
| 779 |
state = ST_UNKNOWN; |
| 780 |
bytes = -1; |
| 781 |
break; |
| 782 |
} |
| 783 |
} |
| 784 |
break; |
| 785 |
} |
| 786 |
free(ln); |
| 787 |
} |
| 788 |
if (dstfp) { |
| 789 |
if (fclose(dstfp)) |
| 790 |
goto rerr; |
| 791 |
printf("%d\n", seq); |
| 792 |
} |
| 793 |
if (!Backup && ftruncate(srcfd, 0)) { |
| 794 |
unlock_mbox(MboxLock); |
| 795 |
error("ftruncate"); |
| 796 |
} |
| 797 |
fclose(srcfp); |
| 798 |
unlock_mbox(MboxLock); |
| 799 |
return seq; |
| 800 |
|
| 801 |
werr: |
| 802 |
if (dstfp) |
| 803 |
fclose(dstfp); |
| 804 |
unlink(inboxfile); |
| 805 |
rerr: |
| 806 |
if (srcfp) |
| 807 |
fclose(srcfp); |
| 808 |
unlock_mbox(MboxLock); |
| 809 |
error("process_mbox(%s)", Mbox); |
| 810 |
return -1; |
| 811 |
} |
| 812 |
|
| 813 |
private int |
| 814 |
process_stdin(int seq) |
| 815 |
{ |
| 816 |
char inboxfile[PATH_MAX]; |
| 817 |
char *ln; |
| 818 |
FILE *srcfp = stdin; |
| 819 |
FILE *dstfp; |
| 820 |
|
| 821 |
if ((dstfp = open_new_inbox_file(&seq, inboxfile)) == NULL) |
| 822 |
goto rerr; |
| 823 |
|
| 824 |
while ((ln = Getline(srcfp)) != NULL) { |
| 825 |
if (Exit) |
| 826 |
goto werr; |
| 827 |
if (fputs(ln, dstfp) == EOF) { |
| 828 |
warning("fputs(%s)", inboxfile); goto werr; |
| 829 |
} |
| 830 |
free(ln); |
| 831 |
} |
| 832 |
|
| 833 |
if (dstfp) { |
| 834 |
fclose(dstfp); |
| 835 |
printf("%d\n", seq); |
| 836 |
} |
| 837 |
return seq; |
| 838 |
|
| 839 |
werr: |
| 840 |
if (dstfp) |
| 841 |
fclose(dstfp); |
| 842 |
unlink(inboxfile); |
| 843 |
rerr: |
| 844 |
error("process_stdin"); |
| 845 |
return -1; |
| 846 |
} |
| 847 |
|
| 848 |
private void |
| 849 |
process(void) |
| 850 |
{ |
| 851 |
char mtimefile[PATH_MAX]; |
| 852 |
FILE *fp; |
| 853 |
size_t wb; |
| 854 |
int len = strlen(MEW_MTIME_PHRASE); |
| 855 |
int seq = get_last_seq(); |
| 856 |
int newseq = 0; |
| 857 |
|
| 858 |
switch (MboxType) { |
| 859 |
case T_MAILDIR: |
| 860 |
newseq = process_maildir(seq); |
| 861 |
break; |
| 862 |
case T_MBOX: |
| 863 |
newseq = process_mbox(seq); |
| 864 |
break; |
| 865 |
case T_STDIN: |
| 866 |
newseq = process_stdin(seq); |
| 867 |
break; |
| 868 |
default: |
| 869 |
error("unknown mbox type (%s)", Mbox); |
| 870 |
} |
| 871 |
|
| 872 |
|
| 873 |
if (!CreateMTime || newseq <= seq) |
| 874 |
return; |
| 875 |
if (strlen(InboxDir) + strlen(MEW_MTIME_FILE) + 1 > PATH_MAX) |
| 876 |
error("pathname too long (%s%s)", InboxDir, MEW_MTIME_FILE); |
| 877 |
snprintf(mtimefile, sizeof(mtimefile), "%s/%s", InboxDir, MEW_MTIME_FILE); |
| 878 |
|
| 879 |
if ((fp = fopen(mtimefile, FDWRITE)) == NULL) |
| 880 |
error("can't create file (%s)", mtimefile); |
| 881 |
if ((wb = fwrite(MEW_MTIME_PHRASE, sizeof(char), len, fp)) != len) { |
| 882 |
fclose(fp); |
| 883 |
error("fwrite failed (%d, %s)", wb, mtimefile); |
| 884 |
} |
| 885 |
fclose(fp); |
| 886 |
} |
| 887 |
|
| 888 |
private void |
| 889 |
sanity_check(void) |
| 890 |
{ |
| 891 |
struct stat sb; |
| 892 |
|
| 893 |
|
| 894 |
if (stat(InboxDir, &sb)) |
| 895 |
error("stat(%s)", InboxDir); |
| 896 |
if (!S_ISDIR(sb.st_mode) || access(InboxDir, W_OK)) |
| 897 |
error("can't write directory (%s)", InboxDir); |
| 898 |
|
| 899 |
|
| 900 |
if (MboxType == T_UNKNOWN && |
| 901 |
(MboxType = check_mbox_type(Mbox)) == T_UNKNOWN) |
| 902 |
error("can't find mbox (%s)", Mbox); |
| 903 |
} |
| 904 |
|
| 905 |
int |
| 906 |
main(int argc, char **argv) |
| 907 |
{ |
| 908 |
extern char *Optarg; |
| 909 |
extern int Optind; |
| 910 |
char *progname = getprognm(argv[0]); |
| 911 |
int ch; |
| 912 |
|
| 913 |
warn_prog = progname; |
| 914 |
init_env(argc, argv); |
| 915 |
|
| 916 |
while ((ch = Getopt(argc, argv, "abcd:fhi:m:op:suvx:")) != EOF) { |
| 917 |
switch (ch) { |
| 918 |
case 'a': |
| 919 |
GetCur = TRUE; |
| 920 |
break; |
| 921 |
case 'b': |
| 922 |
Backup = TRUE; |
| 923 |
break; |
| 924 |
case 'c': |
| 925 |
UseCL = TRUE; |
| 926 |
break; |
| 927 |
case 'd': |
| 928 |
case 'm': |
| 929 |
if (strlen(Optarg) + 1 > PATH_MAX) |
| 930 |
error("pathname too long (%s)", Optarg); |
| 931 |
snprintf(Mbox, sizeof(Mbox), "%s", Optarg); |
| 932 |
break; |
| 933 |
case 'f': |
| 934 |
PreserveUnixFrom = TRUE; |
| 935 |
break; |
| 936 |
case 'i': |
| 937 |
STRDUP(InboxDir, Optarg); |
| 938 |
break; |
| 939 |
case 'o': |
| 940 |
Use_Suffix = TRUE; |
| 941 |
break; |
| 942 |
case 'x': |
| 943 |
STRDUP(Suffix, Optarg); |
| 944 |
Suffix_len = strlen(Suffix); |
| 945 |
break; |
| 946 |
case 'p': |
| 947 |
sscanf(Optarg, "%i", &FileMode); |
| 948 |
break; |
| 949 |
case 's': |
| 950 |
MboxType = T_STDIN; |
| 951 |
break; |
| 952 |
case 'u': |
| 953 |
CreateMTime = FALSE; |
| 954 |
break; |
| 955 |
case 'v': |
| 956 |
version(progname); |
| 957 |
exit(EXIT_SUCCESS); |
| 958 |
case 'h': |
| 959 |
help(progname); |
| 960 |
exit(EXIT_SUCCESS); |
| 961 |
default: |
| 962 |
usage(progname); |
| 963 |
exit(EXIT_SUCCESS); |
| 964 |
} |
| 965 |
} |
| 966 |
argc -= Optind; |
| 967 |
argv += Optind; |
| 968 |
|
| 969 |
sanity_check(); |
| 970 |
process(); |
| 971 |
return EXIT_SUCCESS; |
| 972 |
} |
| 973 |
|
| 974 |
|
| 975 |
|
| 976 |
|
| 977 |
|
| 978 |
|
| 979 |
|
| 980 |
|
| 981 |
|
| 982 |
|
| 983 |
|
| 984 |
|
| 985 |
|
| 986 |
|
| 987 |
|
| 988 |
|
| 989 |
|
| 990 |
|
| 991 |
|
| 992 |
|
| 993 |
|
| 994 |
|
| 995 |
|
| 996 |
|
| 997 |
|
| 998 |
|
| 999 |
|
| 1000 |
|
| 1001 |
|
| 1002 |
|
| 1003 |
|
| 1004 |
|
| 1005 |
|
| 1006 |
|