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
202 while ((c
= getopt (argc
, argv
, "p")) != EOF
)
215 (argc
- optind
< 2) || (argc
- optind
> 3)
221 fprintf (stderr
, "Usage: movemail [-p] inbox destfile%s\n",
231 inname
= argv
[optind
];
232 outname
= argv
[optind
+1];
239 fatal ("Destination file name is empty", 0);
241 /* Check access to output file. */
242 if (access (outname
, F_OK
) == 0 && access (outname
, W_OK
) != 0)
243 pfatal_with_name (outname
);
245 /* Also check that outname's directory is writable to the real uid. */
247 char *buf
= (char *) xmalloc (strlen (outname
) + 1);
249 strcpy (buf
, outname
);
250 p
= buf
+ strlen (buf
);
251 while (p
> buf
&& !IS_DIRECTORY_SEP (p
[-1]))
255 if (access (buf
, W_OK
) != 0)
256 pfatal_with_name (buf
);
261 if (!strncmp (inname
, "po:", 3))
265 status
= popmail (inname
+ 3, outname
, preserve_mail
,
266 (argc
- optind
== 3) ? argv
[optind
+2] : NULL
);
271 #endif /* MAIL_USE_POP */
273 #ifndef DISABLE_DIRECT_ACCESS
275 /* Check access to input file. */
276 if (access (inname
, R_OK
| W_OK
) != 0)
277 pfatal_with_name (inname
);
279 #ifndef MAIL_USE_MMDF
280 #ifndef MAIL_USE_SYSTEM_LOCK
281 #ifdef MAIL_USE_MAILLOCK
282 spool_name
= mail_spool_name (inname
);
286 /* Use a lock file named after our first argument with .lock appended:
287 If it exists, the mail file is locked. */
288 /* Note: this locking mechanism is *required* by the mailer
289 (on systems which use it) to prevent loss of mail.
291 On systems that use a lock file, extracting the mail without locking
292 WILL occasionally cause loss of mail due to timing errors!
294 So, if creation of the lock file fails
295 due to access permission on the mail spool directory,
296 you simply MUST change the permission
297 and/or make movemail a setgid program
298 so it can create lock files properly.
300 You might also wish to verify that your system is one
301 which uses lock files for this purpose. Some systems use other methods.
303 If your system uses the `flock' system call for mail locking,
304 define MAIL_USE_SYSTEM_LOCK in config.h or the s-*.h file
305 and recompile movemail. If the s- file for your system
306 should define MAIL_USE_SYSTEM_LOCK but does not, send a bug report
307 to bug-gnu-emacs@prep.ai.mit.edu so we can fix it. */
309 lockname
= concat (inname
, ".lock", "");
310 tempname
= (char *) xmalloc (strlen (inname
) + strlen ("EXXXXXX") + 1);
311 strcpy (tempname
, inname
);
312 p
= tempname
+ strlen (tempname
);
313 while (p
!= tempname
&& !IS_DIRECTORY_SEP (p
[-1]))
316 strcpy (p
, "EXXXXXX");
322 /* Create the lock file, but not under the lock file name. */
323 /* Give up if cannot do that. */
324 desc
= open (tempname
, O_WRONLY
| O_CREAT
| O_EXCL
, 0666);
327 char *message
= (char *) xmalloc (strlen (tempname
) + 50);
328 sprintf (message
, "%s--see source file lib-src/movemail.c",
330 pfatal_with_name (message
);
334 tem
= link (tempname
, lockname
);
340 /* If lock file is five minutes old, unlock it.
341 Five minutes should be good enough to cope with crashes
342 and wedgitude, and long enough to avoid being fooled
343 by time differences between machines. */
344 if (stat (lockname
, &st
) >= 0)
347 if (st
.st_ctime
< now
- 300)
352 delete_lockname
= lockname
;
354 #endif /* not MAIL_USE_SYSTEM_LOCK */
355 #endif /* not MAIL_USE_MMDF */
361 #if defined (MAIL_USE_MAILLOCK) && defined (HAVE_TOUCHLOCK)
362 long touched_lock
, now
;
367 #ifndef MAIL_USE_MMDF
368 #ifdef MAIL_USE_SYSTEM_LOCK
369 indesc
= open (inname
, O_RDWR
);
370 #else /* if not MAIL_USE_SYSTEM_LOCK */
371 indesc
= open (inname
, O_RDONLY
);
372 #endif /* not MAIL_USE_SYSTEM_LOCK */
373 #else /* MAIL_USE_MMDF */
374 indesc
= lk_open (inname
, O_RDONLY
, 0, 0, 10);
375 #endif /* MAIL_USE_MMDF */
378 pfatal_with_name (inname
);
380 #if defined (BSD_SYSTEM) || defined (XENIX)
381 /* In case movemail is setuid to root, make sure the user can
382 read the output file. */
383 /* This is desirable for all systems
384 but I don't want to assume all have the umask system call */
385 umask (umask (0) & 0333);
386 #endif /* BSD_SYSTEM || XENIX */
387 outdesc
= open (outname
, O_WRONLY
| O_CREAT
| O_EXCL
, 0666);
389 pfatal_with_name (outname
);
391 /* This label exists so we can retry locking
392 after a delay, if it got EAGAIN or EBUSY. */
395 /* Try to lock it. */
396 #ifdef MAIL_USE_MAILLOCK
399 /* The "0 - " is to make it a negative number if maillock returns
401 status
= 0 - maillock (spool_name
, 1);
402 #ifdef HAVE_TOUCHLOCK
403 touched_lock
= time (0);
408 #endif /* MAIL_USE_MAILLOCK */
410 #ifdef MAIL_USE_SYSTEM_LOCK
411 #ifdef MAIL_USE_LOCKF
412 status
= lockf (indesc
, F_LOCK
, 0);
413 #else /* not MAIL_USE_LOCKF */
415 status
= locking (indesc
, LK_RLCK
, 0L);
418 status
= locking (indesc
, LK_RLCK
, -1L);
420 status
= flock (indesc
, LOCK_EX
);
423 #endif /* not MAIL_USE_LOCKF */
424 #endif /* MAIL_USE_SYSTEM_LOCK */
427 /* If it fails, retry up to 5 times
428 for certain failure codes. */
431 if (++lockcount
<= 5)
449 pfatal_with_name (inname
);
457 nread
= read (indesc
, buf
, sizeof buf
);
458 if (nread
!= write (outdesc
, buf
, nread
))
460 int saved_errno
= errno
;
463 pfatal_with_name (outname
);
465 if (nread
< sizeof buf
)
467 #if defined (MAIL_USE_MAILLOCK) && defined (HAVE_TOUCHLOCK)
471 if (now
- touched_lock
> 60)
477 #endif /* MAIL_USE_MAILLOCK */
482 if (fsync (outdesc
) < 0)
483 pfatal_and_delete (outname
);
486 /* Check to make sure no errors before we zap the inbox. */
487 if (close (outdesc
) != 0)
488 pfatal_and_delete (outname
);
490 #ifdef MAIL_USE_SYSTEM_LOCK
493 #if defined (STRIDE) || defined (XENIX) || defined (WINDOWSNT)
494 /* Stride, xenix have file locking, but no ftruncate.
495 This mess will do. */
496 close (open (inname
, O_CREAT
| O_TRUNC
| O_RDWR
, 0666));
498 ftruncate (indesc
, 0L);
499 #endif /* STRIDE or XENIX */
501 #endif /* MAIL_USE_SYSTEM_LOCK */
504 lk_close (indesc
, 0, 0, 0);
509 #ifndef MAIL_USE_SYSTEM_LOCK
512 /* Delete the input file; if we can't, at least get rid of its
514 #ifdef MAIL_UNLINK_SPOOL
515 /* This is generally bad to do, because it destroys the permissions
516 that were set on the file. Better to just empty the file. */
517 if (unlink (inname
) < 0 && errno
!= ENOENT
)
518 #endif /* MAIL_UNLINK_SPOOL */
519 creat (inname
, 0600);
521 #endif /* not MAIL_USE_SYSTEM_LOCK */
523 #ifdef MAIL_USE_MAILLOCK
524 /* This has to occur in the child, i.e., in the process that
525 acquired the lock! */
533 if (!WIFEXITED (status
))
535 else if (WRETCODE (status
) != 0)
536 exit (WRETCODE (status
));
538 #if !defined (MAIL_USE_MMDF) && !defined (MAIL_USE_SYSTEM_LOCK)
539 #ifdef MAIL_USE_MAILLOCK
541 #endif /* MAIL_USE_MAILLOCK */
543 #endif /* not MAIL_USE_MMDF and not MAIL_USE_SYSTEM_LOCK */
545 #endif /* ! DISABLE_DIRECT_ACCESS */
550 #ifdef MAIL_USE_MAILLOCK
551 /* This function uses stat to confirm that the mail directory is
552 identical to the directory of the input file, rather than just
553 string-comparing the two paths, because one or both of them might
554 be symbolic links pointing to some other directory. */
556 mail_spool_name (inname
)
559 struct stat stat1
, stat2
;
563 if (! (fname
= rindex (inname
, '/')))
568 if (stat (MAILDIR
, &stat1
) < 0)
571 indir
= (char *) xmalloc (fname
- inname
+ 1);
572 strncpy (indir
, inname
, fname
- inname
);
573 indir
[fname
-inname
] = '\0';
576 status
= stat (indir
, &stat2
);
583 if (stat1
.st_dev
== stat2
.st_dev
584 && stat1
.st_ino
== stat2
.st_ino
)
589 #endif /* MAIL_USE_MAILLOCK */
591 /* Print error message and exit. */
598 unlink (delete_lockname
);
603 /* Print error message. `s1' is printf control string, `s2' is arg for it. */
609 fprintf (stderr
, "movemail: ");
610 fprintf (stderr
, s1
, s2
, s3
);
611 fprintf (stderr
, "\n");
615 pfatal_with_name (name
)
618 char *s
= concat ("", strerror (errno
), " for %s");
623 pfatal_and_delete (name
)
626 char *s
= concat ("", strerror (errno
), " for %s");
631 /* Return a newly-allocated string whose contents concatenate those of s1, s2, s3. */
637 int len1
= strlen (s1
), len2
= strlen (s2
), len3
= strlen (s3
);
638 char *result
= (char *) xmalloc (len1
+ len2
+ len3
+ 1);
641 strcpy (result
+ len1
, s2
);
642 strcpy (result
+ len1
+ len2
, s3
);
643 *(result
+ len1
+ len2
+ len3
) = 0;
648 /* Like malloc but get fatal error if memory is exhausted. */
654 long *result
= (long *) malloc (size
);
656 fatal ("virtual memory exhausted", 0);
660 /* This is the guts of the interface to the Post Office Protocol. */
665 #include <sys/socket.h>
666 #include <netinet/in.h>
681 char ibuffer
[BUFSIZ
];
682 char obuffer
[BUFSIZ
];
685 popmail (user
, outfile
, preserve
, password
)
698 server
= pop_open (0, user
, password
, POP_NO_GETPASS
);
705 if (pop_stat (server
, &nmsgs
, &nbytes
))
717 mbfi
= open (outfile
, O_WRONLY
| O_CREAT
| O_EXCL
, 0666);
721 error ("Error in open: %s, %s", strerror (errno
), outfile
);
724 fchown (mbfi
, getuid (), -1);
726 if ((mbf
= fdopen (mbfi
, "wb")) == NULL
)
729 error ("Error in fdopen: %s", strerror (errno
));
735 for (i
= 1; i
<= nmsgs
; i
++)
737 mbx_delimit_begin (mbf
);
738 if (pop_retr (server
, i
, mbf
) != OK
)
744 mbx_delimit_end (mbf
);
748 error ("Error in fflush: %s", strerror (errno
));
755 /* On AFS, a call to write only modifies the file in the local
756 * workstation's AFS cache. The changes are not written to the server
757 * until a call to fsync or close is made. Users with AFS home
758 * directories have lost mail when over quota because these checks were
759 * not made in previous versions of movemail. */
762 if (fsync (mbfi
) < 0)
764 error ("Error in fsync: %s", strerror (errno
));
769 if (close (mbfi
) == -1)
771 error ("Error in close: %s", strerror (errno
));
776 for (i
= 1; i
<= nmsgs
; i
++)
778 if (pop_delete (server
, i
))
786 if (pop_quit (server
))
796 pop_retr (server
, msgno
, arg
)
800 extern char *strerror ();
804 if (pop_retrieve_first (server
, msgno
, &line
))
806 strncpy (Errmsg
, pop_error
, sizeof (Errmsg
));
807 Errmsg
[sizeof (Errmsg
)-1] = '\0';
811 while ((ret
= pop_retrieve_next (server
, &line
)) >= 0)
816 if (mbx_write (line
, ret
, arg
) != OK
)
818 strcpy (Errmsg
, strerror (errno
));
826 strncpy (Errmsg
, pop_error
, sizeof (Errmsg
));
827 Errmsg
[sizeof (Errmsg
)-1] = '\0';
834 /* Do this as a macro instead of using strcmp to save on execution time. */
835 #define IS_FROM_LINE(a) ((a[0] == 'F') \
842 mbx_write (line
, len
, mbf
)
847 #ifdef MOVEMAIL_QUOTE_POP_FROM_LINES
848 if (IS_FROM_LINE (line
))
850 if (fputc ('>', mbf
) == EOF
)
854 if (line
[0] == '\037')
856 if (fputs ("^_", mbf
) == EOF
)
861 if (fwrite (line
, 1, len
, mbf
) != len
)
863 if (fputc (0x0a, mbf
) == EOF
)
869 mbx_delimit_begin (mbf
)
872 if (fputs ("\f\n0, unseen,,\n", mbf
) == EOF
)
877 mbx_delimit_end (mbf
)
880 if (putc ('\037', mbf
) == EOF
)
885 #endif /* MAIL_USE_POP */
887 #ifndef HAVE_STRERROR
892 extern char *sys_errlist
[];
895 if (errnum
>= 0 && errnum
< sys_nerr
)
896 return sys_errlist
[errnum
];
897 return (char *) "Unknown error";
900 #endif /* ! HAVE_STRERROR */