Merge from emacs-23.
[bpt/emacs.git] / lib-src / movemail.c
1 /* movemail foo bar -- move file foo to file bar,
2 locking file foo the way /bin/mail respects.
3 Copyright (C) 1986, 1992, 1993, 1994, 1996, 1999, 2001, 2002, 2003, 2004,
4 2005, 2006, 2007, 2008, 2009, 2010, 2011 Free Software Foundation, Inc.
5
6 This file is part of GNU Emacs.
7
8 GNU Emacs is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
12
13 GNU Emacs is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
20
21
22 /* Important notice: defining MAIL_USE_FLOCK or MAIL_USE_LOCKF *will
23 cause loss of mail* if you do it on a system that does not normally
24 use flock as its way of interlocking access to inbox files. The
25 setting of MAIL_USE_FLOCK and MAIL_USE_LOCKF *must agree* with the
26 system's own conventions. It is not a choice that is up to you.
27
28 So, if your system uses lock files rather than flock, then the only way
29 you can get proper operation is to enable movemail to write lockfiles there.
30 This means you must either give that directory access modes
31 that permit everyone to write lockfiles in it, or you must make movemail
32 a setuid or setgid program. */
33
34 /*
35 * Modified January, 1986 by Michael R. Gretzinger (Project Athena)
36 *
37 * Added POP (Post Office Protocol) service. When compiled -DMAIL_USE_POP
38 * movemail will accept input filename arguments of the form
39 * "po:username". This will cause movemail to open a connection to
40 * a pop server running on $MAILHOST (environment variable). Movemail
41 * must be setuid to root in order to work with POP.
42 *
43 * New module: popmail.c
44 * Modified routines:
45 * main - added code within #ifdef MAIL_USE_POP; added setuid (getuid ())
46 * after POP code.
47 * New routines in movemail.c:
48 * get_errmsg - return pointer to system error message
49 *
50 * Modified August, 1993 by Jonathan Kamens (OpenVision Technologies)
51 *
52 * Move all of the POP code into a separate file, "pop.c".
53 * Use strerror instead of get_errmsg.
54 *
55 */
56
57 #include <config.h>
58 #include <sys/types.h>
59 #include <sys/stat.h>
60 #include <sys/file.h>
61 #include <stdio.h>
62 #include <errno.h>
63 #include <time.h>
64
65 #include <getopt.h>
66 #include <unistd.h>
67 #ifdef HAVE_FCNTL_H
68 #include <fcntl.h>
69 #endif
70 #ifdef HAVE_STRING_H
71 #include <string.h>
72 #endif
73 #include "syswait.h"
74 #ifdef MAIL_USE_POP
75 #include "pop.h"
76 #endif
77
78 #ifdef MSDOS
79 #undef access
80 #endif /* MSDOS */
81
82 #ifndef DIRECTORY_SEP
83 #define DIRECTORY_SEP '/'
84 #endif
85 #ifndef IS_DIRECTORY_SEP
86 #define IS_DIRECTORY_SEP(_c_) ((_c_) == DIRECTORY_SEP)
87 #endif
88
89 #ifdef WINDOWSNT
90 #include "ntlib.h"
91 #undef access
92 #undef unlink
93 #define fork() 0
94 #define wait(var) (*(var) = 0)
95 /* Unfortunately, Samba doesn't seem to properly lock Unix files even
96 though the locking call succeeds (and indeed blocks local access from
97 other NT programs). If you have direct file access using an NFS
98 client or something other than Samba, the locking call might work
99 properly - make sure it does before you enable this!
100
101 [18-Feb-97 andrewi] I now believe my comment above to be incorrect,
102 since it was based on a misunderstanding of how locking calls are
103 implemented and used on Unix. */
104 //#define DISABLE_DIRECT_ACCESS
105
106 #include <fcntl.h>
107 #endif /* WINDOWSNT */
108
109 #ifndef F_OK
110 #define F_OK 0
111 #define X_OK 1
112 #define W_OK 2
113 #define R_OK 4
114 #endif
115
116 #ifdef WINDOWSNT
117 #include <sys/locking.h>
118 #endif
119
120 #ifdef MAIL_USE_LOCKF
121 #define MAIL_USE_SYSTEM_LOCK
122 #endif
123
124 #ifdef MAIL_USE_FLOCK
125 #define MAIL_USE_SYSTEM_LOCK
126 #endif
127
128 #ifdef MAIL_USE_MMDF
129 extern int lk_open (), lk_close ();
130 #endif
131
132 #if !defined (MAIL_USE_SYSTEM_LOCK) && !defined (MAIL_USE_MMDF) && \
133 (defined (HAVE_LIBMAIL) || defined (HAVE_LIBLOCKFILE)) && \
134 defined (HAVE_MAILLOCK_H)
135 #include <maillock.h>
136 /* We can't use maillock unless we know what directory system mail
137 files appear in. */
138 #ifdef MAILDIR
139 #define MAIL_USE_MAILLOCK
140 static char *mail_spool_name ();
141 #endif
142 #endif
143
144 #ifndef HAVE_STRERROR
145 char *strerror (int);
146 #endif
147
148 static void fatal (const char *s1, const char *s2, const char *s3) NO_RETURN;
149 static void error (const char *s1, const char *s2, const char *s3);
150 static void pfatal_with_name (char *name) NO_RETURN;
151 static void pfatal_and_delete (char *name) NO_RETURN;
152 static char *concat (const char *s1, const char *s2, const char *s3);
153 static long *xmalloc (unsigned int size);
154 #ifdef MAIL_USE_POP
155 static int popmail (char *mailbox, char *outfile, int preserve, char *password, int reverse_order);
156 static int pop_retr (popserver server, int msgno, FILE *arg);
157 static int mbx_write (char *line, int len, FILE *mbf);
158 static int mbx_delimit_begin (FILE *mbf);
159 static int mbx_delimit_end (FILE *mbf);
160 #endif
161
162 /* Nonzero means this is name of a lock file to delete on fatal error. */
163 char *delete_lockname;
164
165 int
166 main (int argc, char **argv)
167 {
168 char *inname, *outname;
169 int indesc, outdesc;
170 ssize_t nread;
171 int status;
172 int c, preserve_mail = 0;
173
174 #ifndef MAIL_USE_SYSTEM_LOCK
175 struct stat st;
176 long now;
177 int tem;
178 char *lockname, *p;
179 char *tempname;
180 int desc;
181 #endif /* not MAIL_USE_SYSTEM_LOCK */
182
183 #ifdef MAIL_USE_MAILLOCK
184 char *spool_name;
185 #endif
186
187 #ifdef MAIL_USE_POP
188 int pop_reverse_order = 0;
189 # define ARGSTR "pr"
190 #else /* ! MAIL_USE_POP */
191 # define ARGSTR "p"
192 #endif /* MAIL_USE_POP */
193
194 uid_t real_gid = getgid();
195 uid_t priv_gid = getegid();
196
197 #ifdef WINDOWSNT
198 /* Ensure all file i/o is in binary mode. */
199 _fmode = _O_BINARY;
200 #endif
201
202 delete_lockname = 0;
203
204 while ((c = getopt (argc, argv, ARGSTR)) != EOF)
205 {
206 switch (c) {
207 #ifdef MAIL_USE_POP
208 case 'r':
209 pop_reverse_order = 1;
210 break;
211 #endif
212 case 'p':
213 preserve_mail++;
214 break;
215 default:
216 exit (EXIT_FAILURE);
217 }
218 }
219
220 if (
221 #ifdef MAIL_USE_POP
222 (argc - optind < 2) || (argc - optind > 3)
223 #else
224 (argc - optind != 2)
225 #endif
226 )
227 {
228 #ifdef MAIL_USE_POP
229 fprintf (stderr, "Usage: movemail [-p] [-r] inbox destfile%s\n",
230 " [POP-password]");
231 #else
232 fprintf (stderr, "Usage: movemail [-p] inbox destfile%s\n", "");
233 #endif
234 exit (EXIT_FAILURE);
235 }
236
237 inname = argv[optind];
238 outname = argv[optind+1];
239
240 #ifdef MAIL_USE_MMDF
241 mmdf_init (argv[0]);
242 #endif
243
244 if (*outname == 0)
245 fatal ("Destination file name is empty", 0, 0);
246
247 #ifdef MAIL_USE_POP
248 if (!strncmp (inname, "po:", 3))
249 {
250 int status;
251
252 status = popmail (inname + 3, outname, preserve_mail,
253 (argc - optind == 3) ? argv[optind+2] : NULL,
254 pop_reverse_order);
255 exit (status);
256 }
257
258 if (setuid (getuid ()) < 0)
259 fatal ("Failed to drop privileges", 0, 0);
260
261 #endif /* MAIL_USE_POP */
262
263 #ifndef DISABLE_DIRECT_ACCESS
264 #ifndef MAIL_USE_MMDF
265 #ifndef MAIL_USE_SYSTEM_LOCK
266 #ifdef MAIL_USE_MAILLOCK
267 spool_name = mail_spool_name (inname);
268 if (! spool_name)
269 #endif
270 {
271 /* Use a lock file named after our first argument with .lock appended:
272 If it exists, the mail file is locked. */
273 /* Note: this locking mechanism is *required* by the mailer
274 (on systems which use it) to prevent loss of mail.
275
276 On systems that use a lock file, extracting the mail without locking
277 WILL occasionally cause loss of mail due to timing errors!
278
279 So, if creation of the lock file fails
280 due to access permission on the mail spool directory,
281 you simply MUST change the permission
282 and/or make movemail a setgid program
283 so it can create lock files properly.
284
285 You might also wish to verify that your system is one
286 which uses lock files for this purpose. Some systems use other methods.
287
288 If your system uses the `flock' system call for mail locking,
289 define MAIL_USE_SYSTEM_LOCK in config.h or the s-*.h file
290 and recompile movemail. If the s- file for your system
291 should define MAIL_USE_SYSTEM_LOCK but does not, send a bug report
292 to bug-gnu-emacs@prep.ai.mit.edu so we can fix it. */
293
294 lockname = concat (inname, ".lock", "");
295 tempname = (char *) xmalloc (strlen (inname) + strlen ("EXXXXXX") + 1);
296 strcpy (tempname, inname);
297 p = tempname + strlen (tempname);
298 while (p != tempname && !IS_DIRECTORY_SEP (p[-1]))
299 p--;
300 *p = 0;
301 strcpy (p, "EXXXXXX");
302 mktemp (tempname);
303 unlink (tempname);
304
305 while (1)
306 {
307 /* Create the lock file, but not under the lock file name. */
308 /* Give up if cannot do that. */
309 desc = open (tempname, O_WRONLY | O_CREAT | O_EXCL, 0666);
310 if (desc < 0)
311 {
312 char *message = (char *) xmalloc (strlen (tempname) + 50);
313 sprintf (message, "creating %s, which would become the lock file",
314 tempname);
315 pfatal_with_name (message);
316 }
317 close (desc);
318
319 tem = link (tempname, lockname);
320
321 #ifdef EPERM
322 if (tem < 0 && errno == EPERM)
323 fatal ("Unable to create hard link between %s and %s",
324 tempname, lockname);
325 #endif
326
327 unlink (tempname);
328 if (tem >= 0)
329 break;
330 sleep (1);
331
332 /* If lock file is five minutes old, unlock it.
333 Five minutes should be good enough to cope with crashes
334 and wedgitude, and long enough to avoid being fooled
335 by time differences between machines. */
336 if (stat (lockname, &st) >= 0)
337 {
338 now = time (0);
339 if (st.st_ctime < now - 300)
340 unlink (lockname);
341 }
342 }
343
344 delete_lockname = lockname;
345 }
346 #endif /* not MAIL_USE_SYSTEM_LOCK */
347 #endif /* not MAIL_USE_MMDF */
348
349 if (fork () == 0)
350 {
351 int lockcount = 0;
352 int status = 0;
353 #if defined (MAIL_USE_MAILLOCK) && defined (HAVE_TOUCHLOCK)
354 time_t touched_lock, now;
355 #endif
356
357 if (setuid (getuid ()) < 0 || setregid (-1, real_gid) < 0)
358 fatal ("Failed to drop privileges", 0, 0);
359
360 #ifndef MAIL_USE_MMDF
361 #ifdef MAIL_USE_SYSTEM_LOCK
362 indesc = open (inname, O_RDWR);
363 #else /* if not MAIL_USE_SYSTEM_LOCK */
364 indesc = open (inname, O_RDONLY);
365 #endif /* not MAIL_USE_SYSTEM_LOCK */
366 #else /* MAIL_USE_MMDF */
367 indesc = lk_open (inname, O_RDONLY, 0, 0, 10);
368 #endif /* MAIL_USE_MMDF */
369
370 if (indesc < 0)
371 pfatal_with_name (inname);
372
373 #ifdef BSD_SYSTEM
374 /* In case movemail is setuid to root, make sure the user can
375 read the output file. */
376 /* This is desirable for all systems
377 but I don't want to assume all have the umask system call */
378 umask (umask (0) & 0333);
379 #endif /* BSD_SYSTEM */
380 outdesc = open (outname, O_WRONLY | O_CREAT | O_EXCL, 0666);
381 if (outdesc < 0)
382 pfatal_with_name (outname);
383
384 if (setregid (-1, priv_gid) < 0)
385 fatal ("Failed to regain privileges", 0, 0);
386
387 /* This label exists so we can retry locking
388 after a delay, if it got EAGAIN or EBUSY. */
389 retry_lock:
390
391 /* Try to lock it. */
392 #ifdef MAIL_USE_MAILLOCK
393 if (spool_name)
394 {
395 /* The "0 - " is to make it a negative number if maillock returns
396 non-zero. */
397 status = 0 - maillock (spool_name, 1);
398 #ifdef HAVE_TOUCHLOCK
399 touched_lock = time (0);
400 #endif
401 lockcount = 5;
402 }
403 else
404 #endif /* MAIL_USE_MAILLOCK */
405 {
406 #ifdef MAIL_USE_SYSTEM_LOCK
407 #ifdef MAIL_USE_LOCKF
408 status = lockf (indesc, F_LOCK, 0);
409 #else /* not MAIL_USE_LOCKF */
410 #ifdef WINDOWSNT
411 status = locking (indesc, LK_RLCK, -1L);
412 #else
413 status = flock (indesc, LOCK_EX);
414 #endif
415 #endif /* not MAIL_USE_LOCKF */
416 #endif /* MAIL_USE_SYSTEM_LOCK */
417 }
418
419 /* If it fails, retry up to 5 times
420 for certain failure codes. */
421 if (status < 0)
422 {
423 if (++lockcount <= 5)
424 {
425 #ifdef EAGAIN
426 if (errno == EAGAIN)
427 {
428 sleep (1);
429 goto retry_lock;
430 }
431 #endif
432 #ifdef EBUSY
433 if (errno == EBUSY)
434 {
435 sleep (1);
436 goto retry_lock;
437 }
438 #endif
439 }
440
441 pfatal_with_name (inname);
442 }
443
444 {
445 char buf[1024];
446
447 while (1)
448 {
449 nread = read (indesc, buf, sizeof buf);
450 if (nread < 0)
451 pfatal_with_name (inname);
452 if (nread != write (outdesc, buf, nread))
453 {
454 int saved_errno = errno;
455 unlink (outname);
456 errno = saved_errno;
457 pfatal_with_name (outname);
458 }
459 if (nread < sizeof buf)
460 break;
461 #if defined (MAIL_USE_MAILLOCK) && defined (HAVE_TOUCHLOCK)
462 if (spool_name)
463 {
464 now = time (0);
465 if (now - touched_lock > 60)
466 {
467 touchlock ();
468 touched_lock = now;
469 }
470 }
471 #endif /* MAIL_USE_MAILLOCK */
472 }
473 }
474
475 #ifdef BSD_SYSTEM
476 if (fsync (outdesc) < 0)
477 pfatal_and_delete (outname);
478 #endif
479
480 /* Prevent symlink attacks truncating other users' mailboxes */
481 if (setregid (-1, real_gid) < 0)
482 fatal ("Failed to drop privileges", 0, 0);
483
484 /* Check to make sure no errors before we zap the inbox. */
485 if (close (outdesc) != 0)
486 pfatal_and_delete (outname);
487
488 #ifdef MAIL_USE_SYSTEM_LOCK
489 if (! preserve_mail)
490 {
491 if (ftruncate (indesc, 0L) != 0)
492 pfatal_with_name (inname);
493 }
494 #endif /* MAIL_USE_SYSTEM_LOCK */
495
496 #ifdef MAIL_USE_MMDF
497 lk_close (indesc, 0, 0, 0);
498 #else
499 close (indesc);
500 #endif
501
502 #ifndef MAIL_USE_SYSTEM_LOCK
503 if (! preserve_mail)
504 {
505 /* Delete the input file; if we can't, at least get rid of its
506 contents. */
507 #ifdef MAIL_UNLINK_SPOOL
508 /* This is generally bad to do, because it destroys the permissions
509 that were set on the file. Better to just empty the file. */
510 if (unlink (inname) < 0 && errno != ENOENT)
511 #endif /* MAIL_UNLINK_SPOOL */
512 creat (inname, 0600);
513 }
514 #endif /* not MAIL_USE_SYSTEM_LOCK */
515
516 /* End of mailbox truncation */
517 if (setregid (-1, priv_gid) < 0)
518 fatal ("Failed to regain privileges", 0, 0);
519
520 #ifdef MAIL_USE_MAILLOCK
521 /* This has to occur in the child, i.e., in the process that
522 acquired the lock! */
523 if (spool_name)
524 mailunlock ();
525 #endif
526 exit (EXIT_SUCCESS);
527 }
528
529 wait (&status);
530 if (!WIFEXITED (status))
531 exit (EXIT_FAILURE);
532 else if (WRETCODE (status) != 0)
533 exit (WRETCODE (status));
534
535 #if !defined (MAIL_USE_MMDF) && !defined (MAIL_USE_SYSTEM_LOCK)
536 #ifdef MAIL_USE_MAILLOCK
537 if (! spool_name)
538 #endif /* MAIL_USE_MAILLOCK */
539 unlink (lockname);
540 #endif /* not MAIL_USE_MMDF and not MAIL_USE_SYSTEM_LOCK */
541
542 #endif /* ! DISABLE_DIRECT_ACCESS */
543
544 return EXIT_SUCCESS;
545 }
546
547 #ifdef MAIL_USE_MAILLOCK
548 /* This function uses stat to confirm that the mail directory is
549 identical to the directory of the input file, rather than just
550 string-comparing the two paths, because one or both of them might
551 be symbolic links pointing to some other directory. */
552 static char *
553 mail_spool_name (char *inname)
554 {
555 struct stat stat1, stat2;
556 char *indir, *fname;
557 int status;
558
559 if (! (fname = strrchr (inname, '/')))
560 return NULL;
561
562 fname++;
563
564 if (stat (MAILDIR, &stat1) < 0)
565 return NULL;
566
567 indir = (char *) xmalloc (fname - inname + 1);
568 strncpy (indir, inname, fname - inname);
569 indir[fname-inname] = '\0';
570
571
572 status = stat (indir, &stat2);
573
574 free (indir);
575
576 if (status < 0)
577 return NULL;
578
579 if (stat1.st_dev == stat2.st_dev
580 && stat1.st_ino == stat2.st_ino)
581 return fname;
582
583 return NULL;
584 }
585 #endif /* MAIL_USE_MAILLOCK */
586 \f
587 /* Print error message and exit. */
588
589 static void
590 fatal (const char *s1, const char *s2, const char *s3)
591 {
592 if (delete_lockname)
593 unlink (delete_lockname);
594 error (s1, s2, s3);
595 exit (EXIT_FAILURE);
596 }
597
598 /* Print error message. `s1' is printf control string, `s2' and `s3'
599 are args for it or null. */
600
601 static void
602 error (const char *s1, const char *s2, const char *s3)
603 {
604 fprintf (stderr, "movemail: ");
605 if (s3)
606 fprintf (stderr, s1, s2, s3);
607 else if (s2)
608 fprintf (stderr, s1, s2);
609 else
610 fprintf (stderr, "%s", s1);
611 fprintf (stderr, "\n");
612 }
613
614 static void
615 pfatal_with_name (char *name)
616 {
617 fatal ("%s for %s", strerror (errno), name);
618 }
619
620 static void
621 pfatal_and_delete (char *name)
622 {
623 char *s = strerror (errno);
624 unlink (name);
625 fatal ("%s for %s", s, name);
626 }
627
628 /* Return a newly-allocated string whose contents concatenate those of s1, s2, s3. */
629
630 static char *
631 concat (const char *s1, const char *s2, const char *s3)
632 {
633 size_t len1 = strlen (s1), len2 = strlen (s2), len3 = strlen (s3);
634 char *result = (char *) xmalloc (len1 + len2 + len3 + 1);
635
636 strcpy (result, s1);
637 strcpy (result + len1, s2);
638 strcpy (result + len1 + len2, s3);
639 *(result + len1 + len2 + len3) = 0;
640
641 return result;
642 }
643
644 /* Like malloc but get fatal error if memory is exhausted. */
645
646 static long *
647 xmalloc (unsigned int size)
648 {
649 long *result = (long *) malloc (size);
650 if (!result)
651 fatal ("virtual memory exhausted", 0, 0);
652 return result;
653 }
654 \f
655 /* This is the guts of the interface to the Post Office Protocol. */
656
657 #ifdef MAIL_USE_POP
658
659 #ifndef WINDOWSNT
660 #include <sys/socket.h>
661 #include <netinet/in.h>
662 #include <netdb.h>
663 #else
664 #undef _WINSOCKAPI_
665 #include <winsock.h>
666 #endif
667 #include <pwd.h>
668 #include <string.h>
669
670 #define NOTOK (-1)
671 #define OK 0
672 #define DONE 1
673
674 char *progname;
675 FILE *sfi;
676 FILE *sfo;
677 char ibuffer[BUFSIZ];
678 char obuffer[BUFSIZ];
679 char Errmsg[200]; /* POP errors, at least, can exceed
680 the original length of 80. */
681
682 /*
683 * The full valid syntax for a POP mailbox specification for movemail
684 * is "po:username:hostname". The ":hostname" is optional; if it is
685 * omitted, the MAILHOST environment variable will be consulted. Note
686 * that by the time popmail() is called the "po:" has been stripped
687 * off of the front of the mailbox name.
688 *
689 * If the mailbox is in the form "po:username:hostname", then it is
690 * modified by this function -- the second colon is replaced by a
691 * null.
692 *
693 * Return a value suitable for passing to `exit'.
694 */
695
696 static int
697 popmail (char *mailbox, char *outfile, int preserve, char *password, int reverse_order)
698 {
699 int nmsgs, nbytes;
700 register int i;
701 int mbfi;
702 FILE *mbf;
703 char *getenv (const char *);
704 popserver server;
705 int start, end, increment;
706 char *user, *hostname;
707
708 user = mailbox;
709 if ((hostname = strchr (mailbox, ':')))
710 *hostname++ = '\0';
711
712 server = pop_open (hostname, user, password, POP_NO_GETPASS);
713 if (! server)
714 {
715 error ("Error connecting to POP server: %s", pop_error, 0);
716 return EXIT_FAILURE;
717 }
718
719 if (pop_stat (server, &nmsgs, &nbytes))
720 {
721 error ("Error getting message count from POP server: %s", pop_error, 0);
722 return EXIT_FAILURE;
723 }
724
725 if (!nmsgs)
726 {
727 pop_close (server);
728 return EXIT_SUCCESS;
729 }
730
731 mbfi = open (outfile, O_WRONLY | O_CREAT | O_EXCL, 0666);
732 if (mbfi < 0)
733 {
734 pop_close (server);
735 error ("Error in open: %s, %s", strerror (errno), outfile);
736 return EXIT_FAILURE;
737 }
738 fchown (mbfi, getuid (), -1);
739
740 if ((mbf = fdopen (mbfi, "wb")) == NULL)
741 {
742 pop_close (server);
743 error ("Error in fdopen: %s", strerror (errno), 0);
744 close (mbfi);
745 unlink (outfile);
746 return EXIT_FAILURE;
747 }
748
749 if (reverse_order)
750 {
751 start = nmsgs;
752 end = 1;
753 increment = -1;
754 }
755 else
756 {
757 start = 1;
758 end = nmsgs;
759 increment = 1;
760 }
761
762 for (i = start; i * increment <= end * increment; i += increment)
763 {
764 mbx_delimit_begin (mbf);
765 if (pop_retr (server, i, mbf) != OK)
766 {
767 error ("%s", Errmsg, 0);
768 close (mbfi);
769 return EXIT_FAILURE;
770 }
771 mbx_delimit_end (mbf);
772 fflush (mbf);
773 if (ferror (mbf))
774 {
775 error ("Error in fflush: %s", strerror (errno), 0);
776 pop_close (server);
777 close (mbfi);
778 return EXIT_FAILURE;
779 }
780 }
781
782 /* On AFS, a call to write only modifies the file in the local
783 * workstation's AFS cache. The changes are not written to the server
784 * until a call to fsync or close is made. Users with AFS home
785 * directories have lost mail when over quota because these checks were
786 * not made in previous versions of movemail. */
787
788 #ifdef BSD_SYSTEM
789 if (fsync (mbfi) < 0)
790 {
791 error ("Error in fsync: %s", strerror (errno), 0);
792 return EXIT_FAILURE;
793 }
794 #endif
795
796 if (close (mbfi) == -1)
797 {
798 error ("Error in close: %s", strerror (errno), 0);
799 return EXIT_FAILURE;
800 }
801
802 if (! preserve)
803 for (i = 1; i <= nmsgs; i++)
804 {
805 if (pop_delete (server, i))
806 {
807 error ("Error from POP server: %s", pop_error, 0);
808 pop_close (server);
809 return EXIT_FAILURE;
810 }
811 }
812
813 if (pop_quit (server))
814 {
815 error ("Error from POP server: %s", pop_error, 0);
816 return EXIT_FAILURE;
817 }
818
819 return EXIT_SUCCESS;
820 }
821
822 static int
823 pop_retr (popserver server, int msgno, FILE *arg)
824 {
825 char *line;
826 int ret;
827
828 if (pop_retrieve_first (server, msgno, &line))
829 {
830 char *error = concat ("Error from POP server: ", pop_error, "");
831 strncpy (Errmsg, error, sizeof (Errmsg));
832 Errmsg[sizeof (Errmsg)-1] = '\0';
833 free(error);
834 return (NOTOK);
835 }
836
837 while ((ret = pop_retrieve_next (server, &line)) >= 0)
838 {
839 if (! line)
840 break;
841
842 if (mbx_write (line, ret, arg) != OK)
843 {
844 strcpy (Errmsg, strerror (errno));
845 pop_close (server);
846 return (NOTOK);
847 }
848 }
849
850 if (ret)
851 {
852 char *error = concat ("Error from POP server: ", pop_error, "");
853 strncpy (Errmsg, error, sizeof (Errmsg));
854 Errmsg[sizeof (Errmsg)-1] = '\0';
855 free(error);
856 return (NOTOK);
857 }
858
859 return (OK);
860 }
861
862 /* Do this as a macro instead of using strcmp to save on execution time. */
863 #define IS_FROM_LINE(a) ((a[0] == 'F') \
864 && (a[1] == 'r') \
865 && (a[2] == 'o') \
866 && (a[3] == 'm') \
867 && (a[4] == ' '))
868
869 static int
870 mbx_write (char *line, int len, FILE *mbf)
871 {
872 #ifdef MOVEMAIL_QUOTE_POP_FROM_LINES
873 if (IS_FROM_LINE (line))
874 {
875 if (fputc ('>', mbf) == EOF)
876 return (NOTOK);
877 }
878 #endif
879 if (line[0] == '\037')
880 {
881 if (fputs ("^_", mbf) == EOF)
882 return (NOTOK);
883 line++;
884 len--;
885 }
886 if (fwrite (line, 1, len, mbf) != len)
887 return (NOTOK);
888 if (fputc (0x0a, mbf) == EOF)
889 return (NOTOK);
890 return (OK);
891 }
892
893 static int
894 mbx_delimit_begin (FILE *mbf)
895 {
896 time_t now;
897 struct tm *ltime;
898 char fromline[40] = "From movemail ";
899
900 now = time (NULL);
901 ltime = localtime (&now);
902
903 strcat (fromline, asctime (ltime));
904
905 if (fputs (fromline, mbf) == EOF)
906 return (NOTOK);
907 return (OK);
908 }
909
910 static int
911 mbx_delimit_end (FILE *mbf)
912 {
913 if (putc ('\n', mbf) == EOF)
914 return (NOTOK);
915 return (OK);
916 }
917
918 #endif /* MAIL_USE_POP */
919 \f
920 #ifndef HAVE_STRERROR
921 char *
922 strerror (errnum)
923 int errnum;
924 {
925 extern char *sys_errlist[];
926 extern int sys_nerr;
927
928 if (errnum >= 0 && errnum < sys_nerr)
929 return sys_errlist[errnum];
930 return (char *) "Unknown error";
931 }
932
933 #endif /* ! HAVE_STRERROR */
934
935
936 /* movemail.c ends here */