]>
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, 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 sys_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_flags
= POP_NO_GETPASS
| POP_NO_GSSAPI
;
202 # define ARGSTR "gkp"
203 #else /* ! MAIL_USE_POP */
205 #endif /* MAIL_USE_POP */
210 'g' enables Kerberos and disables GSS-API.
211 'k' enables GSS-API and disables Kerberos.
213 By default, Kerberos is enabled (if it is compiled in) and
214 GSS-API is disabled, i.e., "-k" is the default. However, I'm
215 putting the flag in anyway, in case we decide to add new
216 authentication methods or change the default later.
219 while ((c
= getopt (argc
, argv
, ARGSTR
)) != EOF
)
224 pop_flags
|= POP_NO_KERBEROS
;
225 pop_flags
&= ~POP_NO_GSSAPI
;
228 pop_flags
|= POP_NO_GSSAPI
;
229 pop_flags
&= ~POP_NO_KERBEROS
;
242 (argc
- optind
< 2) || (argc
- optind
> 3)
249 fprintf (stderr
, "Usage: movemail %s[-p] inbox destfile%s\n",
251 "[-g|-k] ", " [POP-password]"
259 inname
= argv
[optind
];
260 outname
= argv
[optind
+1];
267 fatal ("Destination file name is empty", 0);
269 /* Check access to output file. */
270 if (access (outname
, F_OK
) == 0 && access (outname
, W_OK
) != 0)
271 pfatal_with_name (outname
);
273 /* Also check that outname's directory is writable to the real uid. */
275 char *buf
= (char *) xmalloc (strlen (outname
) + 1);
277 strcpy (buf
, outname
);
278 p
= buf
+ strlen (buf
);
279 while (p
> buf
&& !IS_DIRECTORY_SEP (p
[-1]))
283 if (access (buf
, W_OK
) != 0)
284 pfatal_with_name (buf
);
289 if (!strncmp (inname
, "po:", 3))
293 status
= popmail (inname
+ 3, outname
, preserve_mail
,
294 (argc
- optind
== 3) ? argv
[optind
+2] : NULL
, pop_flags
);
299 #endif /* MAIL_USE_POP */
301 #ifndef DISABLE_DIRECT_ACCESS
303 /* Check access to input file. */
304 if (access (inname
, R_OK
| W_OK
) != 0)
305 pfatal_with_name (inname
);
307 #ifndef MAIL_USE_MMDF
308 #ifndef MAIL_USE_SYSTEM_LOCK
309 #ifdef MAIL_USE_MAILLOCK
310 spool_name
= mail_spool_name (inname
);
314 /* Use a lock file named after our first argument with .lock appended:
315 If it exists, the mail file is locked. */
316 /* Note: this locking mechanism is *required* by the mailer
317 (on systems which use it) to prevent loss of mail.
319 On systems that use a lock file, extracting the mail without locking
320 WILL occasionally cause loss of mail due to timing errors!
322 So, if creation of the lock file fails
323 due to access permission on the mail spool directory,
324 you simply MUST change the permission
325 and/or make movemail a setgid program
326 so it can create lock files properly.
328 You might also wish to verify that your system is one
329 which uses lock files for this purpose. Some systems use other methods.
331 If your system uses the `flock' system call for mail locking,
332 define MAIL_USE_SYSTEM_LOCK in config.h or the s-*.h file
333 and recompile movemail. If the s- file for your system
334 should define MAIL_USE_SYSTEM_LOCK but does not, send a bug report
335 to bug-gnu-emacs@prep.ai.mit.edu so we can fix it. */
337 lockname
= concat (inname
, ".lock", "");
338 tempname
= (char *) xmalloc (strlen (inname
) + strlen ("EXXXXXX") + 1);
339 strcpy (tempname
, inname
);
340 p
= tempname
+ strlen (tempname
);
341 while (p
!= tempname
&& !IS_DIRECTORY_SEP (p
[-1]))
344 strcpy (p
, "EXXXXXX");
350 /* Create the lock file, but not under the lock file name. */
351 /* Give up if cannot do that. */
352 desc
= open (tempname
, O_WRONLY
| O_CREAT
| O_EXCL
, 0666);
355 char *message
= (char *) xmalloc (strlen (tempname
) + 50);
356 sprintf (message
, "%s--see source file lib-src/movemail.c",
358 pfatal_with_name (message
);
362 tem
= link (tempname
, lockname
);
368 /* If lock file is five minutes old, unlock it.
369 Five minutes should be good enough to cope with crashes
370 and wedgitude, and long enough to avoid being fooled
371 by time differences between machines. */
372 if (stat (lockname
, &st
) >= 0)
375 if (st
.st_ctime
< now
- 300)
380 delete_lockname
= lockname
;
382 #endif /* not MAIL_USE_SYSTEM_LOCK */
383 #endif /* not MAIL_USE_MMDF */
389 #if defined (MAIL_USE_MAILLOCK) && defined (HAVE_TOUCHLOCK)
390 long touched_lock
, now
;
395 #ifndef MAIL_USE_MMDF
396 #ifdef MAIL_USE_SYSTEM_LOCK
397 indesc
= open (inname
, O_RDWR
);
398 #else /* if not MAIL_USE_SYSTEM_LOCK */
399 indesc
= open (inname
, O_RDONLY
);
400 #endif /* not MAIL_USE_SYSTEM_LOCK */
401 #else /* MAIL_USE_MMDF */
402 indesc
= lk_open (inname
, O_RDONLY
, 0, 0, 10);
403 #endif /* MAIL_USE_MMDF */
406 pfatal_with_name (inname
);
408 #if defined (BSD_SYSTEM) || defined (XENIX)
409 /* In case movemail is setuid to root, make sure the user can
410 read the output file. */
411 /* This is desirable for all systems
412 but I don't want to assume all have the umask system call */
413 umask (umask (0) & 0333);
414 #endif /* BSD_SYSTEM || XENIX */
415 outdesc
= open (outname
, O_WRONLY
| O_CREAT
| O_EXCL
, 0666);
417 pfatal_with_name (outname
);
419 /* This label exists so we can retry locking
420 after a delay, if it got EAGAIN or EBUSY. */
423 /* Try to lock it. */
424 #ifdef MAIL_USE_MAILLOCK
427 /* The "0 - " is to make it a negative number if maillock returns
429 status
= 0 - maillock (spool_name
, 1);
430 #ifdef HAVE_TOUCHLOCK
431 touched_lock
= time (0);
436 #endif /* MAIL_USE_MAILLOCK */
438 #ifdef MAIL_USE_SYSTEM_LOCK
439 #ifdef MAIL_USE_LOCKF
440 status
= lockf (indesc
, F_LOCK
, 0);
441 #else /* not MAIL_USE_LOCKF */
443 status
= locking (indesc
, LK_RLCK
, 0L);
446 status
= locking (indesc
, LK_RLCK
, -1L);
448 status
= flock (indesc
, LOCK_EX
);
451 #endif /* not MAIL_USE_LOCKF */
452 #endif /* MAIL_USE_SYSTEM_LOCK */
455 /* If it fails, retry up to 5 times
456 for certain failure codes. */
459 if (++lockcount
<= 5)
477 pfatal_with_name (inname
);
485 nread
= read (indesc
, buf
, sizeof buf
);
486 if (nread
!= write (outdesc
, buf
, nread
))
488 int saved_errno
= errno
;
491 pfatal_with_name (outname
);
493 if (nread
< sizeof buf
)
495 #if defined (MAIL_USE_MAILLOCK) && defined (HAVE_TOUCHLOCK)
499 if (now
- touched_lock
> 60)
505 #endif /* MAIL_USE_MAILLOCK */
510 if (fsync (outdesc
) < 0)
511 pfatal_and_delete (outname
);
514 /* Check to make sure no errors before we zap the inbox. */
515 if (close (outdesc
) != 0)
516 pfatal_and_delete (outname
);
518 #ifdef MAIL_USE_SYSTEM_LOCK
521 #if defined (STRIDE) || defined (XENIX) || defined (WINDOWSNT)
522 /* Stride, xenix have file locking, but no ftruncate.
523 This mess will do. */
524 close (open (inname
, O_CREAT
| O_TRUNC
| O_RDWR
, 0666));
526 ftruncate (indesc
, 0L);
527 #endif /* STRIDE or XENIX */
529 #endif /* MAIL_USE_SYSTEM_LOCK */
532 lk_close (indesc
, 0, 0, 0);
537 #ifndef MAIL_USE_SYSTEM_LOCK
540 /* Delete the input file; if we can't, at least get rid of its
542 #ifdef MAIL_UNLINK_SPOOL
543 /* This is generally bad to do, because it destroys the permissions
544 that were set on the file. Better to just empty the file. */
545 if (unlink (inname
) < 0 && errno
!= ENOENT
)
546 #endif /* MAIL_UNLINK_SPOOL */
547 creat (inname
, 0600);
549 #endif /* not MAIL_USE_SYSTEM_LOCK */
551 #ifdef MAIL_USE_MAILLOCK
552 /* This has to occur in the child, i.e., in the process that
553 acquired the lock! */
561 if (!WIFEXITED (status
))
563 else if (WRETCODE (status
) != 0)
564 exit (WRETCODE (status
));
566 #if !defined (MAIL_USE_MMDF) && !defined (MAIL_USE_SYSTEM_LOCK)
567 #ifdef MAIL_USE_MAILLOCK
569 #endif /* MAIL_USE_MAILLOCK */
571 #endif /* not MAIL_USE_MMDF and not MAIL_USE_SYSTEM_LOCK */
573 #endif /* ! DISABLE_DIRECT_ACCESS */
578 #ifdef MAIL_USE_MAILLOCK
579 /* This function uses stat to confirm that the mail directory is
580 identical to the directory of the input file, rather than just
581 string-comparing the two paths, because one or both of them might
582 be symbolic links pointing to some other directory. */
584 mail_spool_name (inname
)
587 struct stat stat1
, stat2
;
591 if (! (fname
= rindex (inname
, '/')))
596 if (stat (MAILDIR
, &stat1
) < 0)
599 indir
= (char *) xmalloc (fname
- inname
+ 1);
600 strncpy (indir
, inname
, fname
- inname
);
601 indir
[fname
-inname
] = '\0';
604 status
= stat (indir
, &stat2
);
611 if (stat1
.st_dev
== stat2
.st_dev
612 && stat1
.st_ino
== stat2
.st_ino
)
617 #endif /* MAIL_USE_MAILLOCK */
619 /* Print error message and exit. */
626 unlink (delete_lockname
);
631 /* Print error message. `s1' is printf control string, `s2' is arg for it. */
637 fprintf (stderr
, "movemail: ");
638 fprintf (stderr
, s1
, s2
, s3
);
639 fprintf (stderr
, "\n");
643 pfatal_with_name (name
)
646 char *s
= concat ("", strerror (errno
), " for %s");
651 pfatal_and_delete (name
)
654 char *s
= concat ("", strerror (errno
), " for %s");
659 /* Return a newly-allocated string whose contents concatenate those of s1, s2, s3. */
665 int len1
= strlen (s1
), len2
= strlen (s2
), len3
= strlen (s3
);
666 char *result
= (char *) xmalloc (len1
+ len2
+ len3
+ 1);
669 strcpy (result
+ len1
, s2
);
670 strcpy (result
+ len1
+ len2
, s3
);
671 *(result
+ len1
+ len2
+ len3
) = 0;
676 /* Like malloc but get fatal error if memory is exhausted. */
682 long *result
= (long *) malloc (size
);
684 fatal ("virtual memory exhausted", 0);
688 /* This is the guts of the interface to the Post Office Protocol. */
693 #include <sys/socket.h>
694 #include <netinet/in.h>
709 char ibuffer
[BUFSIZ
];
710 char obuffer
[BUFSIZ
];
713 popmail (user
, outfile
, preserve
, password
, flags
)
727 server
= pop_open (0, user
, password
, flags
);
734 if (pop_stat (server
, &nmsgs
, &nbytes
))
746 mbfi
= open (outfile
, O_WRONLY
| O_CREAT
| O_EXCL
, 0666);
750 error ("Error in open: %s, %s", strerror (errno
), outfile
);
753 fchown (mbfi
, getuid (), -1);
755 if ((mbf
= fdopen (mbfi
, "wb")) == NULL
)
758 error ("Error in fdopen: %s", strerror (errno
));
764 for (i
= 1; i
<= nmsgs
; i
++)
766 mbx_delimit_begin (mbf
);
767 if (pop_retr (server
, i
, mbf
) != OK
)
773 mbx_delimit_end (mbf
);
777 error ("Error in fflush: %s", strerror (errno
));
784 /* On AFS, a call to write only modifies the file in the local
785 * workstation's AFS cache. The changes are not written to the server
786 * until a call to fsync or close is made. Users with AFS home
787 * directories have lost mail when over quota because these checks were
788 * not made in previous versions of movemail. */
791 if (fsync (mbfi
) < 0)
793 error ("Error in fsync: %s", strerror (errno
));
798 if (close (mbfi
) == -1)
800 error ("Error in close: %s", strerror (errno
));
805 for (i
= 1; i
<= nmsgs
; i
++)
807 if (pop_delete (server
, i
))
815 if (pop_quit (server
))
825 pop_retr (server
, msgno
, arg
)
829 extern char *strerror ();
833 if (pop_retrieve_first (server
, msgno
, &line
))
835 strncpy (Errmsg
, pop_error
, sizeof (Errmsg
));
836 Errmsg
[sizeof (Errmsg
)-1] = '\0';
840 while ((ret
= pop_retrieve_next (server
, &line
)) >= 0)
845 if (mbx_write (line
, ret
, arg
) != OK
)
847 strcpy (Errmsg
, strerror (errno
));
855 strncpy (Errmsg
, pop_error
, sizeof (Errmsg
));
856 Errmsg
[sizeof (Errmsg
)-1] = '\0';
863 /* Do this as a macro instead of using strcmp to save on execution time. */
864 #define IS_FROM_LINE(a) ((a[0] == 'F') \
871 mbx_write (line
, len
, mbf
)
876 #ifdef MOVEMAIL_QUOTE_POP_FROM_LINES
877 if (IS_FROM_LINE (line
))
879 if (fputc ('>', mbf
) == EOF
)
883 if (line
[0] == '\037')
885 if (fputs ("^_", mbf
) == EOF
)
890 if (fwrite (line
, 1, len
, mbf
) != len
)
892 if (fputc (0x0a, mbf
) == EOF
)
898 mbx_delimit_begin (mbf
)
901 if (fputs ("\f\n0, unseen,,\n", mbf
) == EOF
)
906 mbx_delimit_end (mbf
)
909 if (putc ('\037', mbf
) == EOF
)
914 #endif /* MAIL_USE_POP */
916 #ifndef HAVE_STRERROR
921 extern char *sys_errlist
[];
924 if (errnum
>= 0 && errnum
< sys_nerr
)
925 return sys_errlist
[errnum
];
926 return (char *) "Unknown error";
929 #endif /* ! HAVE_STRERROR */