movemail.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874
  1. /* movemail foo bar -- move file foo to file bar,
  2. locking file foo the way /bin/mail respects.
  3. Copyright (C) 1986, 1992-1994, 1996, 1999, 2001-2017 Free Software
  4. Foundation, Inc.
  5. This file is part of GNU Emacs.
  6. GNU Emacs is free software: you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation, either version 3 of the License, or (at
  9. your option) any later version.
  10. GNU Emacs is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
  16. /* Important notice: defining MAIL_USE_FLOCK or MAIL_USE_LOCKF *will
  17. cause loss of mail* if you do it on a system that does not normally
  18. use flock/lockf as its way of interlocking access to inbox files. The
  19. setting of MAIL_USE_FLOCK and MAIL_USE_LOCKF *must agree* with the
  20. system's own conventions. It is not a choice that is up to you.
  21. So, if your system uses lock files rather than flock, then the only way
  22. you can get proper operation is to enable movemail to write lockfiles there.
  23. This means you must either give that directory access modes
  24. that permit everyone to write lockfiles in it, or you must make movemail
  25. a setuid or setgid program. */
  26. /*
  27. * Modified January, 1986 by Michael R. Gretzinger (Project Athena)
  28. *
  29. * Added POP (Post Office Protocol) service. When compiled -DMAIL_USE_POP
  30. * movemail will accept input filename arguments of the form
  31. * "po:username". This will cause movemail to open a connection to
  32. * a pop server running on $MAILHOST (environment variable). Movemail
  33. * must be setuid to root in order to work with POP.
  34. *
  35. * New module: popmail.c
  36. * Modified routines:
  37. * main - added code within #ifdef MAIL_USE_POP; added setuid (getuid ())
  38. * after POP code.
  39. * New routines in movemail.c:
  40. * get_errmsg - return pointer to system error message
  41. *
  42. * Modified August, 1993 by Jonathan Kamens (OpenVision Technologies)
  43. *
  44. * Move all of the POP code into a separate file, "pop.c".
  45. * Use strerror instead of get_errmsg.
  46. *
  47. */
  48. #include <config.h>
  49. #include <sys/types.h>
  50. #include <sys/stat.h>
  51. #include <sys/file.h>
  52. #include <stdlib.h>
  53. #include <errno.h>
  54. #include <time.h>
  55. #include <getopt.h>
  56. #include <unistd.h>
  57. #include <fcntl.h>
  58. #include <signal.h>
  59. #include <string.h>
  60. #include <unlocked-io.h>
  61. #include "syswait.h"
  62. #ifdef MAIL_USE_POP
  63. #include "pop.h"
  64. #endif
  65. #ifdef MSDOS
  66. #undef access
  67. #endif /* MSDOS */
  68. #ifdef WINDOWSNT
  69. #include "ntlib.h"
  70. #undef access
  71. #undef unlink
  72. #define fork() 0
  73. #define waitpid(child, var, flags) (*(var) = 0)
  74. /* Unfortunately, Samba doesn't seem to properly lock Unix files even
  75. though the locking call succeeds (and indeed blocks local access from
  76. other NT programs). If you have direct file access using an NFS
  77. client or something other than Samba, the locking call might work
  78. properly - make sure it does before you enable this!
  79. [18-Feb-97 andrewi] I now believe my comment above to be incorrect,
  80. since it was based on a misunderstanding of how locking calls are
  81. implemented and used on Unix. */
  82. //#define DISABLE_DIRECT_ACCESS
  83. #include <fcntl.h>
  84. #endif /* WINDOWSNT */
  85. #ifdef WINDOWSNT
  86. #include <sys/locking.h>
  87. #endif
  88. /* If your system uses the `flock' or `lockf' system call for mail locking,
  89. define MAIL_USE_SYSTEM_LOCK. If your system type should always define
  90. MAIL_USE_LOCKF or MAIL_USE_FLOCK but configure does not do this,
  91. please make a bug report. */
  92. #ifdef MAIL_USE_LOCKF
  93. #define MAIL_USE_SYSTEM_LOCK
  94. #endif
  95. #ifdef MAIL_USE_FLOCK
  96. #define MAIL_USE_SYSTEM_LOCK
  97. #endif
  98. #if (!defined MAIL_USE_SYSTEM_LOCK \
  99. && (defined HAVE_LIBMAIL || defined HAVE_LIBLOCKFILE) \
  100. && defined HAVE_MAILLOCK_H)
  101. #include <maillock.h>
  102. /* We can't use maillock unless we know what directory system mail
  103. files appear in. */
  104. #ifdef MAILDIR
  105. #define MAIL_USE_MAILLOCK
  106. static char *mail_spool_name (char *);
  107. #endif
  108. #endif
  109. static _Noreturn void fatal (const char *s1, const char *s2, const char *s3);
  110. static void error (const char *s1, const char *s2, const char *s3);
  111. static _Noreturn void pfatal_with_name (char *name);
  112. static _Noreturn void pfatal_and_delete (char *name);
  113. #ifdef MAIL_USE_POP
  114. static int popmail (char *, char *, bool, char *, bool);
  115. static bool pop_retr (popserver, int, FILE *);
  116. static bool mbx_write (char *, int, FILE *);
  117. static bool mbx_delimit_begin (FILE *);
  118. static bool mbx_delimit_end (FILE *);
  119. #endif
  120. #if (defined MAIL_USE_MAILLOCK \
  121. || (!defined DISABLE_DIRECT_ACCESS && !defined MAIL_USE_SYSTEM_LOCK))
  122. /* Like malloc but get fatal error if memory is exhausted. */
  123. static void *
  124. xmalloc (size_t size)
  125. {
  126. void *result = malloc (size);
  127. if (!result)
  128. fatal ("virtual memory exhausted", 0, 0);
  129. return result;
  130. }
  131. #endif
  132. /* Nonzero means this is name of a lock file to delete on fatal error. */
  133. static char *delete_lockname;
  134. int
  135. main (int argc, char **argv)
  136. {
  137. char *inname, *outname;
  138. int indesc, outdesc;
  139. ssize_t nread;
  140. int wait_status;
  141. int c;
  142. bool preserve_mail = false;
  143. #ifndef MAIL_USE_SYSTEM_LOCK
  144. struct stat st;
  145. int tem;
  146. char *tempname;
  147. size_t inname_len, inname_dirlen;
  148. int desc;
  149. #endif /* not MAIL_USE_SYSTEM_LOCK */
  150. #ifdef MAIL_USE_POP
  151. bool pop_reverse_order = false;
  152. # define ARGSTR "pr"
  153. #else /* ! MAIL_USE_POP */
  154. # define ARGSTR "p"
  155. #endif /* MAIL_USE_POP */
  156. uid_t real_gid = getgid ();
  157. uid_t priv_gid = getegid ();
  158. delete_lockname = 0;
  159. while (0 <= (c = getopt (argc, argv, ARGSTR)))
  160. {
  161. switch (c) {
  162. #ifdef MAIL_USE_POP
  163. case 'r':
  164. pop_reverse_order = true;
  165. break;
  166. #endif
  167. case 'p':
  168. preserve_mail = true;
  169. break;
  170. default:
  171. return EXIT_FAILURE;
  172. }
  173. }
  174. if (
  175. #ifdef MAIL_USE_POP
  176. (argc - optind < 2) || (argc - optind > 3)
  177. #else
  178. (argc - optind != 2)
  179. #endif
  180. )
  181. {
  182. #ifdef MAIL_USE_POP
  183. fprintf (stderr, "Usage: movemail [-p] [-r] inbox destfile%s\n",
  184. " [POP-password]");
  185. #else
  186. fprintf (stderr, "Usage: movemail [-p] inbox destfile%s\n", "");
  187. #endif
  188. return EXIT_FAILURE;
  189. }
  190. inname = argv[optind];
  191. outname = argv[optind+1];
  192. if (*outname == 0)
  193. fatal ("Destination file name is empty", 0, 0);
  194. #ifdef MAIL_USE_POP
  195. if (!strncmp (inname, "po:", 3))
  196. {
  197. int status;
  198. status = popmail (inname + 3, outname, preserve_mail,
  199. (argc - optind == 3) ? argv[optind+2] : NULL,
  200. pop_reverse_order);
  201. return status;
  202. }
  203. if (setuid (getuid ()) < 0)
  204. fatal ("Failed to drop privileges", 0, 0);
  205. #endif /* MAIL_USE_POP */
  206. #ifndef DISABLE_DIRECT_ACCESS
  207. char *lockname = 0;
  208. char *spool_name = 0;
  209. #ifdef MAIL_USE_MAILLOCK
  210. spool_name = mail_spool_name (inname);
  211. #endif
  212. if (! spool_name)
  213. {
  214. #ifndef MAIL_USE_SYSTEM_LOCK
  215. /* Use a lock file named after our first argument with .lock appended:
  216. If it exists, the mail file is locked. */
  217. /* Note: this locking mechanism is *required* by the mailer
  218. (on systems which use it) to prevent loss of mail.
  219. On systems that use a lock file, extracting the mail without locking
  220. WILL occasionally cause loss of mail due to timing errors!
  221. So, if creation of the lock file fails due to access
  222. permission on the mail spool directory, you simply MUST
  223. change the permission and/or make movemail a setgid program
  224. so it can create lock files properly.
  225. You might also wish to verify that your system is one which
  226. uses lock files for this purpose. Some systems use other methods. */
  227. inname_len = strlen (inname);
  228. lockname = xmalloc (inname_len + sizeof ".lock");
  229. strcpy (lockname, inname);
  230. strcpy (lockname + inname_len, ".lock");
  231. for (inname_dirlen = inname_len;
  232. inname_dirlen && !IS_DIRECTORY_SEP (inname[inname_dirlen - 1]);
  233. inname_dirlen--)
  234. continue;
  235. tempname = xmalloc (inname_dirlen + sizeof "EXXXXXX");
  236. while (true)
  237. {
  238. /* Create the lock file, but not under the lock file name. */
  239. /* Give up if cannot do that. */
  240. memcpy (tempname, inname, inname_dirlen);
  241. strcpy (tempname + inname_dirlen, "EXXXXXX");
  242. desc = mkostemp (tempname, O_BINARY);
  243. if (desc < 0)
  244. {
  245. int mkostemp_errno = errno;
  246. error ("error while creating what would become the lock file",
  247. 0, 0);
  248. errno = mkostemp_errno;
  249. pfatal_with_name (tempname);
  250. }
  251. close (desc);
  252. tem = link (tempname, lockname);
  253. if (tem < 0 && errno != EEXIST)
  254. pfatal_with_name (lockname);
  255. unlink (tempname);
  256. if (tem >= 0)
  257. break;
  258. sleep (1);
  259. /* If lock file is five minutes old, unlock it.
  260. Five minutes should be good enough to cope with crashes
  261. and wedgitude, and long enough to avoid being fooled
  262. by time differences between machines. */
  263. if (stat (lockname, &st) >= 0)
  264. {
  265. time_t now = time (0);
  266. if (st.st_ctime < now - 300)
  267. {
  268. unlink (lockname);
  269. lockname = 0;
  270. }
  271. }
  272. }
  273. delete_lockname = lockname;
  274. #endif /* not MAIL_USE_SYSTEM_LOCK */
  275. }
  276. #ifdef SIGCHLD
  277. signal (SIGCHLD, SIG_DFL);
  278. #endif
  279. pid_t child = fork ();
  280. if (child < 0)
  281. fatal ("Error in fork; %s", strerror (errno), 0);
  282. if (child == 0)
  283. {
  284. int lockcount = 0;
  285. int status = 0;
  286. #if defined (MAIL_USE_MAILLOCK) && defined (HAVE_TOUCHLOCK)
  287. time_t touched_lock;
  288. #endif
  289. if (setuid (getuid ()) < 0 || setregid (-1, real_gid) < 0)
  290. fatal ("Failed to drop privileges", 0, 0);
  291. #ifdef MAIL_USE_SYSTEM_LOCK
  292. indesc = open (inname, O_RDWR | O_BINARY);
  293. #else /* if not MAIL_USE_SYSTEM_LOCK */
  294. indesc = open (inname, O_RDONLY | O_BINARY);
  295. #endif /* not MAIL_USE_SYSTEM_LOCK */
  296. if (indesc < 0)
  297. pfatal_with_name (inname);
  298. /* Make sure the user can read the output file. */
  299. umask (umask (0) & 0377);
  300. outdesc = open (outname, O_WRONLY | O_BINARY | O_CREAT | O_EXCL, 0666);
  301. if (outdesc < 0)
  302. pfatal_with_name (outname);
  303. if (setregid (-1, priv_gid) < 0)
  304. fatal ("Failed to regain privileges", 0, 0);
  305. /* This label exists so we can retry locking
  306. after a delay, if it got EAGAIN or EBUSY. */
  307. retry_lock:
  308. /* Try to lock it. */
  309. #ifdef MAIL_USE_MAILLOCK
  310. if (spool_name)
  311. {
  312. /* The "-" is to make it a negative number if maillock returns
  313. non-zero. */
  314. status = - maillock (spool_name, 1);
  315. #ifdef HAVE_TOUCHLOCK
  316. touched_lock = time (0);
  317. #endif
  318. lockcount = 5;
  319. }
  320. else
  321. #endif /* MAIL_USE_MAILLOCK */
  322. {
  323. #ifdef MAIL_USE_SYSTEM_LOCK
  324. #ifdef MAIL_USE_LOCKF
  325. status = lockf (indesc, F_LOCK, 0);
  326. #else /* not MAIL_USE_LOCKF */
  327. #ifdef WINDOWSNT
  328. status = locking (indesc, LK_RLCK, -1L);
  329. #else
  330. status = flock (indesc, LOCK_EX);
  331. #endif
  332. #endif /* not MAIL_USE_LOCKF */
  333. #endif /* MAIL_USE_SYSTEM_LOCK */
  334. }
  335. /* If it fails, retry up to 5 times
  336. for certain failure codes. */
  337. if (status < 0)
  338. {
  339. if (++lockcount <= 5 && (errno == EAGAIN || errno == EBUSY))
  340. {
  341. sleep (1);
  342. goto retry_lock;
  343. }
  344. pfatal_with_name (inname);
  345. }
  346. {
  347. char buf[1024];
  348. while (true)
  349. {
  350. nread = read (indesc, buf, sizeof buf);
  351. if (nread < 0)
  352. pfatal_with_name (inname);
  353. if (nread != write (outdesc, buf, nread))
  354. {
  355. int saved_errno = errno;
  356. unlink (outname);
  357. errno = saved_errno;
  358. pfatal_with_name (outname);
  359. }
  360. if (nread < sizeof buf)
  361. break;
  362. #if defined (MAIL_USE_MAILLOCK) && defined (HAVE_TOUCHLOCK)
  363. if (spool_name)
  364. {
  365. time_t now = time (0);
  366. if (now - touched_lock > 60)
  367. {
  368. touchlock ();
  369. touched_lock = now;
  370. }
  371. }
  372. #endif /* MAIL_USE_MAILLOCK */
  373. }
  374. }
  375. if (fsync (outdesc) != 0 && errno != EINVAL)
  376. pfatal_and_delete (outname);
  377. /* Prevent symlink attacks truncating other users' mailboxes */
  378. if (setregid (-1, real_gid) < 0)
  379. fatal ("Failed to drop privileges", 0, 0);
  380. /* Check to make sure no errors before we zap the inbox. */
  381. if (close (outdesc) != 0)
  382. pfatal_and_delete (outname);
  383. #ifdef MAIL_USE_SYSTEM_LOCK
  384. if (! preserve_mail)
  385. {
  386. if (ftruncate (indesc, 0) != 0)
  387. pfatal_with_name (inname);
  388. }
  389. #endif /* MAIL_USE_SYSTEM_LOCK */
  390. close (indesc);
  391. #ifndef MAIL_USE_SYSTEM_LOCK
  392. if (! preserve_mail)
  393. {
  394. /* Delete the input file; if we can't, at least get rid of its
  395. contents. */
  396. #ifdef MAIL_UNLINK_SPOOL
  397. /* This is generally bad to do, because it destroys the permissions
  398. that were set on the file. Better to just empty the file. */
  399. if (unlink (inname) < 0 && errno != ENOENT)
  400. #endif /* MAIL_UNLINK_SPOOL */
  401. creat (inname, 0600);
  402. }
  403. #endif /* not MAIL_USE_SYSTEM_LOCK */
  404. /* End of mailbox truncation */
  405. if (setregid (-1, priv_gid) < 0)
  406. fatal ("Failed to regain privileges", 0, 0);
  407. #ifdef MAIL_USE_MAILLOCK
  408. /* This has to occur in the child, i.e., in the process that
  409. acquired the lock! */
  410. if (spool_name)
  411. mailunlock ();
  412. #endif
  413. return EXIT_SUCCESS;
  414. }
  415. if (waitpid (child, &wait_status, 0) < 0)
  416. fatal ("Error in waitpid; %s", strerror (errno), 0);
  417. if (!WIFEXITED (wait_status))
  418. return EXIT_FAILURE;
  419. else if (WEXITSTATUS (wait_status) != 0)
  420. return WEXITSTATUS (wait_status);
  421. if (lockname)
  422. unlink (lockname);
  423. #endif /* ! DISABLE_DIRECT_ACCESS */
  424. return EXIT_SUCCESS;
  425. }
  426. #ifdef MAIL_USE_MAILLOCK
  427. /* This function uses stat to confirm that the mail directory is
  428. identical to the directory of the input file, rather than just
  429. string-comparing the two paths, because one or both of them might
  430. be symbolic links pointing to some other directory. */
  431. static char *
  432. mail_spool_name (char *inname)
  433. {
  434. struct stat stat1, stat2;
  435. char *indir, *fname;
  436. int status;
  437. if (! (fname = strrchr (inname, '/')))
  438. return NULL;
  439. fname++;
  440. if (stat (MAILDIR, &stat1) < 0)
  441. return NULL;
  442. indir = xmalloc (fname - inname + 1);
  443. memcpy (indir, inname, fname - inname);
  444. indir[fname-inname] = '\0';
  445. status = stat (indir, &stat2);
  446. free (indir);
  447. if (status < 0)
  448. return NULL;
  449. if (stat1.st_dev == stat2.st_dev
  450. && stat1.st_ino == stat2.st_ino)
  451. return fname;
  452. return NULL;
  453. }
  454. #endif /* MAIL_USE_MAILLOCK */
  455. /* Print error message and exit. */
  456. static void
  457. fatal (const char *s1, const char *s2, const char *s3)
  458. {
  459. if (delete_lockname)
  460. unlink (delete_lockname);
  461. error (s1, s2, s3);
  462. exit (EXIT_FAILURE);
  463. }
  464. /* Print error message. `s1' is printf control string, `s2' and `s3'
  465. are args for it or null. */
  466. static void
  467. error (const char *s1, const char *s2, const char *s3)
  468. {
  469. fprintf (stderr, "movemail: ");
  470. if (s3)
  471. fprintf (stderr, s1, s2, s3);
  472. else if (s2)
  473. fprintf (stderr, s1, s2);
  474. else
  475. fprintf (stderr, "%s", s1);
  476. fprintf (stderr, "\n");
  477. }
  478. static void
  479. pfatal_with_name (char *name)
  480. {
  481. fatal ("%s for %s", strerror (errno), name);
  482. }
  483. static void
  484. pfatal_and_delete (char *name)
  485. {
  486. char *s = strerror (errno);
  487. unlink (name);
  488. fatal ("%s for %s", s, name);
  489. }
  490. /* This is the guts of the interface to the Post Office Protocol. */
  491. #ifdef MAIL_USE_POP
  492. #ifndef WINDOWSNT
  493. #include <sys/socket.h>
  494. #include <netinet/in.h>
  495. #include <netdb.h>
  496. #else
  497. #undef _WINSOCKAPI_
  498. #include <winsock.h>
  499. #endif
  500. #include <pwd.h>
  501. #include <string.h>
  502. /*
  503. * The full valid syntax for a POP mailbox specification for movemail
  504. * is "po:username:hostname". The ":hostname" is optional; if it is
  505. * omitted, the MAILHOST environment variable will be consulted. Note
  506. * that by the time popmail() is called the "po:" has been stripped
  507. * off of the front of the mailbox name.
  508. *
  509. * If the mailbox is in the form "po:username:hostname", then it is
  510. * modified by this function -- the second colon is replaced by a
  511. * null.
  512. *
  513. * Return a value suitable for passing to `exit'.
  514. */
  515. static int
  516. popmail (char *mailbox, char *outfile, bool preserve, char *password,
  517. bool reverse_order)
  518. {
  519. int nmsgs, nbytes;
  520. int i;
  521. int mbfi;
  522. FILE *mbf;
  523. popserver server;
  524. int start, end, increment;
  525. char *user, *hostname;
  526. user = mailbox;
  527. if ((hostname = strchr (mailbox, ':')))
  528. *hostname++ = '\0';
  529. server = pop_open (hostname, user, password, POP_NO_GETPASS);
  530. if (! server)
  531. {
  532. error ("Error connecting to POP server: %s", pop_error, 0);
  533. return EXIT_FAILURE;
  534. }
  535. if (pop_stat (server, &nmsgs, &nbytes))
  536. {
  537. error ("Error getting message count from POP server: %s", pop_error, 0);
  538. return EXIT_FAILURE;
  539. }
  540. if (!nmsgs)
  541. {
  542. pop_close (server);
  543. return EXIT_SUCCESS;
  544. }
  545. mbfi = open (outfile, O_WRONLY | O_BINARY | O_CREAT | O_EXCL, 0666);
  546. if (mbfi < 0)
  547. {
  548. pop_close (server);
  549. error ("Error in open: %s, %s", strerror (errno), outfile);
  550. return EXIT_FAILURE;
  551. }
  552. if (fchown (mbfi, getuid (), -1) != 0)
  553. {
  554. int fchown_errno = errno;
  555. struct stat st;
  556. if (fstat (mbfi, &st) != 0 || st.st_uid != getuid ())
  557. {
  558. pop_close (server);
  559. error ("Error in fchown: %s, %s", strerror (fchown_errno), outfile);
  560. return EXIT_FAILURE;
  561. }
  562. }
  563. mbf = fdopen (mbfi, "wb");
  564. if (!mbf)
  565. {
  566. pop_close (server);
  567. error ("Error in fdopen: %s", strerror (errno), 0);
  568. close (mbfi);
  569. unlink (outfile);
  570. return EXIT_FAILURE;
  571. }
  572. if (reverse_order)
  573. {
  574. start = nmsgs;
  575. end = 1;
  576. increment = -1;
  577. }
  578. else
  579. {
  580. start = 1;
  581. end = nmsgs;
  582. increment = 1;
  583. }
  584. for (i = start; i * increment <= end * increment; i += increment)
  585. if (! (mbx_delimit_begin (mbf)
  586. && pop_retr (server, i, mbf)
  587. && mbx_delimit_end (mbf)
  588. && fflush (mbf) == 0))
  589. {
  590. if (errno)
  591. error ("Error in POP retrieving: %s", strerror (errno), 0);
  592. pop_close (server);
  593. fclose (mbf);
  594. return EXIT_FAILURE;
  595. }
  596. if (fsync (mbfi) != 0 && errno != EINVAL)
  597. {
  598. error ("Error in fsync: %s", strerror (errno), 0);
  599. fclose (mbf);
  600. return EXIT_FAILURE;
  601. }
  602. if (fclose (mbf) != 0)
  603. {
  604. error ("Error in fclose: %s", strerror (errno), 0);
  605. return EXIT_FAILURE;
  606. }
  607. if (! preserve)
  608. for (i = 1; i <= nmsgs; i++)
  609. {
  610. if (pop_delete (server, i))
  611. {
  612. error ("Error from POP server: %s", pop_error, 0);
  613. pop_close (server);
  614. return EXIT_FAILURE;
  615. }
  616. }
  617. if (pop_quit (server))
  618. {
  619. error ("Error from POP server: %s", pop_error, 0);
  620. return EXIT_FAILURE;
  621. }
  622. return EXIT_SUCCESS;
  623. }
  624. static bool
  625. pop_retr (popserver server, int msgno, FILE *arg)
  626. {
  627. char *line;
  628. int ret;
  629. if (pop_retrieve_first (server, msgno, &line))
  630. {
  631. error ("Error from POP server: %s", pop_error, 0);
  632. errno = 0;
  633. return false;
  634. }
  635. while ((ret = pop_retrieve_next (server, &line)) >= 0)
  636. {
  637. if (! line)
  638. break;
  639. if (! mbx_write (line, ret, arg))
  640. {
  641. int write_errno = errno;
  642. pop_close (server);
  643. errno = write_errno;
  644. return false;
  645. }
  646. }
  647. if (ret)
  648. {
  649. error ("Error from POP server: %s", pop_error, 0);
  650. errno = 0;
  651. return false;
  652. }
  653. return true;
  654. }
  655. static bool
  656. mbx_write (char *line, int len, FILE *mbf)
  657. {
  658. #ifdef MOVEMAIL_QUOTE_POP_FROM_LINES
  659. /* Do this as a macro instead of using strcmp to save on execution time. */
  660. # define IS_FROM_LINE(a) ((a[0] == 'F') \
  661. && (a[1] == 'r') \
  662. && (a[2] == 'o') \
  663. && (a[3] == 'm') \
  664. && (a[4] == ' '))
  665. if (IS_FROM_LINE (line))
  666. {
  667. if (fputc ('>', mbf) < 0)
  668. return false;
  669. }
  670. #endif
  671. if (line[0] == '\037')
  672. {
  673. if (fputs ("^_", mbf) < 0)
  674. return false;
  675. line++;
  676. len--;
  677. }
  678. return fwrite (line, 1, len, mbf) == len && 0 <= fputc ('\n', mbf);
  679. }
  680. #ifdef WINDOWSNT
  681. /* Work around MS-Windows lack of support for %e or %T with a
  682. special-purpose strftime that assumes the exact format that
  683. movemail uses. */
  684. static size_t
  685. movemail_strftime (char *s, size_t size, char const *format,
  686. struct tm const *tm)
  687. {
  688. char fmt[size + 6], *q;
  689. const char *p;
  690. for (p = format, q = &fmt[0]; *p; )
  691. {
  692. if (*p == '%' && p[1] == 'e')
  693. {
  694. memcpy (q, "%d", 2);
  695. q += 2;
  696. p += 2;
  697. }
  698. else if (*p == '%' && p[1] == 'T')
  699. {
  700. memcpy (q, "%H:%M:%S", 8);
  701. q += 8;
  702. p += 2;
  703. }
  704. else if (*p == '%' && p[1] == '%')
  705. {
  706. memcpy (q, p, 2);
  707. q += 2;
  708. p += 2;
  709. }
  710. else
  711. *q++ = *p++;
  712. }
  713. size_t n = strftime (s, size, fmt, tm);
  714. char *mday = s + sizeof "From movemail Sun Jan " - 1;
  715. if (*mday == '0')
  716. *mday = ' ';
  717. return n;
  718. }
  719. # undef strftime
  720. # define strftime movemail_strftime
  721. #endif
  722. static bool
  723. mbx_delimit_begin (FILE *mbf)
  724. {
  725. time_t now = time (NULL);
  726. struct tm *ltime = localtime (&now);
  727. if (!ltime)
  728. return false;
  729. char fromline[100];
  730. if (! strftime (fromline, sizeof fromline,
  731. "From movemail %a %b %e %T %Y\n", ltime))
  732. {
  733. errno = EOVERFLOW;
  734. return false;
  735. }
  736. return 0 <= fputs (fromline, mbf);
  737. }
  738. static bool
  739. mbx_delimit_end (FILE *mbf)
  740. {
  741. return 0 <= putc ('\n', mbf);
  742. }
  743. #endif /* MAIL_USE_POP */