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)
85 #define sys_wait(var) (*(var) = 0)
86 /* Unfortunately, Samba doesn't seem to properly lock Unix files even
87 though the locking call succeeds (and indeed blocks local access from
88 other NT programs). If you have direct file access using an NFS
89 client or something other than Samba, the locking call might work
90 properly - make sure it does before you enable this! */
91 #define DISABLE_DIRECT_ACCESS
92 #endif /* WINDOWSNT */
109 #if defined (XENIX) || defined (WINDOWSNT)
110 #include <sys/locking.h>
113 #ifdef MAIL_USE_LOCKF
114 #define MAIL_USE_SYSTEM_LOCK
117 #ifdef MAIL_USE_FLOCK
118 #define MAIL_USE_SYSTEM_LOCK
122 extern int lk_open (), lk_close ();
125 #if !defined (MAIL_USE_SYSTEM_LOCK) && !defined (MAIL_USE_MMDF) && \
126 defined (HAVE_LIBMAIL) && defined (HAVE_MAILLOCK_H)
127 #include <maillock.h>
128 /* We can't use maillock unless we know what directory system mail
131 #define MAIL_USE_MAILLOCK
132 static char *mail_spool_name ();
136 /* Cancel substitutions made by config.h for Emacs. */
149 void pfatal_with_name ();
150 void pfatal_and_delete ();
156 int mbx_delimit_begin ();
157 int mbx_delimit_end ();
159 /* Nonzero means this is name of a lock file to delete on fatal error. */
160 char *delete_lockname
;
167 char *inname
, *outname
;
171 int c
, preserve_mail
= 0;
173 #ifndef MAIL_USE_SYSTEM_LOCK
180 #endif /* not MAIL_USE_SYSTEM_LOCK */
182 #ifdef MAIL_USE_MAILLOCK
188 while ((c
= getopt (argc
, argv
, "p")) != EOF
)
201 (argc
- optind
< 2) || (argc
- optind
> 3)
207 fprintf (stderr
, "Usage: movemail [-p] inbox destfile%s\n",
217 inname
= argv
[optind
];
218 outname
= argv
[optind
+1];
225 fatal ("Destination file name is empty", 0);
227 /* Check access to output file. */
228 if (access (outname
, F_OK
) == 0 && access (outname
, W_OK
) != 0)
229 pfatal_with_name (outname
);
231 /* Also check that outname's directory is writable to the real uid. */
233 char *buf
= (char *) xmalloc (strlen (outname
) + 1);
235 strcpy (buf
, outname
);
236 p
= buf
+ strlen (buf
);
237 while (p
> buf
&& !IS_DIRECTORY_SEP (p
[-1]))
241 if (access (buf
, W_OK
) != 0)
242 pfatal_with_name (buf
);
247 if (!strncmp (inname
, "po:", 3))
251 status
= popmail (inname
+ 3, outname
, preserve_mail
,
252 (argc
- optind
== 3) ? argv
[optind
+2] : NULL
);
257 #endif /* MAIL_USE_POP */
259 #ifndef DISABLE_DIRECT_ACCESS
261 /* Check access to input file. */
262 if (access (inname
, R_OK
| W_OK
) != 0)
263 pfatal_with_name (inname
);
265 #ifndef MAIL_USE_MMDF
266 #ifndef MAIL_USE_SYSTEM_LOCK
267 #ifdef MAIL_USE_MAILLOCK
268 spool_name
= mail_spool_name (inname
);
272 /* Use a lock file named after our first argument with .lock appended:
273 If it exists, the mail file is locked. */
274 /* Note: this locking mechanism is *required* by the mailer
275 (on systems which use it) to prevent loss of mail.
277 On systems that use a lock file, extracting the mail without locking
278 WILL occasionally cause loss of mail due to timing errors!
280 So, if creation of the lock file fails
281 due to access permission on the mail spool directory,
282 you simply MUST change the permission
283 and/or make movemail a setgid program
284 so it can create lock files properly.
286 You might also wish to verify that your system is one
287 which uses lock files for this purpose. Some systems use other methods.
289 If your system uses the `flock' system call for mail locking,
290 define MAIL_USE_SYSTEM_LOCK in config.h or the s-*.h file
291 and recompile movemail. If the s- file for your system
292 should define MAIL_USE_SYSTEM_LOCK but does not, send a bug report
293 to bug-gnu-emacs@prep.ai.mit.edu so we can fix it. */
295 lockname
= concat (inname
, ".lock", "");
296 tempname
= (char *) xmalloc (strlen (inname
) + strlen ("EXXXXXX") + 1);
297 strcpy (tempname
, inname
);
298 p
= tempname
+ strlen (tempname
);
299 while (p
!= tempname
&& !IS_DIRECTORY_SEP (p
[-1]))
302 strcpy (p
, "EXXXXXX");
308 /* Create the lock file, but not under the lock file name. */
309 /* Give up if cannot do that. */
310 desc
= open (tempname
, O_WRONLY
| O_CREAT
| O_EXCL
, 0666);
313 char *message
= (char *) xmalloc (strlen (tempname
) + 50);
314 sprintf (message
, "%s--see source file lib-src/movemail.c",
316 pfatal_with_name (message
);
320 tem
= link (tempname
, lockname
);
326 /* If lock file is five minutes old, unlock it.
327 Five minutes should be good enough to cope with crashes
328 and wedgitude, and long enough to avoid being fooled
329 by time differences between machines. */
330 if (stat (lockname
, &st
) >= 0)
333 if (st
.st_ctime
< now
- 300)
338 delete_lockname
= lockname
;
340 #endif /* not MAIL_USE_SYSTEM_LOCK */
341 #endif /* not MAIL_USE_MMDF */
347 #if defined (MAIL_USE_MAILLOCK) && defined (HAVE_TOUCHLOCK)
348 long touched_lock
, now
;
353 #ifndef MAIL_USE_MMDF
354 #ifdef MAIL_USE_SYSTEM_LOCK
355 indesc
= open (inname
, O_RDWR
);
356 #else /* if not MAIL_USE_SYSTEM_LOCK */
357 indesc
= open (inname
, O_RDONLY
);
358 #endif /* not MAIL_USE_SYSTEM_LOCK */
359 #else /* MAIL_USE_MMDF */
360 indesc
= lk_open (inname
, O_RDONLY
, 0, 0, 10);
361 #endif /* MAIL_USE_MMDF */
364 pfatal_with_name (inname
);
366 #if defined (BSD_SYSTEM) || defined (XENIX)
367 /* In case movemail is setuid to root, make sure the user can
368 read the output file. */
369 /* This is desirable for all systems
370 but I don't want to assume all have the umask system call */
371 umask (umask (0) & 0333);
372 #endif /* BSD_SYSTEM || XENIX */
373 outdesc
= open (outname
, O_WRONLY
| O_CREAT
| O_EXCL
, 0666);
375 pfatal_with_name (outname
);
377 /* This label exists so we can retry locking
378 after a delay, if it got EAGAIN or EBUSY. */
381 /* Try to lock it. */
382 #ifdef MAIL_USE_MAILLOCK
385 /* The "0 - " is to make it a negative number if maillock returns
387 status
= 0 - maillock (spool_name
, 1);
388 #ifdef HAVE_TOUCHLOCK
389 touched_lock
= time (0);
394 #endif /* MAIL_USE_MAILLOCK */
396 #ifdef MAIL_USE_SYSTEM_LOCK
397 #ifdef MAIL_USE_LOCKF
398 status
= lockf (indesc
, F_LOCK
, 0);
399 #else /* not MAIL_USE_LOCKF */
401 status
= locking (indesc
, LK_RLCK
, 0L);
404 status
= locking (indesc
, LK_RLCK
, -1L);
406 status
= flock (indesc
, LOCK_EX
);
409 #endif /* not MAIL_USE_LOCKF */
410 #endif /* MAIL_USE_SYSTEM_LOCK */
413 /* If it fails, retry up to 5 times
414 for certain failure codes. */
417 if (++lockcount
<= 5)
435 pfatal_with_name (inname
);
443 nread
= read (indesc
, buf
, sizeof buf
);
444 if (nread
!= write (outdesc
, buf
, nread
))
446 int saved_errno
= errno
;
449 pfatal_with_name (outname
);
451 if (nread
< sizeof buf
)
453 #if defined (MAIL_USE_MAILLOCK) && defined (HAVE_TOUCHLOCK)
457 if (now
- touched_lock
> 60)
463 #endif /* MAIL_USE_MAILLOCK */
468 if (fsync (outdesc
) < 0)
469 pfatal_and_delete (outname
);
472 /* Check to make sure no errors before we zap the inbox. */
473 if (close (outdesc
) != 0)
474 pfatal_and_delete (outname
);
476 #ifdef MAIL_USE_SYSTEM_LOCK
479 #if defined (STRIDE) || defined (XENIX) || defined (WINDOWSNT)
480 /* Stride, xenix have file locking, but no ftruncate.
481 This mess will do. */
482 close (open (inname
, O_CREAT
| O_TRUNC
| O_RDWR
, 0666));
484 ftruncate (indesc
, 0L);
486 #endif /* STRIDE or XENIX */
487 #endif /* MAIL_USE_SYSTEM_LOCK */
490 lk_close (indesc
, 0, 0, 0);
495 #ifndef MAIL_USE_SYSTEM_LOCK
498 /* Delete the input file; if we can't, at least get rid of its
500 #ifdef MAIL_UNLINK_SPOOL
501 /* This is generally bad to do, because it destroys the permissions
502 that were set on the file. Better to just empty the file. */
503 if (unlink (inname
) < 0 && errno
!= ENOENT
)
504 #endif /* MAIL_UNLINK_SPOOL */
505 creat (inname
, 0600);
507 #endif /* not MAIL_USE_SYSTEM_LOCK */
509 #ifdef MAIL_USE_MAILLOCK
510 /* This has to occur in the child, i.e., in the process that
511 acquired the lock! */
519 if (!WIFEXITED (status
))
521 else if (WRETCODE (status
) != 0)
522 exit (WRETCODE (status
));
524 #if !defined (MAIL_USE_MMDF) && !defined (MAIL_USE_SYSTEM_LOCK)
525 #ifdef MAIL_USE_MAILLOCK
527 #endif /* MAIL_USE_MAILLOCK */
529 #endif /* not MAIL_USE_MMDF and not MAIL_USE_SYSTEM_LOCK */
531 #endif /* ! DISABLE_DIRECT_ACCESS */
536 #ifdef MAIL_USE_MAILLOCK
537 /* This function uses stat to confirm that the mail directory is
538 identical to the directory of the input file, rather than just
539 string-comparing the two paths, because one or both of them might
540 be symbolic links pointing to some other directory. */
542 mail_spool_name (inname
)
545 struct stat stat1
, stat2
;
549 if (! (fname
= rindex (inname
, '/')))
554 if (stat (MAILDIR
, &stat1
) < 0)
557 indir
= (char *) xmalloc (fname
- inname
+ 1);
558 strncpy (indir
, inname
, fname
- inname
);
559 indir
[fname
-inname
] = '\0';
562 status
= stat (indir
, &stat2
);
569 if ((stat1
.st_dev
== stat2
.st_dev
) &&
570 (stat1
.st_ino
== stat2
.st_ino
))
575 #endif /* MAIL_USE_MAILLOCK */
577 /* Print error message and exit. */
584 unlink (delete_lockname
);
589 /* Print error message. `s1' is printf control string, `s2' is arg for it. */
595 fprintf (stderr
, "movemail: ");
596 fprintf (stderr
, s1
, s2
, s3
);
597 fprintf (stderr
, "\n");
601 pfatal_with_name (name
)
604 char *s
= concat ("", strerror (errno
), " for %s");
609 pfatal_and_delete (name
)
612 char *s
= concat ("", strerror (errno
), " for %s");
617 /* Return a newly-allocated string whose contents concatenate those of s1, s2, s3. */
623 int len1
= strlen (s1
), len2
= strlen (s2
), len3
= strlen (s3
);
624 char *result
= (char *) xmalloc (len1
+ len2
+ len3
+ 1);
627 strcpy (result
+ len1
, s2
);
628 strcpy (result
+ len1
+ len2
, s3
);
629 *(result
+ len1
+ len2
+ len3
) = 0;
634 /* Like malloc but get fatal error if memory is exhausted. */
640 long *result
= (long *) malloc (size
);
642 fatal ("virtual memory exhausted", 0);
646 /* This is the guts of the interface to the Post Office Protocol. */
651 #include <sys/socket.h>
652 #include <netinet/in.h>
667 char ibuffer
[BUFSIZ
];
668 char obuffer
[BUFSIZ
];
671 popmail (user
, outfile
, preserve
, password
)
684 server
= pop_open (0, user
, password
, POP_NO_GETPASS
);
691 if (pop_stat (server
, &nmsgs
, &nbytes
))
703 mbfi
= open (outfile
, O_WRONLY
| O_CREAT
| O_EXCL
, 0666);
707 error ("Error in open: %s, %s", strerror (errno
), outfile
);
710 fchown (mbfi
, getuid (), -1);
712 if ((mbf
= fdopen (mbfi
, "wb")) == NULL
)
715 error ("Error in fdopen: %s", strerror (errno
));
721 for (i
= 1; i
<= nmsgs
; i
++)
723 mbx_delimit_begin (mbf
);
724 if (pop_retr (server
, i
, mbf
) != OK
)
730 mbx_delimit_end (mbf
);
734 error ("Error in fflush: %s", strerror (errno
));
741 /* On AFS, a call to write only modifies the file in the local
742 * workstation's AFS cache. The changes are not written to the server
743 * until a call to fsync or close is made. Users with AFS home
744 * directories have lost mail when over quota because these checks were
745 * not made in previous versions of movemail. */
748 if (fsync (mbfi
) < 0)
750 error ("Error in fsync: %s", strerror (errno
));
755 if (close (mbfi
) == -1)
757 error ("Error in close: %s", strerror (errno
));
762 for (i
= 1; i
<= nmsgs
; i
++)
764 if (pop_delete (server
, i
))
772 if (pop_quit (server
))
782 pop_retr (server
, msgno
, arg
)
786 extern char *strerror ();
790 if (pop_retrieve_first (server
, msgno
, &line
))
792 strncpy (Errmsg
, pop_error
, sizeof (Errmsg
));
793 Errmsg
[sizeof (Errmsg
)-1] = '\0';
797 while (! (ret
= pop_retrieve_next (server
, &line
)))
802 if (mbx_write (line
, arg
) != OK
)
804 strcpy (Errmsg
, strerror (errno
));
812 strncpy (Errmsg
, pop_error
, sizeof (Errmsg
));
813 Errmsg
[sizeof (Errmsg
)-1] = '\0';
820 /* Do this as a macro instead of using strcmp to save on execution time. */
821 #define IS_FROM_LINE(a) ((a[0] == 'F') \
828 mbx_write (line
, mbf
)
832 if (IS_FROM_LINE (line
))
834 if (fputc ('>', mbf
) == EOF
)
837 if (fputs (line
, mbf
) == EOF
)
839 if (fputc (0x0a, mbf
) == EOF
)
845 mbx_delimit_begin (mbf
)
848 if (fputs ("\f\n0, unseen,,\n", mbf
) == EOF
)
853 mbx_delimit_end (mbf
)
856 if (putc ('\037', mbf
) == EOF
)
861 #endif /* MAIL_USE_POP */
863 #ifndef HAVE_STRERROR
868 extern char *sys_errlist
[];
871 if (errnum
>= 0 && errnum
< sys_nerr
)
872 return sys_errlist
[errnum
];
873 return (char *) "Unknown error";
876 #endif /* ! HAVE_STRERROR */