movemail.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960
  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-2012
  4. Free Software 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 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. #ifdef HAVE_FCNTL_H
  58. #include <fcntl.h>
  59. #endif
  60. #include <string.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 wait(var) (*(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. #ifndef F_OK
  86. #define F_OK 0
  87. #define X_OK 1
  88. #define W_OK 2
  89. #define R_OK 4
  90. #endif
  91. #ifdef WINDOWSNT
  92. #include <sys/locking.h>
  93. #endif
  94. #ifdef MAIL_USE_LOCKF
  95. #define MAIL_USE_SYSTEM_LOCK
  96. #endif
  97. #ifdef MAIL_USE_FLOCK
  98. #define MAIL_USE_SYSTEM_LOCK
  99. #endif
  100. #ifdef MAIL_USE_MMDF
  101. extern int lk_open (), lk_close ();
  102. #endif
  103. #if !defined (MAIL_USE_SYSTEM_LOCK) && !defined (MAIL_USE_MMDF) && \
  104. (defined (HAVE_LIBMAIL) || defined (HAVE_LIBLOCKFILE)) && \
  105. defined (HAVE_MAILLOCK_H)
  106. #include <maillock.h>
  107. /* We can't use maillock unless we know what directory system mail
  108. files appear in. */
  109. #ifdef MAILDIR
  110. #define MAIL_USE_MAILLOCK
  111. static char *mail_spool_name (char *);
  112. #endif
  113. #endif
  114. #ifndef HAVE_STRERROR
  115. char *strerror (int);
  116. #endif
  117. static void fatal (const char *s1, const char *s2, const char *s3) NO_RETURN;
  118. static void error (const char *s1, const char *s2, const char *s3);
  119. static void pfatal_with_name (char *name) NO_RETURN;
  120. static void pfatal_and_delete (char *name) NO_RETURN;
  121. static char *concat (const char *s1, const char *s2, const char *s3);
  122. static long *xmalloc (unsigned int size);
  123. #ifdef MAIL_USE_POP
  124. static int popmail (char *mailbox, char *outfile, int preserve, char *password, int reverse_order);
  125. static int pop_retr (popserver server, int msgno, FILE *arg);
  126. static int mbx_write (char *line, int len, FILE *mbf);
  127. static int mbx_delimit_begin (FILE *mbf);
  128. static int mbx_delimit_end (FILE *mbf);
  129. #endif
  130. /* Nonzero means this is name of a lock file to delete on fatal error. */
  131. static char *delete_lockname;
  132. int
  133. main (int argc, char **argv)
  134. {
  135. char *inname, *outname;
  136. int indesc, outdesc;
  137. ssize_t nread;
  138. int wait_status;
  139. int c, preserve_mail = 0;
  140. #ifndef MAIL_USE_SYSTEM_LOCK
  141. struct stat st;
  142. int tem;
  143. char *lockname;
  144. char *tempname;
  145. size_t inname_dirlen;
  146. int desc;
  147. #endif /* not MAIL_USE_SYSTEM_LOCK */
  148. #ifdef MAIL_USE_MAILLOCK
  149. char *spool_name;
  150. #endif
  151. #ifdef MAIL_USE_POP
  152. int pop_reverse_order = 0;
  153. # define ARGSTR "pr"
  154. #else /* ! MAIL_USE_POP */
  155. # define ARGSTR "p"
  156. #endif /* MAIL_USE_POP */
  157. uid_t real_gid = getgid ();
  158. uid_t priv_gid = getegid ();
  159. #ifdef WINDOWSNT
  160. /* Ensure all file i/o is in binary mode. */
  161. _fmode = _O_BINARY;
  162. #endif
  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. #ifndef DIRECTORY_SEP
  229. #define DIRECTORY_SEP '/'
  230. #endif
  231. #ifndef IS_DIRECTORY_SEP
  232. #define IS_DIRECTORY_SEP(_c_) ((_c_) == DIRECTORY_SEP)
  233. #endif
  234. /* Use a lock file named after our first argument with .lock appended:
  235. If it exists, the mail file is locked. */
  236. /* Note: this locking mechanism is *required* by the mailer
  237. (on systems which use it) to prevent loss of mail.
  238. On systems that use a lock file, extracting the mail without locking
  239. WILL occasionally cause loss of mail due to timing errors!
  240. So, if creation of the lock file fails
  241. due to access permission on the mail spool directory,
  242. you simply MUST change the permission
  243. and/or make movemail a setgid program
  244. so it can create lock files properly.
  245. You might also wish to verify that your system is one
  246. which uses lock files for this purpose. Some systems use other methods.
  247. If your system uses the `flock' system call for mail locking,
  248. define MAIL_USE_SYSTEM_LOCK in config.h or the s-*.h file
  249. and recompile movemail. If the s- file for your system
  250. should define MAIL_USE_SYSTEM_LOCK but does not, send a bug report
  251. to bug-gnu-emacs@prep.ai.mit.edu so we can fix it. */
  252. lockname = concat (inname, ".lock", "");
  253. for (inname_dirlen = strlen (inname);
  254. inname_dirlen && !IS_DIRECTORY_SEP (inname[inname_dirlen - 1]);
  255. inname_dirlen--)
  256. continue;
  257. tempname = (char *) xmalloc (inname_dirlen + sizeof "EXXXXXX");
  258. while (1)
  259. {
  260. /* Create the lock file, but not under the lock file name. */
  261. /* Give up if cannot do that. */
  262. memcpy (tempname, inname, inname_dirlen);
  263. strcpy (tempname + inname_dirlen, "EXXXXXX");
  264. #ifdef HAVE_MKSTEMP
  265. desc = mkstemp (tempname);
  266. #else
  267. mktemp (tempname);
  268. if (!*tempname)
  269. desc = -1;
  270. else
  271. {
  272. unlink (tempname);
  273. desc = open (tempname, O_WRONLY | O_CREAT | O_EXCL, 0600);
  274. }
  275. #endif
  276. if (desc < 0)
  277. {
  278. int mkstemp_errno = errno;
  279. error ("error while creating what would become the lock file",
  280. 0, 0);
  281. errno = mkstemp_errno;
  282. pfatal_with_name (tempname);
  283. }
  284. close (desc);
  285. tem = link (tempname, lockname);
  286. #ifdef EPERM
  287. if (tem < 0 && errno == EPERM)
  288. fatal ("Unable to create hard link between %s and %s",
  289. tempname, lockname);
  290. #endif
  291. unlink (tempname);
  292. if (tem >= 0)
  293. break;
  294. sleep (1);
  295. /* If lock file is five minutes old, unlock it.
  296. Five minutes should be good enough to cope with crashes
  297. and wedgitude, and long enough to avoid being fooled
  298. by time differences between machines. */
  299. if (stat (lockname, &st) >= 0)
  300. {
  301. time_t now = time (0);
  302. if (st.st_ctime < now - 300)
  303. unlink (lockname);
  304. }
  305. }
  306. delete_lockname = lockname;
  307. }
  308. #endif /* not MAIL_USE_SYSTEM_LOCK */
  309. #endif /* not MAIL_USE_MMDF */
  310. if (fork () == 0)
  311. {
  312. int lockcount = 0;
  313. int status = 0;
  314. #if defined (MAIL_USE_MAILLOCK) && defined (HAVE_TOUCHLOCK)
  315. time_t touched_lock;
  316. # ifdef lint
  317. touched_lock = 0;
  318. # endif
  319. #endif
  320. if (setuid (getuid ()) < 0 || setregid (-1, real_gid) < 0)
  321. fatal ("Failed to drop privileges", 0, 0);
  322. #ifndef MAIL_USE_MMDF
  323. #ifdef MAIL_USE_SYSTEM_LOCK
  324. indesc = open (inname, O_RDWR);
  325. #else /* if not MAIL_USE_SYSTEM_LOCK */
  326. indesc = open (inname, O_RDONLY);
  327. #endif /* not MAIL_USE_SYSTEM_LOCK */
  328. #else /* MAIL_USE_MMDF */
  329. indesc = lk_open (inname, O_RDONLY, 0, 0, 10);
  330. #endif /* MAIL_USE_MMDF */
  331. if (indesc < 0)
  332. pfatal_with_name (inname);
  333. #ifdef BSD_SYSTEM
  334. /* In case movemail is setuid to root, make sure the user can
  335. read the output file. */
  336. /* This is desirable for all systems
  337. but I don't want to assume all have the umask system call */
  338. umask (umask (0) & 0333);
  339. #endif /* BSD_SYSTEM */
  340. outdesc = open (outname, O_WRONLY | O_CREAT | O_EXCL, 0666);
  341. if (outdesc < 0)
  342. pfatal_with_name (outname);
  343. if (setregid (-1, priv_gid) < 0)
  344. fatal ("Failed to regain privileges", 0, 0);
  345. /* This label exists so we can retry locking
  346. after a delay, if it got EAGAIN or EBUSY. */
  347. retry_lock:
  348. /* Try to lock it. */
  349. #ifdef MAIL_USE_MAILLOCK
  350. if (spool_name)
  351. {
  352. /* The "0 - " is to make it a negative number if maillock returns
  353. non-zero. */
  354. status = 0 - maillock (spool_name, 1);
  355. #ifdef HAVE_TOUCHLOCK
  356. touched_lock = time (0);
  357. #endif
  358. lockcount = 5;
  359. }
  360. else
  361. #endif /* MAIL_USE_MAILLOCK */
  362. {
  363. #ifdef MAIL_USE_SYSTEM_LOCK
  364. #ifdef MAIL_USE_LOCKF
  365. status = lockf (indesc, F_LOCK, 0);
  366. #else /* not MAIL_USE_LOCKF */
  367. #ifdef WINDOWSNT
  368. status = locking (indesc, LK_RLCK, -1L);
  369. #else
  370. status = flock (indesc, LOCK_EX);
  371. #endif
  372. #endif /* not MAIL_USE_LOCKF */
  373. #endif /* MAIL_USE_SYSTEM_LOCK */
  374. }
  375. /* If it fails, retry up to 5 times
  376. for certain failure codes. */
  377. if (status < 0)
  378. {
  379. if (++lockcount <= 5)
  380. {
  381. #ifdef EAGAIN
  382. if (errno == EAGAIN)
  383. {
  384. sleep (1);
  385. goto retry_lock;
  386. }
  387. #endif
  388. #ifdef EBUSY
  389. if (errno == EBUSY)
  390. {
  391. sleep (1);
  392. goto retry_lock;
  393. }
  394. #endif
  395. }
  396. pfatal_with_name (inname);
  397. }
  398. {
  399. char buf[1024];
  400. while (1)
  401. {
  402. nread = read (indesc, buf, sizeof buf);
  403. if (nread < 0)
  404. pfatal_with_name (inname);
  405. if (nread != write (outdesc, buf, nread))
  406. {
  407. int saved_errno = errno;
  408. unlink (outname);
  409. errno = saved_errno;
  410. pfatal_with_name (outname);
  411. }
  412. if (nread < sizeof buf)
  413. break;
  414. #if defined (MAIL_USE_MAILLOCK) && defined (HAVE_TOUCHLOCK)
  415. if (spool_name)
  416. {
  417. time_t now = time (0);
  418. if (now - touched_lock > 60)
  419. {
  420. touchlock ();
  421. touched_lock = now;
  422. }
  423. }
  424. #endif /* MAIL_USE_MAILLOCK */
  425. }
  426. }
  427. #ifdef BSD_SYSTEM
  428. if (fsync (outdesc) < 0)
  429. pfatal_and_delete (outname);
  430. #endif
  431. /* Prevent symlink attacks truncating other users' mailboxes */
  432. if (setregid (-1, real_gid) < 0)
  433. fatal ("Failed to drop privileges", 0, 0);
  434. /* Check to make sure no errors before we zap the inbox. */
  435. if (close (outdesc) != 0)
  436. pfatal_and_delete (outname);
  437. #ifdef MAIL_USE_SYSTEM_LOCK
  438. if (! preserve_mail)
  439. {
  440. if (ftruncate (indesc, 0L) != 0)
  441. pfatal_with_name (inname);
  442. }
  443. #endif /* MAIL_USE_SYSTEM_LOCK */
  444. #ifdef MAIL_USE_MMDF
  445. lk_close (indesc, 0, 0, 0);
  446. #else
  447. close (indesc);
  448. #endif
  449. #ifndef MAIL_USE_SYSTEM_LOCK
  450. if (! preserve_mail)
  451. {
  452. /* Delete the input file; if we can't, at least get rid of its
  453. contents. */
  454. #ifdef MAIL_UNLINK_SPOOL
  455. /* This is generally bad to do, because it destroys the permissions
  456. that were set on the file. Better to just empty the file. */
  457. if (unlink (inname) < 0 && errno != ENOENT)
  458. #endif /* MAIL_UNLINK_SPOOL */
  459. creat (inname, 0600);
  460. }
  461. #endif /* not MAIL_USE_SYSTEM_LOCK */
  462. /* End of mailbox truncation */
  463. if (setregid (-1, priv_gid) < 0)
  464. fatal ("Failed to regain privileges", 0, 0);
  465. #ifdef MAIL_USE_MAILLOCK
  466. /* This has to occur in the child, i.e., in the process that
  467. acquired the lock! */
  468. if (spool_name)
  469. mailunlock ();
  470. #endif
  471. exit (EXIT_SUCCESS);
  472. }
  473. wait (&wait_status);
  474. if (!WIFEXITED (wait_status))
  475. exit (EXIT_FAILURE);
  476. else if (WRETCODE (wait_status) != 0)
  477. exit (WRETCODE (wait_status));
  478. #if !defined (MAIL_USE_MMDF) && !defined (MAIL_USE_SYSTEM_LOCK)
  479. #ifdef MAIL_USE_MAILLOCK
  480. if (! spool_name)
  481. #endif /* MAIL_USE_MAILLOCK */
  482. unlink (lockname);
  483. #endif /* not MAIL_USE_MMDF and not MAIL_USE_SYSTEM_LOCK */
  484. #endif /* ! DISABLE_DIRECT_ACCESS */
  485. return EXIT_SUCCESS;
  486. }
  487. #ifdef MAIL_USE_MAILLOCK
  488. /* This function uses stat to confirm that the mail directory is
  489. identical to the directory of the input file, rather than just
  490. string-comparing the two paths, because one or both of them might
  491. be symbolic links pointing to some other directory. */
  492. static char *
  493. mail_spool_name (char *inname)
  494. {
  495. struct stat stat1, stat2;
  496. char *indir, *fname;
  497. int status;
  498. if (! (fname = strrchr (inname, '/')))
  499. return NULL;
  500. fname++;
  501. if (stat (MAILDIR, &stat1) < 0)
  502. return NULL;
  503. indir = (char *) xmalloc (fname - inname + 1);
  504. strncpy (indir, inname, fname - inname);
  505. indir[fname-inname] = '\0';
  506. status = stat (indir, &stat2);
  507. free (indir);
  508. if (status < 0)
  509. return NULL;
  510. if (stat1.st_dev == stat2.st_dev
  511. && stat1.st_ino == stat2.st_ino)
  512. return fname;
  513. return NULL;
  514. }
  515. #endif /* MAIL_USE_MAILLOCK */
  516. /* Print error message and exit. */
  517. static void
  518. fatal (const char *s1, const char *s2, const char *s3)
  519. {
  520. if (delete_lockname)
  521. unlink (delete_lockname);
  522. error (s1, s2, s3);
  523. exit (EXIT_FAILURE);
  524. }
  525. /* Print error message. `s1' is printf control string, `s2' and `s3'
  526. are args for it or null. */
  527. static void
  528. error (const char *s1, const char *s2, const char *s3)
  529. {
  530. fprintf (stderr, "movemail: ");
  531. if (s3)
  532. fprintf (stderr, s1, s2, s3);
  533. else if (s2)
  534. fprintf (stderr, s1, s2);
  535. else
  536. fprintf (stderr, "%s", s1);
  537. fprintf (stderr, "\n");
  538. }
  539. static void
  540. pfatal_with_name (char *name)
  541. {
  542. fatal ("%s for %s", strerror (errno), name);
  543. }
  544. static void
  545. pfatal_and_delete (char *name)
  546. {
  547. char *s = strerror (errno);
  548. unlink (name);
  549. fatal ("%s for %s", s, name);
  550. }
  551. /* Return a newly-allocated string whose contents concatenate those of s1, s2, s3. */
  552. static char *
  553. concat (const char *s1, const char *s2, const char *s3)
  554. {
  555. size_t len1 = strlen (s1), len2 = strlen (s2), len3 = strlen (s3);
  556. char *result = (char *) xmalloc (len1 + len2 + len3 + 1);
  557. strcpy (result, s1);
  558. strcpy (result + len1, s2);
  559. strcpy (result + len1 + len2, s3);
  560. *(result + len1 + len2 + len3) = 0;
  561. return result;
  562. }
  563. /* Like malloc but get fatal error if memory is exhausted. */
  564. static long *
  565. xmalloc (unsigned int size)
  566. {
  567. long *result = (long *) malloc (size);
  568. if (!result)
  569. fatal ("virtual memory exhausted", 0, 0);
  570. return result;
  571. }
  572. /* This is the guts of the interface to the Post Office Protocol. */
  573. #ifdef MAIL_USE_POP
  574. #ifndef WINDOWSNT
  575. #include <sys/socket.h>
  576. #include <netinet/in.h>
  577. #include <netdb.h>
  578. #else
  579. #undef _WINSOCKAPI_
  580. #include <winsock.h>
  581. #endif
  582. #include <pwd.h>
  583. #include <string.h>
  584. #define NOTOK (-1)
  585. #define OK 0
  586. static char Errmsg[200]; /* POP errors, at least, can exceed
  587. the original length of 80. */
  588. /*
  589. * The full valid syntax for a POP mailbox specification for movemail
  590. * is "po:username:hostname". The ":hostname" is optional; if it is
  591. * omitted, the MAILHOST environment variable will be consulted. Note
  592. * that by the time popmail() is called the "po:" has been stripped
  593. * off of the front of the mailbox name.
  594. *
  595. * If the mailbox is in the form "po:username:hostname", then it is
  596. * modified by this function -- the second colon is replaced by a
  597. * null.
  598. *
  599. * Return a value suitable for passing to `exit'.
  600. */
  601. static int
  602. popmail (char *mailbox, char *outfile, int preserve, char *password, int reverse_order)
  603. {
  604. int nmsgs, nbytes;
  605. register int i;
  606. int mbfi;
  607. FILE *mbf;
  608. char *getenv (const char *);
  609. popserver server;
  610. int start, end, increment;
  611. char *user, *hostname;
  612. user = mailbox;
  613. if ((hostname = strchr (mailbox, ':')))
  614. *hostname++ = '\0';
  615. server = pop_open (hostname, user, password, POP_NO_GETPASS);
  616. if (! server)
  617. {
  618. error ("Error connecting to POP server: %s", pop_error, 0);
  619. return EXIT_FAILURE;
  620. }
  621. if (pop_stat (server, &nmsgs, &nbytes))
  622. {
  623. error ("Error getting message count from POP server: %s", pop_error, 0);
  624. return EXIT_FAILURE;
  625. }
  626. if (!nmsgs)
  627. {
  628. pop_close (server);
  629. return EXIT_SUCCESS;
  630. }
  631. mbfi = open (outfile, O_WRONLY | O_CREAT | O_EXCL, 0666);
  632. if (mbfi < 0)
  633. {
  634. pop_close (server);
  635. error ("Error in open: %s, %s", strerror (errno), outfile);
  636. return EXIT_FAILURE;
  637. }
  638. if (fchown (mbfi, getuid (), -1) != 0)
  639. {
  640. int fchown_errno = errno;
  641. struct stat st;
  642. if (fstat (mbfi, &st) != 0 || st.st_uid != getuid ())
  643. {
  644. pop_close (server);
  645. error ("Error in fchown: %s, %s", strerror (fchown_errno), outfile);
  646. return EXIT_FAILURE;
  647. }
  648. }
  649. if ((mbf = fdopen (mbfi, "wb")) == NULL)
  650. {
  651. pop_close (server);
  652. error ("Error in fdopen: %s", strerror (errno), 0);
  653. close (mbfi);
  654. unlink (outfile);
  655. return EXIT_FAILURE;
  656. }
  657. if (reverse_order)
  658. {
  659. start = nmsgs;
  660. end = 1;
  661. increment = -1;
  662. }
  663. else
  664. {
  665. start = 1;
  666. end = nmsgs;
  667. increment = 1;
  668. }
  669. for (i = start; i * increment <= end * increment; i += increment)
  670. {
  671. mbx_delimit_begin (mbf);
  672. if (pop_retr (server, i, mbf) != OK)
  673. {
  674. error ("%s", Errmsg, 0);
  675. close (mbfi);
  676. return EXIT_FAILURE;
  677. }
  678. mbx_delimit_end (mbf);
  679. fflush (mbf);
  680. if (ferror (mbf))
  681. {
  682. error ("Error in fflush: %s", strerror (errno), 0);
  683. pop_close (server);
  684. close (mbfi);
  685. return EXIT_FAILURE;
  686. }
  687. }
  688. /* On AFS, a call to write only modifies the file in the local
  689. * workstation's AFS cache. The changes are not written to the server
  690. * until a call to fsync or close is made. Users with AFS home
  691. * directories have lost mail when over quota because these checks were
  692. * not made in previous versions of movemail. */
  693. #ifdef BSD_SYSTEM
  694. if (fsync (mbfi) < 0)
  695. {
  696. error ("Error in fsync: %s", strerror (errno), 0);
  697. return EXIT_FAILURE;
  698. }
  699. #endif
  700. if (close (mbfi) == -1)
  701. {
  702. error ("Error in close: %s", strerror (errno), 0);
  703. return EXIT_FAILURE;
  704. }
  705. if (! preserve)
  706. for (i = 1; i <= nmsgs; i++)
  707. {
  708. if (pop_delete (server, i))
  709. {
  710. error ("Error from POP server: %s", pop_error, 0);
  711. pop_close (server);
  712. return EXIT_FAILURE;
  713. }
  714. }
  715. if (pop_quit (server))
  716. {
  717. error ("Error from POP server: %s", pop_error, 0);
  718. return EXIT_FAILURE;
  719. }
  720. return EXIT_SUCCESS;
  721. }
  722. static int
  723. pop_retr (popserver server, int msgno, FILE *arg)
  724. {
  725. char *line;
  726. int ret;
  727. if (pop_retrieve_first (server, msgno, &line))
  728. {
  729. char *msg = concat ("Error from POP server: ", pop_error, "");
  730. strncpy (Errmsg, msg, sizeof (Errmsg));
  731. Errmsg[sizeof (Errmsg)-1] = '\0';
  732. free (msg);
  733. return (NOTOK);
  734. }
  735. while ((ret = pop_retrieve_next (server, &line)) >= 0)
  736. {
  737. if (! line)
  738. break;
  739. if (mbx_write (line, ret, arg) != OK)
  740. {
  741. strcpy (Errmsg, strerror (errno));
  742. pop_close (server);
  743. return (NOTOK);
  744. }
  745. }
  746. if (ret)
  747. {
  748. char *msg = concat ("Error from POP server: ", pop_error, "");
  749. strncpy (Errmsg, msg, sizeof (Errmsg));
  750. Errmsg[sizeof (Errmsg)-1] = '\0';
  751. free (msg);
  752. return (NOTOK);
  753. }
  754. return (OK);
  755. }
  756. static int
  757. mbx_write (char *line, int len, FILE *mbf)
  758. {
  759. #ifdef MOVEMAIL_QUOTE_POP_FROM_LINES
  760. /* Do this as a macro instead of using strcmp to save on execution time. */
  761. # define IS_FROM_LINE(a) ((a[0] == 'F') \
  762. && (a[1] == 'r') \
  763. && (a[2] == 'o') \
  764. && (a[3] == 'm') \
  765. && (a[4] == ' '))
  766. if (IS_FROM_LINE (line))
  767. {
  768. if (fputc ('>', mbf) == EOF)
  769. return (NOTOK);
  770. }
  771. #endif
  772. if (line[0] == '\037')
  773. {
  774. if (fputs ("^_", mbf) == EOF)
  775. return (NOTOK);
  776. line++;
  777. len--;
  778. }
  779. if (fwrite (line, 1, len, mbf) != len)
  780. return (NOTOK);
  781. if (fputc (0x0a, mbf) == EOF)
  782. return (NOTOK);
  783. return (OK);
  784. }
  785. static int
  786. mbx_delimit_begin (FILE *mbf)
  787. {
  788. time_t now;
  789. struct tm *ltime;
  790. char fromline[40] = "From movemail ";
  791. now = time (NULL);
  792. ltime = localtime (&now);
  793. strcat (fromline, asctime (ltime));
  794. if (fputs (fromline, mbf) == EOF)
  795. return (NOTOK);
  796. return (OK);
  797. }
  798. static int
  799. mbx_delimit_end (FILE *mbf)
  800. {
  801. if (putc ('\n', mbf) == EOF)
  802. return (NOTOK);
  803. return (OK);
  804. }
  805. #endif /* MAIL_USE_POP */
  806. #ifndef HAVE_STRERROR
  807. char *
  808. strerror (errnum)
  809. int errnum;
  810. {
  811. extern char *sys_errlist[];
  812. extern int sys_nerr;
  813. if (errnum >= 0 && errnum < sys_nerr)
  814. return sys_errlist[errnum];
  815. return (char *) "Unknown error";
  816. }
  817. #endif /* ! HAVE_STRERROR */
  818. /* movemail.c ends here */