(Fdelete_backward_char): Convert Finsert_char argument to
[emacs.git] / lib-src / movemail.c
blob866f0b08139e2a309982cf5e3c315dd12fc75fc7
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)
10 any later version.
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
44 * Modified routines:
45 * main - added code within #ifdef MAIL_USE_POP; added setuid (getuid ())
46 * after POP code.
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>
60 #include <sys/stat.h>
61 #include <sys/file.h>
62 #include <stdio.h>
63 #include <errno.h>
64 #include <../src/syswait.h>
65 #include <getopt.h>
66 #ifdef MAIL_USE_POP
67 #include "pop.h"
68 #endif
70 #ifdef MSDOS
71 #undef access
72 #endif /* MSDOS */
74 #ifndef DIRECTORY_SEP
75 #define DIRECTORY_SEP '/'
76 #endif
77 #ifndef IS_DIRECTORY_SEP
78 #define IS_DIRECTORY_SEP(_c_) ((_c_) == DIRECTORY_SEP)
79 #endif
81 #ifdef WINDOWSNT
82 #undef access
83 #undef unlink
84 #define fork() 0
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 #ifdef USG
95 #include <fcntl.h>
96 #include <unistd.h>
97 #ifndef F_OK
98 #define F_OK 0
99 #define X_OK 1
100 #define W_OK 2
101 #define R_OK 4
102 #endif
103 #endif /* USG */
105 #ifdef HAVE_UNISTD_H
106 #include <unistd.h>
107 #endif
109 #if defined (XENIX) || defined (WINDOWSNT)
110 #include <sys/locking.h>
111 #endif
113 #ifdef MAIL_USE_LOCKF
114 #define MAIL_USE_SYSTEM_LOCK
115 #endif
117 #ifdef MAIL_USE_FLOCK
118 #define MAIL_USE_SYSTEM_LOCK
119 #endif
121 #ifdef MAIL_USE_MMDF
122 extern int lk_open (), lk_close ();
123 #endif
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
129 files appear in. */
130 #ifdef MAILDIR
131 #define MAIL_USE_MAILLOCK
132 static char *mail_spool_name ();
133 #endif
134 #endif
136 /* Cancel substitutions made by config.h for Emacs. */
137 #undef open
138 #undef read
139 #undef write
140 #undef close
142 #ifndef errno
143 extern int errno;
144 #endif
145 char *strerror ();
146 extern char *rindex ();
148 void fatal ();
149 void error ();
150 void pfatal_with_name ();
151 void pfatal_and_delete ();
152 char *concat ();
153 long *xmalloc ();
154 int popmail ();
155 int pop_retr ();
156 int mbx_write ();
157 int mbx_delimit_begin ();
158 int mbx_delimit_end ();
160 /* Nonzero means this is name of a lock file to delete on fatal error. */
161 char *delete_lockname;
164 main (argc, argv)
165 int argc;
166 char **argv;
168 char *inname, *outname;
169 int indesc, outdesc;
170 int nread;
171 WAITTYPE status;
172 int c, preserve_mail = 0;
174 #ifndef MAIL_USE_SYSTEM_LOCK
175 struct stat st;
176 long now;
177 int tem;
178 char *lockname, *p;
179 char *tempname;
180 int desc;
181 #endif /* not MAIL_USE_SYSTEM_LOCK */
183 #ifdef MAIL_USE_MAILLOCK
184 char *spool_name;
185 #endif
187 delete_lockname = 0;
189 while ((c = getopt (argc, argv, "p")) != EOF)
191 switch (c) {
192 case 'p':
193 preserve_mail++;
194 break;
195 default:
196 exit(1);
200 if (
201 #ifdef MAIL_USE_POP
202 (argc - optind < 2) || (argc - optind > 3)
203 #else
204 (argc - optind != 2)
205 #endif
208 fprintf (stderr, "Usage: movemail [-p] inbox destfile%s\n",
209 #ifdef MAIL_USE_POP
210 " [POP-password]"
211 #else
213 #endif
215 exit (1);
218 inname = argv[optind];
219 outname = argv[optind+1];
221 #ifdef MAIL_USE_MMDF
222 mmdf_init (argv[0]);
223 #endif
225 if (*outname == 0)
226 fatal ("Destination file name is empty", 0);
228 /* Check access to output file. */
229 if (access (outname, F_OK) == 0 && access (outname, W_OK) != 0)
230 pfatal_with_name (outname);
232 /* Also check that outname's directory is writable to the real uid. */
234 char *buf = (char *) xmalloc (strlen (outname) + 1);
235 char *p;
236 strcpy (buf, outname);
237 p = buf + strlen (buf);
238 while (p > buf && !IS_DIRECTORY_SEP (p[-1]))
239 *--p = 0;
240 if (p == buf)
241 *p++ = '.';
242 if (access (buf, W_OK) != 0)
243 pfatal_with_name (buf);
244 free (buf);
247 #ifdef MAIL_USE_POP
248 if (!strncmp (inname, "po:", 3))
250 int status;
252 status = popmail (inname + 3, outname, preserve_mail,
253 (argc - optind == 3) ? argv[optind+2] : NULL);
254 exit (status);
257 setuid (getuid ());
258 #endif /* MAIL_USE_POP */
260 #ifndef DISABLE_DIRECT_ACCESS
262 /* Check access to input file. */
263 if (access (inname, R_OK | W_OK) != 0)
264 pfatal_with_name (inname);
266 #ifndef MAIL_USE_MMDF
267 #ifndef MAIL_USE_SYSTEM_LOCK
268 #ifdef MAIL_USE_MAILLOCK
269 spool_name = mail_spool_name (inname);
270 if (! spool_name)
271 #endif
273 /* Use a lock file named after our first argument with .lock appended:
274 If it exists, the mail file is locked. */
275 /* Note: this locking mechanism is *required* by the mailer
276 (on systems which use it) to prevent loss of mail.
278 On systems that use a lock file, extracting the mail without locking
279 WILL occasionally cause loss of mail due to timing errors!
281 So, if creation of the lock file fails
282 due to access permission on the mail spool directory,
283 you simply MUST change the permission
284 and/or make movemail a setgid program
285 so it can create lock files properly.
287 You might also wish to verify that your system is one
288 which uses lock files for this purpose. Some systems use other methods.
290 If your system uses the `flock' system call for mail locking,
291 define MAIL_USE_SYSTEM_LOCK in config.h or the s-*.h file
292 and recompile movemail. If the s- file for your system
293 should define MAIL_USE_SYSTEM_LOCK but does not, send a bug report
294 to bug-gnu-emacs@prep.ai.mit.edu so we can fix it. */
296 lockname = concat (inname, ".lock", "");
297 tempname = (char *) xmalloc (strlen (inname) + strlen ("EXXXXXX") + 1);
298 strcpy (tempname, inname);
299 p = tempname + strlen (tempname);
300 while (p != tempname && !IS_DIRECTORY_SEP (p[-1]))
301 p--;
302 *p = 0;
303 strcpy (p, "EXXXXXX");
304 mktemp (tempname);
305 unlink (tempname);
307 while (1)
309 /* Create the lock file, but not under the lock file name. */
310 /* Give up if cannot do that. */
311 desc = open (tempname, O_WRONLY | O_CREAT | O_EXCL, 0666);
312 if (desc < 0)
314 char *message = (char *) xmalloc (strlen (tempname) + 50);
315 sprintf (message, "%s--see source file lib-src/movemail.c",
316 tempname);
317 pfatal_with_name (message);
319 close (desc);
321 tem = link (tempname, lockname);
322 unlink (tempname);
323 if (tem >= 0)
324 break;
325 sleep (1);
327 /* If lock file is five minutes old, unlock it.
328 Five minutes should be good enough to cope with crashes
329 and wedgitude, and long enough to avoid being fooled
330 by time differences between machines. */
331 if (stat (lockname, &st) >= 0)
333 now = time (0);
334 if (st.st_ctime < now - 300)
335 unlink (lockname);
339 delete_lockname = lockname;
341 #endif /* not MAIL_USE_SYSTEM_LOCK */
342 #endif /* not MAIL_USE_MMDF */
344 if (fork () == 0)
346 int lockcount = 0;
347 int status = 0;
348 #if defined (MAIL_USE_MAILLOCK) && defined (HAVE_TOUCHLOCK)
349 long touched_lock, now;
350 #endif
352 setuid (getuid ());
354 #ifndef MAIL_USE_MMDF
355 #ifdef MAIL_USE_SYSTEM_LOCK
356 indesc = open (inname, O_RDWR);
357 #else /* if not MAIL_USE_SYSTEM_LOCK */
358 indesc = open (inname, O_RDONLY);
359 #endif /* not MAIL_USE_SYSTEM_LOCK */
360 #else /* MAIL_USE_MMDF */
361 indesc = lk_open (inname, O_RDONLY, 0, 0, 10);
362 #endif /* MAIL_USE_MMDF */
364 if (indesc < 0)
365 pfatal_with_name (inname);
367 #if defined (BSD_SYSTEM) || defined (XENIX)
368 /* In case movemail is setuid to root, make sure the user can
369 read the output file. */
370 /* This is desirable for all systems
371 but I don't want to assume all have the umask system call */
372 umask (umask (0) & 0333);
373 #endif /* BSD_SYSTEM || XENIX */
374 outdesc = open (outname, O_WRONLY | O_CREAT | O_EXCL, 0666);
375 if (outdesc < 0)
376 pfatal_with_name (outname);
378 /* This label exists so we can retry locking
379 after a delay, if it got EAGAIN or EBUSY. */
380 retry_lock:
382 /* Try to lock it. */
383 #ifdef MAIL_USE_MAILLOCK
384 if (spool_name)
386 /* The "0 - " is to make it a negative number if maillock returns
387 non-zero. */
388 status = 0 - maillock (spool_name, 1);
389 #ifdef HAVE_TOUCHLOCK
390 touched_lock = time (0);
391 #endif
392 lockcount = 5;
394 else
395 #endif /* MAIL_USE_MAILLOCK */
397 #ifdef MAIL_USE_SYSTEM_LOCK
398 #ifdef MAIL_USE_LOCKF
399 status = lockf (indesc, F_LOCK, 0);
400 #else /* not MAIL_USE_LOCKF */
401 #ifdef XENIX
402 status = locking (indesc, LK_RLCK, 0L);
403 #else
404 #ifdef WINDOWSNT
405 status = locking (indesc, LK_RLCK, -1L);
406 #else
407 status = flock (indesc, LOCK_EX);
408 #endif
409 #endif
410 #endif /* not MAIL_USE_LOCKF */
411 #endif /* MAIL_USE_SYSTEM_LOCK */
414 /* If it fails, retry up to 5 times
415 for certain failure codes. */
416 if (status < 0)
418 if (++lockcount <= 5)
420 #ifdef EAGAIN
421 if (errno == EAGAIN)
423 sleep (1);
424 goto retry_lock;
426 #endif
427 #ifdef EBUSY
428 if (errno == EBUSY)
430 sleep (1);
431 goto retry_lock;
433 #endif
436 pfatal_with_name (inname);
440 char buf[1024];
442 while (1)
444 nread = read (indesc, buf, sizeof buf);
445 if (nread != write (outdesc, buf, nread))
447 int saved_errno = errno;
448 unlink (outname);
449 errno = saved_errno;
450 pfatal_with_name (outname);
452 if (nread < sizeof buf)
453 break;
454 #if defined (MAIL_USE_MAILLOCK) && defined (HAVE_TOUCHLOCK)
455 if (spool_name)
457 now = time (0);
458 if (now - touched_lock > 60)
460 touchlock ();
461 touched_lock = now;
464 #endif /* MAIL_USE_MAILLOCK */
468 #ifdef BSD_SYSTEM
469 if (fsync (outdesc) < 0)
470 pfatal_and_delete (outname);
471 #endif
473 /* Check to make sure no errors before we zap the inbox. */
474 if (close (outdesc) != 0)
475 pfatal_and_delete (outname);
477 #ifdef MAIL_USE_SYSTEM_LOCK
478 if (! preserve_mail)
480 #if defined (STRIDE) || defined (XENIX) || defined (WINDOWSNT)
481 /* Stride, xenix have file locking, but no ftruncate.
482 This mess will do. */
483 close (open (inname, O_CREAT | O_TRUNC | O_RDWR, 0666));
484 #else
485 ftruncate (indesc, 0L);
487 #endif /* STRIDE or XENIX */
488 #endif /* MAIL_USE_SYSTEM_LOCK */
490 #ifdef MAIL_USE_MMDF
491 lk_close (indesc, 0, 0, 0);
492 #else
493 close (indesc);
494 #endif
496 #ifndef MAIL_USE_SYSTEM_LOCK
497 if (! preserve_mail)
499 /* Delete the input file; if we can't, at least get rid of its
500 contents. */
501 #ifdef MAIL_UNLINK_SPOOL
502 /* This is generally bad to do, because it destroys the permissions
503 that were set on the file. Better to just empty the file. */
504 if (unlink (inname) < 0 && errno != ENOENT)
505 #endif /* MAIL_UNLINK_SPOOL */
506 creat (inname, 0600);
508 #endif /* not MAIL_USE_SYSTEM_LOCK */
510 #ifdef MAIL_USE_MAILLOCK
511 /* This has to occur in the child, i.e., in the process that
512 acquired the lock! */
513 if (spool_name)
514 mailunlock ();
515 #endif
516 exit (0);
519 wait (&status);
520 if (!WIFEXITED (status))
521 exit (1);
522 else if (WRETCODE (status) != 0)
523 exit (WRETCODE (status));
525 #if !defined (MAIL_USE_MMDF) && !defined (MAIL_USE_SYSTEM_LOCK)
526 #ifdef MAIL_USE_MAILLOCK
527 if (! spool_name)
528 #endif /* MAIL_USE_MAILLOCK */
529 unlink (lockname);
530 #endif /* not MAIL_USE_MMDF and not MAIL_USE_SYSTEM_LOCK */
532 #endif /* ! DISABLE_DIRECT_ACCESS */
534 return 0;
537 #ifdef MAIL_USE_MAILLOCK
538 /* This function uses stat to confirm that the mail directory is
539 identical to the directory of the input file, rather than just
540 string-comparing the two paths, because one or both of them might
541 be symbolic links pointing to some other directory. */
542 static char *
543 mail_spool_name (inname)
544 char *inname;
546 struct stat stat1, stat2;
547 char *indir, *fname;
548 int status;
550 if (! (fname = rindex (inname, '/')))
551 return NULL;
553 fname++;
555 if (stat (MAILDIR, &stat1) < 0)
556 return NULL;
558 indir = (char *) xmalloc (fname - inname + 1);
559 strncpy (indir, inname, fname - inname);
560 indir[fname-inname] = '\0';
563 status = stat (indir, &stat2);
565 free (indir);
567 if (status < 0)
568 return NULL;
570 if (stat1.st_dev == stat2.st_dev
571 && stat1.st_ino == stat2.st_ino)
572 return fname;
574 return NULL;
576 #endif /* MAIL_USE_MAILLOCK */
578 /* Print error message and exit. */
580 void
581 fatal (s1, s2)
582 char *s1, *s2;
584 if (delete_lockname)
585 unlink (delete_lockname);
586 error (s1, s2);
587 exit (1);
590 /* Print error message. `s1' is printf control string, `s2' is arg for it. */
592 void
593 error (s1, s2, s3)
594 char *s1, *s2, *s3;
596 fprintf (stderr, "movemail: ");
597 fprintf (stderr, s1, s2, s3);
598 fprintf (stderr, "\n");
601 void
602 pfatal_with_name (name)
603 char *name;
605 char *s = concat ("", strerror (errno), " for %s");
606 fatal (s, name);
609 void
610 pfatal_and_delete (name)
611 char *name;
613 char *s = concat ("", strerror (errno), " for %s");
614 unlink (name);
615 fatal (s, name);
618 /* Return a newly-allocated string whose contents concatenate those of s1, s2, s3. */
620 char *
621 concat (s1, s2, s3)
622 char *s1, *s2, *s3;
624 int len1 = strlen (s1), len2 = strlen (s2), len3 = strlen (s3);
625 char *result = (char *) xmalloc (len1 + len2 + len3 + 1);
627 strcpy (result, s1);
628 strcpy (result + len1, s2);
629 strcpy (result + len1 + len2, s3);
630 *(result + len1 + len2 + len3) = 0;
632 return result;
635 /* Like malloc but get fatal error if memory is exhausted. */
637 long *
638 xmalloc (size)
639 unsigned size;
641 long *result = (long *) malloc (size);
642 if (!result)
643 fatal ("virtual memory exhausted", 0);
644 return result;
647 /* This is the guts of the interface to the Post Office Protocol. */
649 #ifdef MAIL_USE_POP
651 #ifndef WINDOWSNT
652 #include <sys/socket.h>
653 #include <netinet/in.h>
654 #include <netdb.h>
655 #else
656 #undef _WINSOCKAPI_
657 #include <winsock.h>
658 #endif
659 #include <pwd.h>
661 #define NOTOK (-1)
662 #define OK 0
663 #define DONE 1
665 char *progname;
666 FILE *sfi;
667 FILE *sfo;
668 char ibuffer[BUFSIZ];
669 char obuffer[BUFSIZ];
670 char Errmsg[80];
672 popmail (user, outfile, preserve, password)
673 char *user;
674 char *outfile;
675 int preserve;
676 char *password;
678 int nmsgs, nbytes;
679 register int i;
680 int mbfi;
681 FILE *mbf;
682 char *getenv ();
683 popserver server;
685 server = pop_open (0, user, password, POP_NO_GETPASS);
686 if (! server)
688 error (pop_error);
689 return (1);
692 if (pop_stat (server, &nmsgs, &nbytes))
694 error (pop_error);
695 return (1);
698 if (!nmsgs)
700 pop_close (server);
701 return (0);
704 mbfi = open (outfile, O_WRONLY | O_CREAT | O_EXCL, 0666);
705 if (mbfi < 0)
707 pop_close (server);
708 error ("Error in open: %s, %s", strerror (errno), outfile);
709 return (1);
711 fchown (mbfi, getuid (), -1);
713 if ((mbf = fdopen (mbfi, "wb")) == NULL)
715 pop_close (server);
716 error ("Error in fdopen: %s", strerror (errno));
717 close (mbfi);
718 unlink (outfile);
719 return (1);
722 for (i = 1; i <= nmsgs; i++)
724 mbx_delimit_begin (mbf);
725 if (pop_retr (server, i, mbf) != OK)
727 error (Errmsg);
728 close (mbfi);
729 return (1);
731 mbx_delimit_end (mbf);
732 fflush (mbf);
733 if (ferror (mbf))
735 error ("Error in fflush: %s", strerror (errno));
736 pop_close (server);
737 close (mbfi);
738 return (1);
742 /* On AFS, a call to write only modifies the file in the local
743 * workstation's AFS cache. The changes are not written to the server
744 * until a call to fsync or close is made. Users with AFS home
745 * directories have lost mail when over quota because these checks were
746 * not made in previous versions of movemail. */
748 #ifdef BSD_SYSTEM
749 if (fsync (mbfi) < 0)
751 error ("Error in fsync: %s", strerror (errno));
752 return (1);
754 #endif
756 if (close (mbfi) == -1)
758 error ("Error in close: %s", strerror (errno));
759 return (1);
762 if (! preserve)
763 for (i = 1; i <= nmsgs; i++)
765 if (pop_delete (server, i))
767 error (pop_error);
768 pop_close (server);
769 return (1);
773 if (pop_quit (server))
775 error (pop_error);
776 return (1);
779 return (0);
783 pop_retr (server, msgno, arg)
784 popserver server;
785 FILE *arg;
787 extern char *strerror ();
788 char *line;
789 int ret;
791 if (pop_retrieve_first (server, msgno, &line))
793 strncpy (Errmsg, pop_error, sizeof (Errmsg));
794 Errmsg[sizeof (Errmsg)-1] = '\0';
795 return (NOTOK);
798 while (! (ret = pop_retrieve_next (server, &line)))
800 if (! line)
801 break;
803 if (mbx_write (line, arg) != OK)
805 strcpy (Errmsg, strerror (errno));
806 pop_close (server);
807 return (NOTOK);
811 if (ret)
813 strncpy (Errmsg, pop_error, sizeof (Errmsg));
814 Errmsg[sizeof (Errmsg)-1] = '\0';
815 return (NOTOK);
818 return (OK);
821 /* Do this as a macro instead of using strcmp to save on execution time. */
822 #define IS_FROM_LINE(a) ((a[0] == 'F') \
823 && (a[1] == 'r') \
824 && (a[2] == 'o') \
825 && (a[3] == 'm') \
826 && (a[4] == ' '))
829 mbx_write (line, mbf)
830 char *line;
831 FILE *mbf;
833 if (IS_FROM_LINE (line))
835 if (fputc ('>', mbf) == EOF)
836 return (NOTOK);
838 if (fputs (line, mbf) == EOF)
839 return (NOTOK);
840 if (fputc (0x0a, mbf) == EOF)
841 return (NOTOK);
842 return (OK);
846 mbx_delimit_begin (mbf)
847 FILE *mbf;
849 if (fputs ("\f\n0, unseen,,\n", mbf) == EOF)
850 return (NOTOK);
851 return (OK);
854 mbx_delimit_end (mbf)
855 FILE *mbf;
857 if (putc ('\037', mbf) == EOF)
858 return (NOTOK);
859 return (OK);
862 #endif /* MAIL_USE_POP */
864 #ifndef HAVE_STRERROR
865 char *
866 strerror (errnum)
867 int errnum;
869 extern char *sys_errlist[];
870 extern int sys_nerr;
872 if (errnum >= 0 && errnum < sys_nerr)
873 return sys_errlist[errnum];
874 return (char *) "Unknown error";
877 #endif /* ! HAVE_STRERROR */