movemail.c 21 KB

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