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