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 */
94 /* Cancel substitutions made by config.h for Emacs. */
119 #if defined (XENIX) || defined (WINDOWSNT)
120 #include <sys/locking.h>
123 #ifdef MAIL_USE_LOCKF
124 #define MAIL_USE_SYSTEM_LOCK
127 #ifdef MAIL_USE_FLOCK
128 #define MAIL_USE_SYSTEM_LOCK
132 extern int lk_open (), lk_close ();
135 #if !defined (MAIL_USE_SYSTEM_LOCK) && !defined (MAIL_USE_MMDF) && \
136 defined (HAVE_LIBMAIL) && defined (HAVE_MAILLOCK_H)
137 #include <maillock.h>
138 /* We can't use maillock unless we know what directory system mail
141 #define MAIL_USE_MAILLOCK
142 static char *mail_spool_name ();
150 extern char *rindex ();
154 void pfatal_with_name ();
155 void pfatal_and_delete ();
161 int mbx_delimit_begin ();
162 int mbx_delimit_end ();
164 /* Nonzero means this is name of a lock file to delete on fatal error. */
165 char *delete_lockname
;
172 char *inname
, *outname
;
176 int c
, preserve_mail
= 0;
178 #ifndef MAIL_USE_SYSTEM_LOCK
185 #endif /* not MAIL_USE_SYSTEM_LOCK */
187 #ifdef MAIL_USE_MAILLOCK
192 int pop_flags
= POP_NO_GETPASS
| POP_NO_GSSAPI
;
193 # define ARGSTR "gkp"
194 #else /* ! MAIL_USE_POP */
196 #endif /* MAIL_USE_POP */
201 'g' enables Kerberos and disables GSS-API.
202 'k' enables GSS-API and disables Kerberos.
204 By default, Kerberos is enabled (if it is compiled in) and
205 GSS-API is disabled, i.e., "-k" is the default. However, I'm
206 putting the flag in anyway, in case we decide to add new
207 authentication methods or change the default later.
210 while ((c
= getopt (argc
, argv
, ARGSTR
)) != EOF
)
215 pop_flags
|= POP_NO_KERBEROS
;
216 pop_flags
&= ~POP_NO_GSSAPI
;
219 pop_flags
|= POP_NO_GSSAPI
;
220 pop_flags
&= ~POP_NO_KERBEROS
;
233 (argc
- optind
< 2) || (argc
- optind
> 3)
240 fprintf (stderr
, "Usage: movemail %s[-p] inbox destfile%s\n",
242 "[-g|-k] ", " [POP-password]"
250 inname
= argv
[optind
];
251 outname
= argv
[optind
+1];
258 fatal ("Destination file name is empty", 0);
260 /* Check access to output file. */
261 if (access (outname
, F_OK
) == 0 && access (outname
, W_OK
) != 0)
262 pfatal_with_name (outname
);
264 /* Also check that outname's directory is writable to the real uid. */
266 char *buf
= (char *) xmalloc (strlen (outname
) + 1);
268 strcpy (buf
, outname
);
269 p
= buf
+ strlen (buf
);
270 while (p
> buf
&& !IS_DIRECTORY_SEP (p
[-1]))
274 if (access (buf
, W_OK
) != 0)
275 pfatal_with_name (buf
);
280 if (!strncmp (inname
, "po:", 3))
284 status
= popmail (inname
+ 3, outname
, preserve_mail
,
285 (argc
- optind
== 3) ? argv
[optind
+2] : NULL
, pop_flags
);
290 #endif /* MAIL_USE_POP */
292 #ifndef DISABLE_DIRECT_ACCESS
294 /* Check access to input file. */
295 if (access (inname
, R_OK
| W_OK
) != 0)
296 pfatal_with_name (inname
);
298 #ifndef MAIL_USE_MMDF
299 #ifndef MAIL_USE_SYSTEM_LOCK
300 #ifdef MAIL_USE_MAILLOCK
301 spool_name
= mail_spool_name (inname
);
305 /* Use a lock file named after our first argument with .lock appended:
306 If it exists, the mail file is locked. */
307 /* Note: this locking mechanism is *required* by the mailer
308 (on systems which use it) to prevent loss of mail.
310 On systems that use a lock file, extracting the mail without locking
311 WILL occasionally cause loss of mail due to timing errors!
313 So, if creation of the lock file fails
314 due to access permission on the mail spool directory,
315 you simply MUST change the permission
316 and/or make movemail a setgid program
317 so it can create lock files properly.
319 You might also wish to verify that your system is one
320 which uses lock files for this purpose. Some systems use other methods.
322 If your system uses the `flock' system call for mail locking,
323 define MAIL_USE_SYSTEM_LOCK in config.h or the s-*.h file
324 and recompile movemail. If the s- file for your system
325 should define MAIL_USE_SYSTEM_LOCK but does not, send a bug report
326 to bug-gnu-emacs@prep.ai.mit.edu so we can fix it. */
328 lockname
= concat (inname
, ".lock", "");
329 tempname
= (char *) xmalloc (strlen (inname
) + strlen ("EXXXXXX") + 1);
330 strcpy (tempname
, inname
);
331 p
= tempname
+ strlen (tempname
);
332 while (p
!= tempname
&& !IS_DIRECTORY_SEP (p
[-1]))
335 strcpy (p
, "EXXXXXX");
341 /* Create the lock file, but not under the lock file name. */
342 /* Give up if cannot do that. */
343 desc
= open (tempname
, O_WRONLY
| O_CREAT
| O_EXCL
, 0666);
346 char *message
= (char *) xmalloc (strlen (tempname
) + 50);
347 sprintf (message
, "%s--see source file lib-src/movemail.c",
349 pfatal_with_name (message
);
353 tem
= link (tempname
, lockname
);
359 /* If lock file is five minutes old, unlock it.
360 Five minutes should be good enough to cope with crashes
361 and wedgitude, and long enough to avoid being fooled
362 by time differences between machines. */
363 if (stat (lockname
, &st
) >= 0)
366 if (st
.st_ctime
< now
- 300)
371 delete_lockname
= lockname
;
373 #endif /* not MAIL_USE_SYSTEM_LOCK */
374 #endif /* not MAIL_USE_MMDF */
380 #if defined (MAIL_USE_MAILLOCK) && defined (HAVE_TOUCHLOCK)
381 long touched_lock
, now
;
386 #ifndef MAIL_USE_MMDF
387 #ifdef MAIL_USE_SYSTEM_LOCK
388 indesc
= open (inname
, O_RDWR
);
389 #else /* if not MAIL_USE_SYSTEM_LOCK */
390 indesc
= open (inname
, O_RDONLY
);
391 #endif /* not MAIL_USE_SYSTEM_LOCK */
392 #else /* MAIL_USE_MMDF */
393 indesc
= lk_open (inname
, O_RDONLY
, 0, 0, 10);
394 #endif /* MAIL_USE_MMDF */
397 pfatal_with_name (inname
);
399 #if defined (BSD_SYSTEM) || defined (XENIX)
400 /* In case movemail is setuid to root, make sure the user can
401 read the output file. */
402 /* This is desirable for all systems
403 but I don't want to assume all have the umask system call */
404 umask (umask (0) & 0333);
405 #endif /* BSD_SYSTEM || XENIX */
406 outdesc
= open (outname
, O_WRONLY
| O_CREAT
| O_EXCL
, 0666);
408 pfatal_with_name (outname
);
410 /* This label exists so we can retry locking
411 after a delay, if it got EAGAIN or EBUSY. */
414 /* Try to lock it. */
415 #ifdef MAIL_USE_MAILLOCK
418 /* The "0 - " is to make it a negative number if maillock returns
420 status
= 0 - maillock (spool_name
, 1);
421 #ifdef HAVE_TOUCHLOCK
422 touched_lock
= time (0);
427 #endif /* MAIL_USE_MAILLOCK */
429 #ifdef MAIL_USE_SYSTEM_LOCK
430 #ifdef MAIL_USE_LOCKF
431 status
= lockf (indesc
, F_LOCK
, 0);
432 #else /* not MAIL_USE_LOCKF */
434 status
= locking (indesc
, LK_RLCK
, 0L);
437 status
= locking (indesc
, LK_RLCK
, -1L);
439 status
= flock (indesc
, LOCK_EX
);
442 #endif /* not MAIL_USE_LOCKF */
443 #endif /* MAIL_USE_SYSTEM_LOCK */
446 /* If it fails, retry up to 5 times
447 for certain failure codes. */
450 if (++lockcount
<= 5)
468 pfatal_with_name (inname
);
476 nread
= read (indesc
, buf
, sizeof buf
);
477 if (nread
!= write (outdesc
, buf
, nread
))
479 int saved_errno
= errno
;
482 pfatal_with_name (outname
);
484 if (nread
< sizeof buf
)
486 #if defined (MAIL_USE_MAILLOCK) && defined (HAVE_TOUCHLOCK)
490 if (now
- touched_lock
> 60)
496 #endif /* MAIL_USE_MAILLOCK */
501 if (fsync (outdesc
) < 0)
502 pfatal_and_delete (outname
);
505 /* Check to make sure no errors before we zap the inbox. */
506 if (close (outdesc
) != 0)
507 pfatal_and_delete (outname
);
509 #ifdef MAIL_USE_SYSTEM_LOCK
512 #if defined (STRIDE) || defined (XENIX) || defined (WINDOWSNT)
513 /* Stride, xenix have file locking, but no ftruncate.
514 This mess will do. */
515 close (open (inname
, O_CREAT
| O_TRUNC
| O_RDWR
, 0666));
517 ftruncate (indesc
, 0L);
518 #endif /* STRIDE or XENIX */
520 #endif /* MAIL_USE_SYSTEM_LOCK */
523 lk_close (indesc
, 0, 0, 0);
528 #ifndef MAIL_USE_SYSTEM_LOCK
531 /* Delete the input file; if we can't, at least get rid of its
533 #ifdef MAIL_UNLINK_SPOOL
534 /* This is generally bad to do, because it destroys the permissions
535 that were set on the file. Better to just empty the file. */
536 if (unlink (inname
) < 0 && errno
!= ENOENT
)
537 #endif /* MAIL_UNLINK_SPOOL */
538 creat (inname
, 0600);
540 #endif /* not MAIL_USE_SYSTEM_LOCK */
542 #ifdef MAIL_USE_MAILLOCK
543 /* This has to occur in the child, i.e., in the process that
544 acquired the lock! */
552 if (!WIFEXITED (status
))
554 else if (WRETCODE (status
) != 0)
555 exit (WRETCODE (status
));
557 #if !defined (MAIL_USE_MMDF) && !defined (MAIL_USE_SYSTEM_LOCK)
558 #ifdef MAIL_USE_MAILLOCK
560 #endif /* MAIL_USE_MAILLOCK */
562 #endif /* not MAIL_USE_MMDF and not MAIL_USE_SYSTEM_LOCK */
564 #endif /* ! DISABLE_DIRECT_ACCESS */
569 #ifdef MAIL_USE_MAILLOCK
570 /* This function uses stat to confirm that the mail directory is
571 identical to the directory of the input file, rather than just
572 string-comparing the two paths, because one or both of them might
573 be symbolic links pointing to some other directory. */
575 mail_spool_name (inname
)
578 struct stat stat1
, stat2
;
582 if (! (fname
= rindex (inname
, '/')))
587 if (stat (MAILDIR
, &stat1
) < 0)
590 indir
= (char *) xmalloc (fname
- inname
+ 1);
591 strncpy (indir
, inname
, fname
- inname
);
592 indir
[fname
-inname
] = '\0';
595 status
= stat (indir
, &stat2
);
602 if (stat1
.st_dev
== stat2
.st_dev
603 && stat1
.st_ino
== stat2
.st_ino
)
608 #endif /* MAIL_USE_MAILLOCK */
610 /* Print error message and exit. */
617 unlink (delete_lockname
);
622 /* Print error message. `s1' is printf control string, `s2' is arg for it. */
628 fprintf (stderr
, "movemail: ");
629 fprintf (stderr
, s1
, s2
, s3
);
630 fprintf (stderr
, "\n");
634 pfatal_with_name (name
)
637 char *s
= concat ("", strerror (errno
), " for %s");
642 pfatal_and_delete (name
)
645 char *s
= concat ("", strerror (errno
), " for %s");
650 /* Return a newly-allocated string whose contents concatenate those of s1, s2, s3. */
656 int len1
= strlen (s1
), len2
= strlen (s2
), len3
= strlen (s3
);
657 char *result
= (char *) xmalloc (len1
+ len2
+ len3
+ 1);
660 strcpy (result
+ len1
, s2
);
661 strcpy (result
+ len1
+ len2
, s3
);
662 *(result
+ len1
+ len2
+ len3
) = 0;
667 /* Like malloc but get fatal error if memory is exhausted. */
673 long *result
= (long *) malloc (size
);
675 fatal ("virtual memory exhausted", 0);
679 /* This is the guts of the interface to the Post Office Protocol. */
684 #include <sys/socket.h>
685 #include <netinet/in.h>
700 char ibuffer
[BUFSIZ
];
701 char obuffer
[BUFSIZ
];
704 popmail (user
, outfile
, preserve
, password
, flags
)
718 server
= pop_open (0, user
, password
, flags
);
725 if (pop_stat (server
, &nmsgs
, &nbytes
))
737 mbfi
= open (outfile
, O_WRONLY
| O_CREAT
| O_EXCL
, 0666);
741 error ("Error in open: %s, %s", strerror (errno
), outfile
);
744 fchown (mbfi
, getuid (), -1);
746 if ((mbf
= fdopen (mbfi
, "wb")) == NULL
)
749 error ("Error in fdopen: %s", strerror (errno
));
755 for (i
= 1; i
<= nmsgs
; i
++)
757 mbx_delimit_begin (mbf
);
758 if (pop_retr (server
, i
, mbf
) != OK
)
764 mbx_delimit_end (mbf
);
768 error ("Error in fflush: %s", strerror (errno
));
775 /* On AFS, a call to write only modifies the file in the local
776 * workstation's AFS cache. The changes are not written to the server
777 * until a call to fsync or close is made. Users with AFS home
778 * directories have lost mail when over quota because these checks were
779 * not made in previous versions of movemail. */
782 if (fsync (mbfi
) < 0)
784 error ("Error in fsync: %s", strerror (errno
));
789 if (close (mbfi
) == -1)
791 error ("Error in close: %s", strerror (errno
));
796 for (i
= 1; i
<= nmsgs
; i
++)
798 if (pop_delete (server
, i
))
806 if (pop_quit (server
))
816 pop_retr (server
, msgno
, arg
)
820 extern char *strerror ();
824 if (pop_retrieve_first (server
, msgno
, &line
))
826 strncpy (Errmsg
, pop_error
, sizeof (Errmsg
));
827 Errmsg
[sizeof (Errmsg
)-1] = '\0';
831 while ((ret
= pop_retrieve_next (server
, &line
)) >= 0)
836 if (mbx_write (line
, ret
, arg
) != OK
)
838 strcpy (Errmsg
, strerror (errno
));
846 strncpy (Errmsg
, pop_error
, sizeof (Errmsg
));
847 Errmsg
[sizeof (Errmsg
)-1] = '\0';
854 /* Do this as a macro instead of using strcmp to save on execution time. */
855 #define IS_FROM_LINE(a) ((a[0] == 'F') \
862 mbx_write (line
, len
, mbf
)
867 #ifdef MOVEMAIL_QUOTE_POP_FROM_LINES
868 if (IS_FROM_LINE (line
))
870 if (fputc ('>', mbf
) == EOF
)
874 if (line
[0] == '\037')
876 if (fputs ("^_", mbf
) == EOF
)
881 if (fwrite (line
, 1, len
, mbf
) != len
)
883 if (fputc (0x0a, mbf
) == EOF
)
889 mbx_delimit_begin (mbf
)
892 if (fputs ("\f\n0, unseen,,\n", mbf
) == EOF
)
897 mbx_delimit_end (mbf
)
900 if (putc ('\037', mbf
) == EOF
)
905 #endif /* MAIL_USE_POP */
907 #ifndef HAVE_STRERROR
912 extern char *sys_errlist
[];
915 if (errnum
>= 0 && errnum
< sys_nerr
)
916 return sys_errlist
[errnum
];
917 return (char *) "Unknown error";
920 #endif /* ! HAVE_STRERROR */