]>
code.delx.au - gnu-emacs/blob - 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, 92, 93, 94, 96, 1999 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 */
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
99 #endif /* WINDOWSNT */
116 #if defined (XENIX) || defined (WINDOWSNT)
117 #include <sys/locking.h>
120 #ifdef MAIL_USE_LOCKF
121 #define MAIL_USE_SYSTEM_LOCK
124 #ifdef MAIL_USE_FLOCK
125 #define MAIL_USE_SYSTEM_LOCK
129 extern int lk_open (), lk_close ();
132 #if !defined (MAIL_USE_SYSTEM_LOCK) && !defined (MAIL_USE_MMDF) && \
133 defined (HAVE_LIBMAIL) && defined (HAVE_MAILLOCK_H)
134 #include <maillock.h>
135 /* We can't use maillock unless we know what directory system mail
138 #define MAIL_USE_MAILLOCK
139 static char *mail_spool_name ();
147 extern char *rindex ();
151 void pfatal_with_name ();
152 void pfatal_and_delete ();
158 int mbx_delimit_begin ();
159 int mbx_delimit_end ();
161 /* Nonzero means this is name of a lock file to delete on fatal error. */
162 char *delete_lockname
;
169 char *inname
, *outname
;
173 int c
, preserve_mail
= 0;
175 #ifndef MAIL_USE_SYSTEM_LOCK
182 #endif /* not MAIL_USE_SYSTEM_LOCK */
184 #ifdef MAIL_USE_MAILLOCK
189 int pop_reverse_order
= 0;
191 #else /* ! MAIL_USE_POP */
193 #endif /* MAIL_USE_POP */
196 /* Ensure all file i/o is in binary mode. */
202 while ((c
= getopt (argc
, argv
, ARGSTR
)) != EOF
)
207 pop_reverse_order
= 1;
220 (argc
- optind
< 2) || (argc
- optind
> 3)
226 fprintf (stderr
, "Usage: movemail [-p] inbox destfile%s\n",
236 inname
= argv
[optind
];
237 outname
= argv
[optind
+1];
244 fatal ("Destination file name is empty", 0);
246 /* Check access to output file. */
247 if (access (outname
, F_OK
) == 0 && access (outname
, W_OK
) != 0)
248 pfatal_with_name (outname
);
250 /* Also check that outname's directory is writable to the real uid. */
252 char *buf
= (char *) xmalloc (strlen (outname
) + 1);
254 strcpy (buf
, outname
);
255 p
= buf
+ strlen (buf
);
256 while (p
> buf
&& !IS_DIRECTORY_SEP (p
[-1]))
260 if (access (buf
, W_OK
) != 0)
261 pfatal_with_name (buf
);
266 if (!strncmp (inname
, "po:", 3))
270 status
= popmail (inname
+ 3, outname
, preserve_mail
,
271 (argc
- optind
== 3) ? argv
[optind
+2] : NULL
,
277 #endif /* MAIL_USE_POP */
279 #ifndef DISABLE_DIRECT_ACCESS
281 /* Check access to input file. */
282 if (access (inname
, R_OK
| W_OK
) != 0)
283 pfatal_with_name (inname
);
285 #ifndef MAIL_USE_MMDF
286 #ifndef MAIL_USE_SYSTEM_LOCK
287 #ifdef MAIL_USE_MAILLOCK
288 spool_name
= mail_spool_name (inname
);
292 /* Use a lock file named after our first argument with .lock appended:
293 If it exists, the mail file is locked. */
294 /* Note: this locking mechanism is *required* by the mailer
295 (on systems which use it) to prevent loss of mail.
297 On systems that use a lock file, extracting the mail without locking
298 WILL occasionally cause loss of mail due to timing errors!
300 So, if creation of the lock file fails
301 due to access permission on the mail spool directory,
302 you simply MUST change the permission
303 and/or make movemail a setgid program
304 so it can create lock files properly.
306 You might also wish to verify that your system is one
307 which uses lock files for this purpose. Some systems use other methods.
309 If your system uses the `flock' system call for mail locking,
310 define MAIL_USE_SYSTEM_LOCK in config.h or the s-*.h file
311 and recompile movemail. If the s- file for your system
312 should define MAIL_USE_SYSTEM_LOCK but does not, send a bug report
313 to bug-gnu-emacs@prep.ai.mit.edu so we can fix it. */
315 lockname
= concat (inname
, ".lock", "");
316 tempname
= (char *) xmalloc (strlen (inname
) + strlen ("EXXXXXX") + 1);
317 strcpy (tempname
, inname
);
318 p
= tempname
+ strlen (tempname
);
319 while (p
!= tempname
&& !IS_DIRECTORY_SEP (p
[-1]))
322 strcpy (p
, "EXXXXXX");
328 /* Create the lock file, but not under the lock file name. */
329 /* Give up if cannot do that. */
330 desc
= open (tempname
, O_WRONLY
| O_CREAT
| O_EXCL
, 0666);
333 char *message
= (char *) xmalloc (strlen (tempname
) + 50);
334 sprintf (message
, "creating %s, which would become the lock file",
336 pfatal_with_name (message
);
340 tem
= link (tempname
, lockname
);
346 /* If lock file is five minutes old, unlock it.
347 Five minutes should be good enough to cope with crashes
348 and wedgitude, and long enough to avoid being fooled
349 by time differences between machines. */
350 if (stat (lockname
, &st
) >= 0)
353 if (st
.st_ctime
< now
- 300)
358 delete_lockname
= lockname
;
360 #endif /* not MAIL_USE_SYSTEM_LOCK */
361 #endif /* not MAIL_USE_MMDF */
367 #if defined (MAIL_USE_MAILLOCK) && defined (HAVE_TOUCHLOCK)
368 long touched_lock
, now
;
373 #ifndef MAIL_USE_MMDF
374 #ifdef MAIL_USE_SYSTEM_LOCK
375 indesc
= open (inname
, O_RDWR
);
376 #else /* if not MAIL_USE_SYSTEM_LOCK */
377 indesc
= open (inname
, O_RDONLY
);
378 #endif /* not MAIL_USE_SYSTEM_LOCK */
379 #else /* MAIL_USE_MMDF */
380 indesc
= lk_open (inname
, O_RDONLY
, 0, 0, 10);
381 #endif /* MAIL_USE_MMDF */
384 pfatal_with_name (inname
);
386 #if defined (BSD_SYSTEM) || defined (XENIX)
387 /* In case movemail is setuid to root, make sure the user can
388 read the output file. */
389 /* This is desirable for all systems
390 but I don't want to assume all have the umask system call */
391 umask (umask (0) & 0333);
392 #endif /* BSD_SYSTEM || XENIX */
393 outdesc
= open (outname
, O_WRONLY
| O_CREAT
| O_EXCL
, 0666);
395 pfatal_with_name (outname
);
397 /* This label exists so we can retry locking
398 after a delay, if it got EAGAIN or EBUSY. */
401 /* Try to lock it. */
402 #ifdef MAIL_USE_MAILLOCK
405 /* The "0 - " is to make it a negative number if maillock returns
407 status
= 0 - maillock (spool_name
, 1);
408 #ifdef HAVE_TOUCHLOCK
409 touched_lock
= time (0);
414 #endif /* MAIL_USE_MAILLOCK */
416 #ifdef MAIL_USE_SYSTEM_LOCK
417 #ifdef MAIL_USE_LOCKF
418 status
= lockf (indesc
, F_LOCK
, 0);
419 #else /* not MAIL_USE_LOCKF */
421 status
= locking (indesc
, LK_RLCK
, 0L);
424 status
= locking (indesc
, LK_RLCK
, -1L);
426 status
= flock (indesc
, LOCK_EX
);
429 #endif /* not MAIL_USE_LOCKF */
430 #endif /* MAIL_USE_SYSTEM_LOCK */
433 /* If it fails, retry up to 5 times
434 for certain failure codes. */
437 if (++lockcount
<= 5)
455 pfatal_with_name (inname
);
463 nread
= read (indesc
, buf
, sizeof buf
);
464 if (nread
!= write (outdesc
, buf
, nread
))
466 int saved_errno
= errno
;
469 pfatal_with_name (outname
);
471 if (nread
< sizeof buf
)
473 #if defined (MAIL_USE_MAILLOCK) && defined (HAVE_TOUCHLOCK)
477 if (now
- touched_lock
> 60)
483 #endif /* MAIL_USE_MAILLOCK */
488 if (fsync (outdesc
) < 0)
489 pfatal_and_delete (outname
);
492 /* Check to make sure no errors before we zap the inbox. */
493 if (close (outdesc
) != 0)
494 pfatal_and_delete (outname
);
496 #ifdef MAIL_USE_SYSTEM_LOCK
499 #if defined (STRIDE) || defined (XENIX)
500 /* Stride, xenix have file locking, but no ftruncate.
501 This mess will do. */
502 close (open (inname
, O_CREAT
| O_TRUNC
| O_RDWR
, 0666));
504 ftruncate (indesc
, 0L);
505 #endif /* STRIDE or XENIX */
507 #endif /* MAIL_USE_SYSTEM_LOCK */
510 lk_close (indesc
, 0, 0, 0);
515 #ifndef MAIL_USE_SYSTEM_LOCK
518 /* Delete the input file; if we can't, at least get rid of its
520 #ifdef MAIL_UNLINK_SPOOL
521 /* This is generally bad to do, because it destroys the permissions
522 that were set on the file. Better to just empty the file. */
523 if (unlink (inname
) < 0 && errno
!= ENOENT
)
524 #endif /* MAIL_UNLINK_SPOOL */
525 creat (inname
, 0600);
527 #endif /* not MAIL_USE_SYSTEM_LOCK */
529 #ifdef MAIL_USE_MAILLOCK
530 /* This has to occur in the child, i.e., in the process that
531 acquired the lock! */
539 if (!WIFEXITED (status
))
541 else if (WRETCODE (status
) != 0)
542 exit (WRETCODE (status
));
544 #if !defined (MAIL_USE_MMDF) && !defined (MAIL_USE_SYSTEM_LOCK)
545 #ifdef MAIL_USE_MAILLOCK
547 #endif /* MAIL_USE_MAILLOCK */
549 #endif /* not MAIL_USE_MMDF and not MAIL_USE_SYSTEM_LOCK */
551 #endif /* ! DISABLE_DIRECT_ACCESS */
556 #ifdef MAIL_USE_MAILLOCK
557 /* This function uses stat to confirm that the mail directory is
558 identical to the directory of the input file, rather than just
559 string-comparing the two paths, because one or both of them might
560 be symbolic links pointing to some other directory. */
562 mail_spool_name (inname
)
565 struct stat stat1
, stat2
;
569 if (! (fname
= rindex (inname
, '/')))
574 if (stat (MAILDIR
, &stat1
) < 0)
577 indir
= (char *) xmalloc (fname
- inname
+ 1);
578 strncpy (indir
, inname
, fname
- inname
);
579 indir
[fname
-inname
] = '\0';
582 status
= stat (indir
, &stat2
);
589 if (stat1
.st_dev
== stat2
.st_dev
590 && stat1
.st_ino
== stat2
.st_ino
)
595 #endif /* MAIL_USE_MAILLOCK */
597 /* Print error message and exit. */
604 unlink (delete_lockname
);
609 /* Print error message. `s1' is printf control string, `s2' is arg for it. */
615 fprintf (stderr
, "movemail: ");
616 fprintf (stderr
, s1
, s2
, s3
);
617 fprintf (stderr
, "\n");
621 pfatal_with_name (name
)
624 char *s
= concat ("", strerror (errno
), " for %s");
629 pfatal_and_delete (name
)
632 char *s
= concat ("", strerror (errno
), " for %s");
637 /* Return a newly-allocated string whose contents concatenate those of s1, s2, s3. */
643 int len1
= strlen (s1
), len2
= strlen (s2
), len3
= strlen (s3
);
644 char *result
= (char *) xmalloc (len1
+ len2
+ len3
+ 1);
647 strcpy (result
+ len1
, s2
);
648 strcpy (result
+ len1
+ len2
, s3
);
649 *(result
+ len1
+ len2
+ len3
) = 0;
654 /* Like malloc but get fatal error if memory is exhausted. */
660 long *result
= (long *) malloc (size
);
662 fatal ("virtual memory exhausted", 0);
666 /* This is the guts of the interface to the Post Office Protocol. */
671 #include <sys/socket.h>
672 #include <netinet/in.h>
687 char ibuffer
[BUFSIZ
];
688 char obuffer
[BUFSIZ
];
689 char Errmsg
[200]; /* POP errors, at least, can exceed
690 the original length of 80. */
693 * The full legal syntax for a POP mailbox specification for movemail
694 * is "po:username:hostname". The ":hostname" is optional; if it is
695 * omitted, the MAILHOST environment variable will be consulted. Note
696 * that by the time popmail() is called the "po:" has been stripped
697 * off of the front of the mailbox name.
699 * If the mailbox is in the form "po:username:hostname", then it is
700 * modified by this function -- the second colon is replaced by a
704 popmail (mailbox
, outfile
, preserve
, password
, reverse_order
)
717 int start
, end
, increment
;
718 char *user
, *hostname
;
721 if ((hostname
= index(mailbox
, ':')))
724 server
= pop_open (hostname
, user
, password
, POP_NO_GETPASS
);
727 error ("Error connecting to POP server: %s", pop_error
);
731 if (pop_stat (server
, &nmsgs
, &nbytes
))
733 error ("Error getting message count from POP server: %s", pop_error
);
743 mbfi
= open (outfile
, O_WRONLY
| O_CREAT
| O_EXCL
, 0666);
747 error ("Error in open: %s, %s", strerror (errno
), outfile
);
750 fchown (mbfi
, getuid (), -1);
752 if ((mbf
= fdopen (mbfi
, "wb")) == NULL
)
755 error ("Error in fdopen: %s", strerror (errno
));
774 for (i
= start
; i
* increment
<= end
* increment
; i
+= increment
)
776 mbx_delimit_begin (mbf
);
777 if (pop_retr (server
, i
, mbf
) != OK
)
783 mbx_delimit_end (mbf
);
787 error ("Error in fflush: %s", strerror (errno
));
794 /* On AFS, a call to write only modifies the file in the local
795 * workstation's AFS cache. The changes are not written to the server
796 * until a call to fsync or close is made. Users with AFS home
797 * directories have lost mail when over quota because these checks were
798 * not made in previous versions of movemail. */
801 if (fsync (mbfi
) < 0)
803 error ("Error in fsync: %s", strerror (errno
));
808 if (close (mbfi
) == -1)
810 error ("Error in close: %s", strerror (errno
));
815 for (i
= 1; i
<= nmsgs
; i
++)
817 if (pop_delete (server
, i
))
819 error ("Error from POP server: %s", pop_error
);
825 if (pop_quit (server
))
827 error ("Error from POP server: %s", pop_error
);
835 pop_retr (server
, msgno
, arg
)
839 extern char *strerror ();
843 if (pop_retrieve_first (server
, msgno
, &line
))
845 char *error
= concat ("Error from POP server: ", pop_error
, "");
846 strncpy (Errmsg
, error
, sizeof (Errmsg
));
847 Errmsg
[sizeof (Errmsg
)-1] = '\0';
852 while ((ret
= pop_retrieve_next (server
, &line
)) >= 0)
857 if (mbx_write (line
, ret
, arg
) != OK
)
859 strcpy (Errmsg
, strerror (errno
));
867 char *error
= concat ("Error from POP server: ", pop_error
, "");
868 strncpy (Errmsg
, error
, sizeof (Errmsg
));
869 Errmsg
[sizeof (Errmsg
)-1] = '\0';
877 /* Do this as a macro instead of using strcmp to save on execution time. */
878 #define IS_FROM_LINE(a) ((a[0] == 'F') \
885 mbx_write (line
, len
, mbf
)
890 #ifdef MOVEMAIL_QUOTE_POP_FROM_LINES
891 if (IS_FROM_LINE (line
))
893 if (fputc ('>', mbf
) == EOF
)
897 if (line
[0] == '\037')
899 if (fputs ("^_", mbf
) == EOF
)
904 if (fwrite (line
, 1, len
, mbf
) != len
)
906 if (fputc (0x0a, mbf
) == EOF
)
912 mbx_delimit_begin (mbf
)
915 if (fputs ("\f\n0, unseen,,\n", mbf
) == EOF
)
920 mbx_delimit_end (mbf
)
923 if (putc ('\037', mbf
) == EOF
)
928 #endif /* MAIL_USE_POP */
930 #ifndef HAVE_STRERROR
935 extern char *sys_errlist
[];
938 if (errnum
>= 0 && errnum
< sys_nerr
)
939 return sys_errlist
[errnum
];
940 return (char *) "Unknown error";
943 #endif /* ! HAVE_STRERROR */