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 Free Software Foundation, Inc.
5 This file is part of GNU Emacs.
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 2, or (at your option)
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.
17 You should have received a copy of the GNU General Public License
18 along with GNU Emacs; see the file COPYING. If not, write to
19 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
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.
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. */
35 * Modified January, 1986 by Michael R. Gretzinger (Project Athena)
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.
43 * New module: popmail.c
45 * main - added code within #ifdef MAIL_USE_POP; added setuid (getuid ())
47 * New routines in movemail.c:
48 * get_errmsg - return pointer to system error message
50 * Modified August, 1993 by Jonathan Kamens (OpenVision Technologies)
52 * Move all of the POP code into a separate file, "pop.c".
53 * Use strerror instead of get_errmsg.
57 #define NO_SHORTNAMES /* Tell config not to load remap.h */
58 #include <../src/config.h>
59 #include <sys/types.h>
64 #include <../src/syswait.h>
75 #define DIRECTORY_SEP '/'
77 #ifndef IS_DIRECTORY_SEP
78 #define IS_DIRECTORY_SEP(_c_) ((_c_) == DIRECTORY_SEP)
86 #define wait(var) (*(var) = 0)
87 /* Unfortunately, Samba doesn't seem to properly lock Unix files even
88 though the locking call succeeds (and indeed blocks local access from
89 other NT programs). If you have direct file access using an NFS
90 client or something other than Samba, the locking call might work
91 properly - make sure it does before you enable this!
93 [18-Feb-97 andrewi] I now believe my comment above to be incorrect,
94 since it was based on a misunderstanding of how locking calls are
95 implemented and used on Unix. */
96 //#define DISABLE_DIRECT_ACCESS
98 /* Ensure all file i/o is in binary mode. */
100 int _fmode
= _O_BINARY
;
101 #endif /* WINDOWSNT */
103 /* Cancel substitutions made by config.h for Emacs. */
128 #if defined (XENIX) || defined (WINDOWSNT)
129 #include <sys/locking.h>
132 #ifdef MAIL_USE_LOCKF
133 #define MAIL_USE_SYSTEM_LOCK
136 #ifdef MAIL_USE_FLOCK
137 #define MAIL_USE_SYSTEM_LOCK
141 extern int lk_open (), lk_close ();
144 #if !defined (MAIL_USE_SYSTEM_LOCK) && !defined (MAIL_USE_MMDF) && \
145 defined (HAVE_LIBMAIL) && defined (HAVE_MAILLOCK_H)
146 #include <maillock.h>
147 /* We can't use maillock unless we know what directory system mail
150 #define MAIL_USE_MAILLOCK
151 static char *mail_spool_name ();
159 extern char *rindex ();
163 void pfatal_with_name ();
164 void pfatal_and_delete ();
170 int mbx_delimit_begin ();
171 int mbx_delimit_end ();
173 /* Nonzero means this is name of a lock file to delete on fatal error. */
174 char *delete_lockname
;
181 char *inname
, *outname
;
185 int c
, preserve_mail
= 0;
187 #ifndef MAIL_USE_SYSTEM_LOCK
194 #endif /* not MAIL_USE_SYSTEM_LOCK */
196 #ifdef MAIL_USE_MAILLOCK
201 int pop_reverse_order
= 0;
203 #else /* ! MAIL_USE_POP */
205 #endif /* MAIL_USE_POP */
209 while ((c
= getopt (argc
, argv
, ARGSTR
)) != EOF
)
214 pop_reverse_order
= 1;
227 (argc
- optind
< 2) || (argc
- optind
> 3)
233 fprintf (stderr
, "Usage: movemail [-p] inbox destfile%s\n",
243 inname
= argv
[optind
];
244 outname
= argv
[optind
+1];
251 fatal ("Destination file name is empty", 0);
253 /* Check access to output file. */
254 if (access (outname
, F_OK
) == 0 && access (outname
, W_OK
) != 0)
255 pfatal_with_name (outname
);
257 /* Also check that outname's directory is writable to the real uid. */
259 char *buf
= (char *) xmalloc (strlen (outname
) + 1);
261 strcpy (buf
, outname
);
262 p
= buf
+ strlen (buf
);
263 while (p
> buf
&& !IS_DIRECTORY_SEP (p
[-1]))
267 if (access (buf
, W_OK
) != 0)
268 pfatal_with_name (buf
);
273 if (!strncmp (inname
, "po:", 3))
277 status
= popmail (inname
+ 3, outname
, preserve_mail
,
278 (argc
- optind
== 3) ? argv
[optind
+2] : NULL
,
284 #endif /* MAIL_USE_POP */
286 #ifndef DISABLE_DIRECT_ACCESS
288 /* Check access to input file. */
289 if (access (inname
, R_OK
| W_OK
) != 0)
290 pfatal_with_name (inname
);
292 #ifndef MAIL_USE_MMDF
293 #ifndef MAIL_USE_SYSTEM_LOCK
294 #ifdef MAIL_USE_MAILLOCK
295 spool_name
= mail_spool_name (inname
);
299 /* Use a lock file named after our first argument with .lock appended:
300 If it exists, the mail file is locked. */
301 /* Note: this locking mechanism is *required* by the mailer
302 (on systems which use it) to prevent loss of mail.
304 On systems that use a lock file, extracting the mail without locking
305 WILL occasionally cause loss of mail due to timing errors!
307 So, if creation of the lock file fails
308 due to access permission on the mail spool directory,
309 you simply MUST change the permission
310 and/or make movemail a setgid program
311 so it can create lock files properly.
313 You might also wish to verify that your system is one
314 which uses lock files for this purpose. Some systems use other methods.
316 If your system uses the `flock' system call for mail locking,
317 define MAIL_USE_SYSTEM_LOCK in config.h or the s-*.h file
318 and recompile movemail. If the s- file for your system
319 should define MAIL_USE_SYSTEM_LOCK but does not, send a bug report
320 to bug-gnu-emacs@prep.ai.mit.edu so we can fix it. */
322 lockname
= concat (inname
, ".lock", "");
323 tempname
= (char *) xmalloc (strlen (inname
) + strlen ("EXXXXXX") + 1);
324 strcpy (tempname
, inname
);
325 p
= tempname
+ strlen (tempname
);
326 while (p
!= tempname
&& !IS_DIRECTORY_SEP (p
[-1]))
329 strcpy (p
, "EXXXXXX");
335 /* Create the lock file, but not under the lock file name. */
336 /* Give up if cannot do that. */
337 desc
= open (tempname
, O_WRONLY
| O_CREAT
| O_EXCL
, 0666);
340 char *message
= (char *) xmalloc (strlen (tempname
) + 50);
341 sprintf (message
, "%s--see source file lib-src/movemail.c",
343 pfatal_with_name (message
);
347 tem
= link (tempname
, lockname
);
353 /* If lock file is five minutes old, unlock it.
354 Five minutes should be good enough to cope with crashes
355 and wedgitude, and long enough to avoid being fooled
356 by time differences between machines. */
357 if (stat (lockname
, &st
) >= 0)
360 if (st
.st_ctime
< now
- 300)
365 delete_lockname
= lockname
;
367 #endif /* not MAIL_USE_SYSTEM_LOCK */
368 #endif /* not MAIL_USE_MMDF */
374 #if defined (MAIL_USE_MAILLOCK) && defined (HAVE_TOUCHLOCK)
375 long touched_lock
, now
;
380 #ifndef MAIL_USE_MMDF
381 #ifdef MAIL_USE_SYSTEM_LOCK
382 indesc
= open (inname
, O_RDWR
);
383 #else /* if not MAIL_USE_SYSTEM_LOCK */
384 indesc
= open (inname
, O_RDONLY
);
385 #endif /* not MAIL_USE_SYSTEM_LOCK */
386 #else /* MAIL_USE_MMDF */
387 indesc
= lk_open (inname
, O_RDONLY
, 0, 0, 10);
388 #endif /* MAIL_USE_MMDF */
391 pfatal_with_name (inname
);
393 #if defined (BSD_SYSTEM) || defined (XENIX)
394 /* In case movemail is setuid to root, make sure the user can
395 read the output file. */
396 /* This is desirable for all systems
397 but I don't want to assume all have the umask system call */
398 umask (umask (0) & 0333);
399 #endif /* BSD_SYSTEM || XENIX */
400 outdesc
= open (outname
, O_WRONLY
| O_CREAT
| O_EXCL
, 0666);
402 pfatal_with_name (outname
);
404 /* This label exists so we can retry locking
405 after a delay, if it got EAGAIN or EBUSY. */
408 /* Try to lock it. */
409 #ifdef MAIL_USE_MAILLOCK
412 /* The "0 - " is to make it a negative number if maillock returns
414 status
= 0 - maillock (spool_name
, 1);
415 #ifdef HAVE_TOUCHLOCK
416 touched_lock
= time (0);
421 #endif /* MAIL_USE_MAILLOCK */
423 #ifdef MAIL_USE_SYSTEM_LOCK
424 #ifdef MAIL_USE_LOCKF
425 status
= lockf (indesc
, F_LOCK
, 0);
426 #else /* not MAIL_USE_LOCKF */
428 status
= locking (indesc
, LK_RLCK
, 0L);
431 status
= locking (indesc
, LK_RLCK
, -1L);
433 status
= flock (indesc
, LOCK_EX
);
436 #endif /* not MAIL_USE_LOCKF */
437 #endif /* MAIL_USE_SYSTEM_LOCK */
440 /* If it fails, retry up to 5 times
441 for certain failure codes. */
444 if (++lockcount
<= 5)
462 pfatal_with_name (inname
);
470 nread
= read (indesc
, buf
, sizeof buf
);
471 if (nread
!= write (outdesc
, buf
, nread
))
473 int saved_errno
= errno
;
476 pfatal_with_name (outname
);
478 if (nread
< sizeof buf
)
480 #if defined (MAIL_USE_MAILLOCK) && defined (HAVE_TOUCHLOCK)
484 if (now
- touched_lock
> 60)
490 #endif /* MAIL_USE_MAILLOCK */
495 if (fsync (outdesc
) < 0)
496 pfatal_and_delete (outname
);
499 /* Check to make sure no errors before we zap the inbox. */
500 if (close (outdesc
) != 0)
501 pfatal_and_delete (outname
);
503 #ifdef MAIL_USE_SYSTEM_LOCK
506 #if defined (STRIDE) || defined (XENIX)
507 /* Stride, xenix have file locking, but no ftruncate.
508 This mess will do. */
509 close (open (inname
, O_CREAT
| O_TRUNC
| O_RDWR
, 0666));
511 ftruncate (indesc
, 0L);
512 #endif /* STRIDE or XENIX */
514 #endif /* MAIL_USE_SYSTEM_LOCK */
517 lk_close (indesc
, 0, 0, 0);
522 #ifndef MAIL_USE_SYSTEM_LOCK
525 /* Delete the input file; if we can't, at least get rid of its
527 #ifdef MAIL_UNLINK_SPOOL
528 /* This is generally bad to do, because it destroys the permissions
529 that were set on the file. Better to just empty the file. */
530 if (unlink (inname
) < 0 && errno
!= ENOENT
)
531 #endif /* MAIL_UNLINK_SPOOL */
532 creat (inname
, 0600);
534 #endif /* not MAIL_USE_SYSTEM_LOCK */
536 #ifdef MAIL_USE_MAILLOCK
537 /* This has to occur in the child, i.e., in the process that
538 acquired the lock! */
546 if (!WIFEXITED (status
))
548 else if (WRETCODE (status
) != 0)
549 exit (WRETCODE (status
));
551 #if !defined (MAIL_USE_MMDF) && !defined (MAIL_USE_SYSTEM_LOCK)
552 #ifdef MAIL_USE_MAILLOCK
554 #endif /* MAIL_USE_MAILLOCK */
556 #endif /* not MAIL_USE_MMDF and not MAIL_USE_SYSTEM_LOCK */
558 #endif /* ! DISABLE_DIRECT_ACCESS */
563 #ifdef MAIL_USE_MAILLOCK
564 /* This function uses stat to confirm that the mail directory is
565 identical to the directory of the input file, rather than just
566 string-comparing the two paths, because one or both of them might
567 be symbolic links pointing to some other directory. */
569 mail_spool_name (inname
)
572 struct stat stat1
, stat2
;
576 if (! (fname
= rindex (inname
, '/')))
581 if (stat (MAILDIR
, &stat1
) < 0)
584 indir
= (char *) xmalloc (fname
- inname
+ 1);
585 strncpy (indir
, inname
, fname
- inname
);
586 indir
[fname
-inname
] = '\0';
589 status
= stat (indir
, &stat2
);
596 if (stat1
.st_dev
== stat2
.st_dev
597 && stat1
.st_ino
== stat2
.st_ino
)
602 #endif /* MAIL_USE_MAILLOCK */
604 /* Print error message and exit. */
611 unlink (delete_lockname
);
616 /* Print error message. `s1' is printf control string, `s2' is arg for it. */
622 fprintf (stderr
, "movemail: ");
623 fprintf (stderr
, s1
, s2
, s3
);
624 fprintf (stderr
, "\n");
628 pfatal_with_name (name
)
631 char *s
= concat ("", strerror (errno
), " for %s");
636 pfatal_and_delete (name
)
639 char *s
= concat ("", strerror (errno
), " for %s");
644 /* Return a newly-allocated string whose contents concatenate those of s1, s2, s3. */
650 int len1
= strlen (s1
), len2
= strlen (s2
), len3
= strlen (s3
);
651 char *result
= (char *) xmalloc (len1
+ len2
+ len3
+ 1);
654 strcpy (result
+ len1
, s2
);
655 strcpy (result
+ len1
+ len2
, s3
);
656 *(result
+ len1
+ len2
+ len3
) = 0;
661 /* Like malloc but get fatal error if memory is exhausted. */
667 long *result
= (long *) malloc (size
);
669 fatal ("virtual memory exhausted", 0);
673 /* This is the guts of the interface to the Post Office Protocol. */
678 #include <sys/socket.h>
679 #include <netinet/in.h>
694 char ibuffer
[BUFSIZ
];
695 char obuffer
[BUFSIZ
];
698 popmail (user
, outfile
, preserve
, password
, reverse_order
)
711 int start
, end
, increment
;
713 server
= pop_open (0, user
, password
, POP_NO_GETPASS
);
716 error ("Error connecting to POP server: %s", pop_error
);
720 if (pop_stat (server
, &nmsgs
, &nbytes
))
722 error ("Error getting message count from POP server: %s", pop_error
);
732 mbfi
= open (outfile
, O_WRONLY
| O_CREAT
| O_EXCL
, 0666);
736 error ("Error in open: %s, %s", strerror (errno
), outfile
);
739 fchown (mbfi
, getuid (), -1);
741 if ((mbf
= fdopen (mbfi
, "wb")) == NULL
)
744 error ("Error in fdopen: %s", strerror (errno
));
763 for (i
= start
; i
* increment
<= end
* increment
; i
+= increment
)
765 mbx_delimit_begin (mbf
);
766 if (pop_retr (server
, i
, mbf
) != OK
)
772 mbx_delimit_end (mbf
);
776 error ("Error in fflush: %s", strerror (errno
));
783 /* On AFS, a call to write only modifies the file in the local
784 * workstation's AFS cache. The changes are not written to the server
785 * until a call to fsync or close is made. Users with AFS home
786 * directories have lost mail when over quota because these checks were
787 * not made in previous versions of movemail. */
790 if (fsync (mbfi
) < 0)
792 error ("Error in fsync: %s", strerror (errno
));
797 if (close (mbfi
) == -1)
799 error ("Error in close: %s", strerror (errno
));
804 for (i
= 1; i
<= nmsgs
; i
++)
806 if (pop_delete (server
, i
))
808 error ("Error from POP server: %s", pop_error
);
814 if (pop_quit (server
))
816 error ("Error from POP server: %s", pop_error
);
824 pop_retr (server
, msgno
, arg
)
828 extern char *strerror ();
832 if (pop_retrieve_first (server
, msgno
, &line
))
834 char *error
= concat ("Error from POP server: ", pop_error
, "");
835 strncpy (Errmsg
, error
, sizeof (Errmsg
));
836 Errmsg
[sizeof (Errmsg
)-1] = '\0';
841 while ((ret
= pop_retrieve_next (server
, &line
)) >= 0)
846 if (mbx_write (line
, ret
, arg
) != OK
)
848 strcpy (Errmsg
, strerror (errno
));
856 char *error
= concat ("Error from POP server: ", pop_error
, "");
857 strncpy (Errmsg
, error
, sizeof (Errmsg
));
858 Errmsg
[sizeof (Errmsg
)-1] = '\0';
866 /* Do this as a macro instead of using strcmp to save on execution time. */
867 #define IS_FROM_LINE(a) ((a[0] == 'F') \
874 mbx_write (line
, len
, mbf
)
879 #ifdef MOVEMAIL_QUOTE_POP_FROM_LINES
880 if (IS_FROM_LINE (line
))
882 if (fputc ('>', mbf
) == EOF
)
886 if (line
[0] == '\037')
888 if (fputs ("^_", mbf
) == EOF
)
893 if (fwrite (line
, 1, len
, mbf
) != len
)
895 if (fputc (0x0a, mbf
) == EOF
)
901 mbx_delimit_begin (mbf
)
904 if (fputs ("\f\n0, unseen,,\n", mbf
) == EOF
)
909 mbx_delimit_end (mbf
)
912 if (putc ('\037', mbf
) == EOF
)
917 #endif /* MAIL_USE_POP */
919 #ifndef HAVE_STRERROR
924 extern char *sys_errlist
[];
927 if (errnum
>= 0 && errnum
< sys_nerr
)
928 return sys_errlist
[errnum
];
929 return (char *) "Unknown error";
932 #endif /* ! HAVE_STRERROR */