*** empty log message ***
[emacs.git] / lib-src / movemail.c
blob269b3b6b64fbfde89fc22a4c08e536f3e86ab30d
1 /* movemail foo bar -- move file foo to file bar,
2 locking file foo the way /bin/mail respects.
3 Copyright (C) 1986 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 1, 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, 675 Mass Ave, Cambridge, MA 02139, USA. */
22 * Modified January, 1986 by Michael R. Gretzinger (Project Athena)
24 * Added POP (Post Office Protocol) service. When compiled -DPOP
25 * movemail will accept input filename arguments of the form
26 * "po:username". This will cause movemail to open a connection to
27 * a pop server running on $MAILHOST (environment variable). Movemail
28 * must be setuid to root in order to work with POP.
30 * New module: popmail.c
31 * Modified routines:
32 * main - added code within #ifdef MAIL_USE_POP; added setuid(getuid())
33 * after POP code.
34 * New routines in movemail.c:
35 * get_errmsg - return pointer to system error message
39 #include <sys/types.h>
40 #include <sys/stat.h>
41 #include <sys/file.h>
42 #include <errno.h>
43 #define NO_SHORTNAMES /* Tell config not to load remap.h */
44 #include "../src/config.h"
46 #ifdef USG
47 #include <fcntl.h>
48 #include <unistd.h>
49 #ifndef F_OK
50 #define F_OK 0
51 #define X_OK 1
52 #define W_OK 2
53 #define R_OK 4
54 #endif
55 #endif /* USG */
57 #ifdef XENIX
58 #include <sys/locking.h>
59 #endif
61 #ifdef MAIL_USE_MMDF
62 extern int lk_open (), lk_close ();
63 #endif
65 /* Cancel substitutions made by config.h for Emacs. */
66 #undef open
67 #undef read
68 #undef write
69 #undef close
71 char *concat ();
72 extern int errno;
74 /* Nonzero means this is name of a lock file to delete on fatal error. */
75 char *delete_lockname;
77 main (argc, argv)
78 int argc;
79 char **argv;
81 char *inname, *outname;
82 int indesc, outdesc;
83 char buf[1024];
84 int nread;
86 #ifndef MAIL_USE_FLOCK
87 struct stat st;
88 long now;
89 int tem;
90 char *lockname, *p;
91 char tempname[40];
92 int desc;
93 #endif /* not MAIL_USE_FLOCK */
95 delete_lockname = 0;
97 if (argc < 3)
98 fatal ("two arguments required");
100 inname = argv[1];
101 outname = argv[2];
103 #ifdef MAIL_USE_MMDF
104 mmdf_init (argv[0]);
105 #endif
107 /* Check access to input and output file. */
108 if (access (inname, R_OK | W_OK) != 0)
109 pfatal_with_name (inname);
110 if (access (outname, F_OK) == 0 && access (outname, W_OK) != 0)
111 pfatal_with_name (outname);
113 /* Also check that outname's directory is writeable to the real uid. */
115 char *buf = (char *) malloc (strlen (outname) + 1);
116 char *p, q;
117 strcpy (buf, outname);
118 p = buf + strlen (buf);
119 while (p > buf && p[-1] != '/')
120 *--p = 0;
121 if (p == buf)
122 *p++ = '.';
123 if (access (buf, W_OK) != 0)
124 pfatal_with_name (buf);
125 free (buf);
128 #ifdef MAIL_USE_POP
129 if (!bcmp (inname, "po:", 3))
131 int status; char *user;
133 user = (char *) rindex (inname, ':') + 1;
134 status = popmail (user, outname);
135 exit (status);
138 setuid (getuid());
139 #endif /* MAIL_USE_POP */
141 #ifndef MAIL_USE_MMDF
142 #ifndef MAIL_USE_FLOCK
143 /* Use a lock file named /usr/spool/mail/$USER.lock:
144 If it exists, the mail file is locked. */
145 lockname = concat (inname, ".lock", "");
146 strcpy (tempname, inname);
147 p = tempname + strlen (tempname);
148 while (p != tempname && p[-1] != '/')
149 p--;
150 *p = 0;
151 strcpy (p, "EXXXXXX");
152 mktemp (tempname);
153 (void) unlink (tempname);
155 while (1)
157 /* Create the lock file, but not under the lock file name. */
158 /* Give up if cannot do that. */
159 desc = open (tempname, O_WRONLY | O_CREAT, 0666);
160 if (desc < 0)
161 pfatal_with_name (concat ("temporary file \"", tempname, "\""));
162 close (desc);
164 tem = link (tempname, lockname);
165 (void) unlink (tempname);
166 if (tem >= 0)
167 break;
168 sleep (1);
170 /* If lock file is a minute old, unlock it. */
171 if (stat (lockname, &st) >= 0)
173 now = time (0);
174 if (st.st_ctime < now - 60)
175 (void) unlink (lockname);
179 delete_lockname = lockname;
180 #endif /* not MAIL_USE_FLOCK */
182 #ifdef MAIL_USE_FLOCK
183 indesc = open (inname, O_RDWR);
184 #else /* if not MAIL_USE_FLOCK */
185 indesc = open (inname, O_RDONLY);
186 #endif /* not MAIL_USE_FLOCK */
187 #else /* MAIL_USE_MMDF */
188 indesc = lk_open (inname, O_RDONLY, 0, 0, 10);
189 #endif /* MAIL_USE_MMDF */
191 if (indesc < 0)
192 pfatal_with_name (inname);
194 #if defined(BSD) || defined(XENIX)
195 /* In case movemail is setuid to root, make sure the user can
196 read the output file. */
197 /* This is desirable for all systems
198 but I don't want to assume all have the umask system call */
199 umask (umask (0) & 0333);
200 #endif /* BSD or Xenix */
201 outdesc = open (outname, O_WRONLY | O_CREAT | O_EXCL, 0666);
202 if (outdesc < 0)
203 pfatal_with_name (outname);
204 #ifdef MAIL_USE_FLOCK
205 #ifdef XENIX
206 if (locking (indesc, LK_RLCK, 0L) < 0) pfatal_with_name (inname);
207 #else
208 if (flock (indesc, LOCK_EX) < 0) pfatal_with_name (inname);
209 #endif
210 #endif /* MAIL_USE_FLOCK */
212 while (1)
214 nread = read (indesc, buf, sizeof buf);
215 if (nread != write (outdesc, buf, nread))
217 int saved_errno = errno;
218 (void) unlink (outname);
219 errno = saved_errno;
220 pfatal_with_name (outname);
222 if (nread < sizeof buf)
223 break;
226 #ifdef BSD
227 fsync (outdesc);
228 #endif
230 /* Check to make sure no errors before we zap the inbox. */
231 if (close (outdesc) != 0)
233 int saved_errno = errno;
234 (void) unlink (outname);
235 errno = saved_errno;
236 pfatal_with_name (outname);
239 #ifdef MAIL_USE_FLOCK
240 #if defined(STRIDE) || defined(XENIX)
241 /* Stride, xenix have file locking, but no ftruncate. This mess will do. */
242 (void) close (open (inname, O_CREAT | O_TRUNC | O_RDWR, 0666));
243 #else
244 (void) ftruncate (indesc, 0L);
245 #endif /* STRIDE or XENIX */
246 #endif /* MAIL_USE_FLOCK */
248 #ifdef MAIL_USE_MMDF
249 lk_close (indesc, 0, 0, 0);
250 #else
251 close (indesc);
252 #endif
254 #ifndef MAIL_USE_FLOCK
255 /* Delete the input file; if we can't, at least get rid of its contents. */
256 if (unlink (inname) < 0)
257 if (errno != ENOENT)
258 creat (inname, 0666);
259 #ifndef MAIL_USE_MMDF
260 unlink (lockname);
261 #endif /* not MAIL_USE_MMDF */
262 #endif /* not MAIL_USE_FLOCK */
263 exit (0);
266 /* Print error message and exit. */
268 fatal (s1, s2)
269 char *s1, *s2;
271 if (delete_lockname)
272 unlink (delete_lockname);
273 error (s1, s2);
274 exit (1);
277 /* Print error message. `s1' is printf control string, `s2' is arg for it. */
279 error (s1, s2)
280 char *s1, *s2;
282 printf ("movemail: ");
283 printf (s1, s2);
284 printf ("\n");
287 pfatal_with_name (name)
288 char *name;
290 extern int errno, sys_nerr;
291 extern char *sys_errlist[];
292 char *s;
294 if (errno < sys_nerr)
295 s = concat ("", sys_errlist[errno], " for %s");
296 else
297 s = "cannot open %s";
298 fatal (s, name);
301 /* Return a newly-allocated string whose contents concatenate those of s1, s2, s3. */
303 char *
304 concat (s1, s2, s3)
305 char *s1, *s2, *s3;
307 int len1 = strlen (s1), len2 = strlen (s2), len3 = strlen (s3);
308 char *result = (char *) xmalloc (len1 + len2 + len3 + 1);
310 strcpy (result, s1);
311 strcpy (result + len1, s2);
312 strcpy (result + len1 + len2, s3);
313 *(result + len1 + len2 + len3) = 0;
315 return result;
318 /* Like malloc but get fatal error if memory is exhausted. */
321 xmalloc (size)
322 int size;
324 int result = malloc (size);
325 if (!result)
326 fatal ("virtual memory exhausted", 0);
327 return result;
330 /* This is the guts of the interface to the Post Office Protocol. */
332 #ifdef MAIL_USE_POP
334 #include <sys/socket.h>
335 #include <netinet/in.h>
336 #include <netdb.h>
337 #include <stdio.h>
339 #ifdef USG
340 #include <fcntl.h>
341 /* Cancel substitutions made by config.h for Emacs. */
342 #undef open
343 #undef read
344 #undef write
345 #undef close
346 #endif /* USG */
348 #define NOTOK (-1)
349 #define OK 0
350 #define DONE 1
352 char *progname;
353 FILE *sfi;
354 FILE *sfo;
355 char Errmsg[80];
357 static int debug = 0;
359 popmail(user, outfile)
360 char *user;
361 char *outfile;
363 char *host;
364 int nmsgs, nbytes;
365 char response[128];
366 register int i;
367 int mbfi;
368 FILE *mbf;
369 char *getenv();
370 int mbx_write();
371 char *get_errmsg();
373 host = getenv("MAILHOST");
374 if (host == NULL) {
375 fatal("no MAILHOST defined");
378 if (pop_init(host) == NOTOK) {
379 error(Errmsg);
380 return(1);
383 if (getline(response, sizeof response, sfi) != OK) {
384 error(response);
385 return(1);
388 if (pop_command("USER %s", user) == NOTOK ||
389 pop_command("RPOP %s", user) == NOTOK) {
390 error(Errmsg);
391 pop_command("QUIT");
392 return(1);
395 if (pop_stat(&nmsgs, &nbytes) == NOTOK) {
396 error(Errmsg);
397 pop_command("QUIT");
398 return(1);
401 if (!nmsgs)
403 pop_command("QUIT");
404 return(0);
407 mbfi = open (outfile, O_WRONLY | O_CREAT | O_EXCL, 0666);
408 if (mbfi < 0)
410 pop_command("QUIT");
411 error("Error in open: %s, %s", get_errmsg(), outfile);
412 return(1);
414 fchown(mbfi, getuid(), -1);
416 if ((mbf = fdopen(mbfi, "w")) == NULL)
418 pop_command("QUIT");
419 error("Error in fdopen: %s", get_errmsg());
420 close(mbfi);
421 unlink(outfile);
422 return(1);
425 for (i = 1; i <= nmsgs; i++) {
426 mbx_delimit_begin(mbf);
427 if (pop_retr(i, mbx_write, mbf) != OK) {
428 error(Errmsg);
429 pop_command("QUIT");
430 close(mbfi);
431 return(1);
433 mbx_delimit_end(mbf);
434 fflush(mbf);
437 for (i = 1; i <= nmsgs; i++) {
438 if (pop_command("DELE %d", i) == NOTOK) {
439 error(Errmsg);
440 pop_command("QUIT");
441 close(mbfi);
442 return(1);
446 pop_command("QUIT");
447 close(mbfi);
448 return(0);
451 pop_init(host)
452 char *host;
454 register struct hostent *hp;
455 register struct servent *sp;
456 int lport = IPPORT_RESERVED - 1;
457 struct sockaddr_in sin;
458 register int s;
459 char *get_errmsg();
461 hp = gethostbyname(host);
462 if (hp == NULL) {
463 sprintf(Errmsg, "MAILHOST unknown: %s", host);
464 return(NOTOK);
467 sp = getservbyname("pop", "tcp");
468 if (sp == 0) {
469 strcpy(Errmsg, "tcp/pop: unknown service");
470 return(NOTOK);
473 sin.sin_family = hp->h_addrtype;
474 bcopy(hp->h_addr, (char *)&sin.sin_addr, hp->h_length);
475 sin.sin_port = sp->s_port;
476 s = rresvport(&lport);
477 if (s < 0) {
478 sprintf(Errmsg, "error creating socket: %s", get_errmsg());
479 return(NOTOK);
482 if (connect(s, (char *)&sin, sizeof sin) < 0) {
483 sprintf(Errmsg, "error during connect: %s", get_errmsg());
484 close(s);
485 return(NOTOK);
488 sfi = fdopen(s, "r");
489 sfo = fdopen(s, "w");
490 if (sfi == NULL || sfo == NULL) {
491 sprintf(Errmsg, "error in fdopen: %s", get_errmsg());
492 close(s);
493 return(NOTOK);
496 return(OK);
499 pop_command(fmt, a, b, c, d)
500 char *fmt;
502 char buf[128];
503 char errmsg[64];
505 sprintf(buf, fmt, a, b, c, d);
507 if (debug) fprintf(stderr, "---> %s\n", buf);
508 if (putline(buf, Errmsg, sfo) == NOTOK) return(NOTOK);
510 if (getline(buf, sizeof buf, sfi) != OK) {
511 strcpy(Errmsg, buf);
512 return(NOTOK);
515 if (debug) fprintf(stderr, "<--- %s\n", buf);
516 if (*buf != '+') {
517 strcpy(Errmsg, buf);
518 return(NOTOK);
519 } else {
520 return(OK);
525 pop_stat(nmsgs, nbytes)
526 int *nmsgs, *nbytes;
528 char buf[128];
530 if (debug) fprintf(stderr, "---> STAT\n");
531 if (putline("STAT", Errmsg, sfo) == NOTOK) return(NOTOK);
533 if (getline(buf, sizeof buf, sfi) != OK) {
534 strcpy(Errmsg, buf);
535 return(NOTOK);
538 if (debug) fprintf(stderr, "<--- %s\n", buf);
539 if (*buf != '+') {
540 strcpy(Errmsg, buf);
541 return(NOTOK);
542 } else {
543 sscanf(buf, "+OK %d %d", nmsgs, nbytes);
544 return(OK);
548 pop_retr(msgno, action, arg)
549 int (*action)();
551 char buf[128];
553 sprintf(buf, "RETR %d", msgno);
554 if (debug) fprintf(stderr, "%s\n", buf);
555 if (putline(buf, Errmsg, sfo) == NOTOK) return(NOTOK);
557 if (getline(buf, sizeof buf, sfi) != OK) {
558 strcpy(Errmsg, buf);
559 return(NOTOK);
562 while (1) {
563 switch (multiline(buf, sizeof buf, sfi)) {
564 case OK:
565 (*action)(buf, arg);
566 break;
567 case DONE:
568 return (OK);
569 case NOTOK:
570 strcpy(Errmsg, buf);
571 return (NOTOK);
576 getline(buf, n, f)
577 char *buf;
578 register int n;
579 FILE *f;
581 register char *p;
582 int c;
584 p = buf;
585 while (--n > 0 && (c = fgetc(f)) != EOF)
586 if ((*p++ = c) == '\n') break;
588 if (ferror(f)) {
589 strcpy(buf, "error on connection");
590 return (NOTOK);
593 if (c == EOF && p == buf) {
594 strcpy(buf, "connection closed by foreign host");
595 return (DONE);
598 *p = NULL;
599 if (*--p == '\n') *p = NULL;
600 if (*--p == '\r') *p = NULL;
601 return(OK);
604 multiline(buf, n, f)
605 char *buf;
606 register int n;
607 FILE *f;
609 if (getline(buf, n, f) != OK) return (NOTOK);
610 if (*buf == '.') {
611 if (*(buf+1) == NULL) {
612 return (DONE);
613 } else {
614 strcpy(buf, buf+1);
617 return(OK);
620 char *
621 get_errmsg()
623 extern int errno, sys_nerr;
624 extern char *sys_errlist[];
625 char *s;
627 if (errno < sys_nerr)
628 s = sys_errlist[errno];
629 else
630 s = "unknown error";
631 return(s);
634 putline(buf, err, f)
635 char *buf;
636 char *err;
637 FILE *f;
639 fprintf(f, "%s\r\n", buf);
640 fflush(f);
641 if (ferror(f)) {
642 strcpy(err, "lost connection");
643 return(NOTOK);
645 return(OK);
648 mbx_write(line, mbf)
649 char *line;
650 FILE *mbf;
652 fputs(line, mbf);
653 fputc(0x0a, mbf);
656 mbx_delimit_begin(mbf)
657 FILE *mbf;
659 fputs("\f\n0,unseen,,\n", mbf);
662 mbx_delimit_end(mbf)
663 FILE *mbf;
665 putc('\037', mbf);
668 #endif /* MAIL_USE_POP */