(SETUP_SYNTAX_TABLE_FOR_OBJECT): Treat nil like a buffer.
[bpt/emacs.git] / lib-src / movemail.c
CommitLineData
237e0016
RS
1/* movemail foo bar -- move file foo to file bar,
2 locking file foo the way /bin/mail respects.
7f75d5c6 3 Copyright (C) 1986, 1992, 1993, 1994, 1996 Free Software Foundation, Inc.
237e0016
RS
4
5This file is part of GNU Emacs.
6
93320c23
JA
7GNU Emacs is free software; you can redistribute it and/or modify
8it under the terms of the GNU General Public License as published by
39356651 9the Free Software Foundation; either version 2, or (at your option)
93320c23
JA
10any later version.
11
237e0016 12GNU Emacs is distributed in the hope that it will be useful,
93320c23
JA
13but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15GNU General Public License for more details.
237e0016 16
93320c23
JA
17You should have received a copy of the GNU General Public License
18along with GNU Emacs; see the file COPYING. If not, write to
3b7ad313
EN
19the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20Boston, MA 02111-1307, USA. */
237e0016 21
63cf923d
RS
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.
08d0752f
RS
27
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. */
33
237e0016
RS
34/*
35 * Modified January, 1986 by Michael R. Gretzinger (Project Athena)
36 *
88c40feb 37 * Added POP (Post Office Protocol) service. When compiled -DMAIL_USE_POP
237e0016
RS
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.
42 *
43 * New module: popmail.c
44 * Modified routines:
cfa191ff 45 * main - added code within #ifdef MAIL_USE_POP; added setuid (getuid ())
237e0016
RS
46 * after POP code.
47 * New routines in movemail.c:
48 * get_errmsg - return pointer to system error message
49 *
2e82e3c3
RS
50 * Modified August, 1993 by Jonathan Kamens (OpenVision Technologies)
51 *
52 * Move all of the POP code into a separate file, "pop.c".
53 * Use strerror instead of get_errmsg.
54 *
237e0016
RS
55 */
56
3b9ee819
RS
57#define NO_SHORTNAMES /* Tell config not to load remap.h */
58#include <../src/config.h>
237e0016
RS
59#include <sys/types.h>
60#include <sys/stat.h>
61#include <sys/file.h>
e2f9d9af 62#include <stdio.h>
237e0016 63#include <errno.h>
8ca83cfd 64#include <../src/syswait.h>
fea4325c 65#include <getopt.h>
2e82e3c3
RS
66#ifdef MAIL_USE_POP
67#include "pop.h"
68#endif
237e0016 69
91cf09ac
RS
70#ifdef MSDOS
71#undef access
72#endif /* MSDOS */
73
61946d67
RS
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
80
7f75d5c6
RS
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 */
93
237e0016
RS
94#ifdef USG
95#include <fcntl.h>
96#include <unistd.h>
4ec9a77a
RS
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
237e0016
RS
103#endif /* USG */
104
29beb080
RS
105#ifdef HAVE_UNISTD_H
106#include <unistd.h>
107#endif
108
7f75d5c6 109#if defined (XENIX) || defined (WINDOWSNT)
237e0016
RS
110#include <sys/locking.h>
111#endif
112
63cf923d
RS
113#ifdef MAIL_USE_LOCKF
114#define MAIL_USE_SYSTEM_LOCK
115#endif
116
117#ifdef MAIL_USE_FLOCK
118#define MAIL_USE_SYSTEM_LOCK
119#endif
120
4293ba7f
RS
121#ifdef MAIL_USE_MMDF
122extern int lk_open (), lk_close ();
123#endif
124
a4deff3c
RS
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
132static char *mail_spool_name ();
133#endif
134#endif
135
237e0016
RS
136/* Cancel substitutions made by config.h for Emacs. */
137#undef open
138#undef read
139#undef write
140#undef close
141
e97dd183 142#ifndef errno
237e0016 143extern int errno;
e97dd183 144#endif
e2f9d9af 145char *strerror ();
e2f9d9af
DM
146
147void fatal ();
148void error ();
149void pfatal_with_name ();
150void pfatal_and_delete ();
151char *concat ();
2583d6d7 152long *xmalloc ();
e2f9d9af
DM
153int popmail ();
154int pop_retr ();
155int mbx_write ();
156int mbx_delimit_begin ();
157int mbx_delimit_end ();
237e0016
RS
158
159/* Nonzero means this is name of a lock file to delete on fatal error. */
160char *delete_lockname;
161
e2f9d9af 162int
237e0016
RS
163main (argc, argv)
164 int argc;
165 char **argv;
166{
167 char *inname, *outname;
168 int indesc, outdesc;
237e0016 169 int nread;
8ca83cfd 170 WAITTYPE status;
fea4325c 171 int c, preserve_mail = 0;
237e0016 172
63cf923d 173#ifndef MAIL_USE_SYSTEM_LOCK
237e0016
RS
174 struct stat st;
175 long now;
176 int tem;
177 char *lockname, *p;
906ad89d 178 char *tempname;
237e0016 179 int desc;
63cf923d 180#endif /* not MAIL_USE_SYSTEM_LOCK */
237e0016 181
a4deff3c
RS
182#ifdef MAIL_USE_MAILLOCK
183 char *spool_name;
184#endif
185
237e0016
RS
186 delete_lockname = 0;
187
fea4325c 188 while ((c = getopt (argc, argv, "p")) != EOF)
e2f9d9af 189 {
fea4325c
RS
190 switch (c) {
191 case 'p':
192 preserve_mail++;
193 break;
194 default:
195 exit(1);
196 }
197 }
198
199 if (
200#ifdef MAIL_USE_POP
201 (argc - optind < 2) || (argc - optind > 3)
202#else
203 (argc - optind != 2)
204#endif
205 )
206 {
207 fprintf (stderr, "Usage: movemail [-p] inbox destfile%s\n",
208#ifdef MAIL_USE_POP
209 " [POP-password]"
210#else
211 ""
212#endif
213 );
a4deff3c 214 exit (1);
e2f9d9af 215 }
237e0016 216
fea4325c
RS
217 inname = argv[optind];
218 outname = argv[optind+1];
237e0016 219
4293ba7f
RS
220#ifdef MAIL_USE_MMDF
221 mmdf_init (argv[0]);
222#endif
223
af7bd34e
RS
224 if (*outname == 0)
225 fatal ("Destination file name is empty", 0);
226
b1ce62a8 227 /* Check access to output file. */
237e0016
RS
228 if (access (outname, F_OK) == 0 && access (outname, W_OK) != 0)
229 pfatal_with_name (outname);
230
8e6208c5 231 /* Also check that outname's directory is writable to the real uid. */
237e0016 232 {
91cf09ac 233 char *buf = (char *) xmalloc (strlen (outname) + 1);
e2f9d9af 234 char *p;
237e0016
RS
235 strcpy (buf, outname);
236 p = buf + strlen (buf);
7f75d5c6 237 while (p > buf && !IS_DIRECTORY_SEP (p[-1]))
237e0016
RS
238 *--p = 0;
239 if (p == buf)
240 *p++ = '.';
241 if (access (buf, W_OK) != 0)
242 pfatal_with_name (buf);
243 free (buf);
244 }
245
246#ifdef MAIL_USE_POP
12a0565a 247 if (!strncmp (inname, "po:", 3))
237e0016 248 {
b3112191 249 int status;
237e0016 250
fea4325c
RS
251 status = popmail (inname + 3, outname, preserve_mail,
252 (argc - optind == 3) ? argv[optind+2] : NULL);
237e0016
RS
253 exit (status);
254 }
255
cfa191ff 256 setuid (getuid ());
237e0016
RS
257#endif /* MAIL_USE_POP */
258
7f75d5c6
RS
259#ifndef DISABLE_DIRECT_ACCESS
260
b1ce62a8
RS
261 /* Check access to input file. */
262 if (access (inname, R_OK | W_OK) != 0)
263 pfatal_with_name (inname);
264
4293ba7f 265#ifndef MAIL_USE_MMDF
63cf923d 266#ifndef MAIL_USE_SYSTEM_LOCK
a4deff3c
RS
267#ifdef MAIL_USE_MAILLOCK
268 spool_name = mail_spool_name (inname);
269 if (! spool_name)
270#endif
237e0016 271 {
a4deff3c
RS
272 /* Use a lock file named after our first argument with .lock appended:
273 If it exists, the mail file is locked. */
274 /* Note: this locking mechanism is *required* by the mailer
275 (on systems which use it) to prevent loss of mail.
276
277 On systems that use a lock file, extracting the mail without locking
278 WILL occasionally cause loss of mail due to timing errors!
279
280 So, if creation of the lock file fails
281 due to access permission on the mail spool directory,
282 you simply MUST change the permission
283 and/or make movemail a setgid program
284 so it can create lock files properly.
285
286 You might also wish to verify that your system is one
287 which uses lock files for this purpose. Some systems use other methods.
288
289 If your system uses the `flock' system call for mail locking,
290 define MAIL_USE_SYSTEM_LOCK in config.h or the s-*.h file
291 and recompile movemail. If the s- file for your system
292 should define MAIL_USE_SYSTEM_LOCK but does not, send a bug report
293 to bug-gnu-emacs@prep.ai.mit.edu so we can fix it. */
294
295 lockname = concat (inname, ".lock", "");
296 tempname = (char *) xmalloc (strlen (inname) + strlen ("EXXXXXX") + 1);
297 strcpy (tempname, inname);
298 p = tempname + strlen (tempname);
299 while (p != tempname && !IS_DIRECTORY_SEP (p[-1]))
300 p--;
301 *p = 0;
302 strcpy (p, "EXXXXXX");
303 mktemp (tempname);
cfa191ff 304 unlink (tempname);
237e0016 305
a4deff3c 306 while (1)
237e0016 307 {
a4deff3c
RS
308 /* Create the lock file, but not under the lock file name. */
309 /* Give up if cannot do that. */
310 desc = open (tempname, O_WRONLY | O_CREAT | O_EXCL, 0666);
311 if (desc < 0)
312 {
313 char *message = (char *) xmalloc (strlen (tempname) + 50);
314 sprintf (message, "%s--see source file lib-src/movemail.c",
315 tempname);
316 pfatal_with_name (message);
317 }
318 close (desc);
319
320 tem = link (tempname, lockname);
321 unlink (tempname);
322 if (tem >= 0)
323 break;
324 sleep (1);
325
326 /* If lock file is five minutes old, unlock it.
327 Five minutes should be good enough to cope with crashes
328 and wedgitude, and long enough to avoid being fooled
329 by time differences between machines. */
330 if (stat (lockname, &st) >= 0)
331 {
332 now = time (0);
333 if (st.st_ctime < now - 300)
334 unlink (lockname);
335 }
237e0016 336 }
237e0016 337
a4deff3c
RS
338 delete_lockname = lockname;
339 }
63cf923d
RS
340#endif /* not MAIL_USE_SYSTEM_LOCK */
341#endif /* not MAIL_USE_MMDF */
237e0016 342
8ca83cfd
RS
343 if (fork () == 0)
344 {
25025815 345 int lockcount = 0;
a4deff3c
RS
346 int status = 0;
347#if defined (MAIL_USE_MAILLOCK) && defined (HAVE_TOUCHLOCK)
348 long touched_lock, now;
349#endif
25025815 350
d5216c20 351 setuid (getuid ());
8ca83cfd 352
63cf923d
RS
353#ifndef MAIL_USE_MMDF
354#ifdef MAIL_USE_SYSTEM_LOCK
8ca83cfd 355 indesc = open (inname, O_RDWR);
63cf923d 356#else /* if not MAIL_USE_SYSTEM_LOCK */
8ca83cfd 357 indesc = open (inname, O_RDONLY);
63cf923d 358#endif /* not MAIL_USE_SYSTEM_LOCK */
8ca83cfd
RS
359#else /* MAIL_USE_MMDF */
360 indesc = lk_open (inname, O_RDONLY, 0, 0, 10);
4293ba7f
RS
361#endif /* MAIL_USE_MMDF */
362
8ca83cfd
RS
363 if (indesc < 0)
364 pfatal_with_name (inname);
237e0016 365
e397a017 366#if defined (BSD_SYSTEM) || defined (XENIX)
8ca83cfd
RS
367 /* In case movemail is setuid to root, make sure the user can
368 read the output file. */
369 /* This is desirable for all systems
370 but I don't want to assume all have the umask system call */
371 umask (umask (0) & 0333);
e397a017 372#endif /* BSD_SYSTEM || XENIX */
8ca83cfd
RS
373 outdesc = open (outname, O_WRONLY | O_CREAT | O_EXCL, 0666);
374 if (outdesc < 0)
375 pfatal_with_name (outname);
25025815
RS
376
377 /* This label exists so we can retry locking
378 after a delay, if it got EAGAIN or EBUSY. */
379 retry_lock:
380
381 /* Try to lock it. */
a4deff3c
RS
382#ifdef MAIL_USE_MAILLOCK
383 if (spool_name)
384 {
385 /* The "0 - " is to make it a negative number if maillock returns
386 non-zero. */
387 status = 0 - maillock (spool_name, 1);
388#ifdef HAVE_TOUCHLOCK
389 touched_lock = time (0);
390#endif
391 lockcount = 5;
392 }
393 else
394#endif /* MAIL_USE_MAILLOCK */
395 {
63cf923d
RS
396#ifdef MAIL_USE_SYSTEM_LOCK
397#ifdef MAIL_USE_LOCKF
a4deff3c 398 status = lockf (indesc, F_LOCK, 0);
63cf923d 399#else /* not MAIL_USE_LOCKF */
237e0016 400#ifdef XENIX
a4deff3c 401 status = locking (indesc, LK_RLCK, 0L);
7f75d5c6
RS
402#else
403#ifdef WINDOWSNT
a4deff3c 404 status = locking (indesc, LK_RLCK, -1L);
237e0016 405#else
a4deff3c 406 status = flock (indesc, LOCK_EX);
237e0016 407#endif
7f75d5c6 408#endif
63cf923d
RS
409#endif /* not MAIL_USE_LOCKF */
410#endif /* MAIL_USE_SYSTEM_LOCK */
a4deff3c 411 }
237e0016 412
25025815
RS
413 /* If it fails, retry up to 5 times
414 for certain failure codes. */
415 if (status < 0)
416 {
417 if (++lockcount <= 5)
418 {
419#ifdef EAGAIN
420 if (errno == EAGAIN)
421 {
422 sleep (1);
423 goto retry_lock;
424 }
425#endif
426#ifdef EBUSY
427 if (errno == EBUSY)
428 {
429 sleep (1);
430 goto retry_lock;
431 }
432#endif
433 }
434
435 pfatal_with_name (inname);
436 }
437
08564963 438 {
8ca83cfd
RS
439 char buf[1024];
440
441 while (1)
08564963 442 {
8ca83cfd
RS
443 nread = read (indesc, buf, sizeof buf);
444 if (nread != write (outdesc, buf, nread))
445 {
446 int saved_errno = errno;
447 unlink (outname);
448 errno = saved_errno;
449 pfatal_with_name (outname);
450 }
451 if (nread < sizeof buf)
452 break;
a4deff3c
RS
453#if defined (MAIL_USE_MAILLOCK) && defined (HAVE_TOUCHLOCK)
454 if (spool_name)
455 {
456 now = time (0);
457 if (now - touched_lock > 60)
458 {
459 touchlock ();
460 touched_lock = now;
461 }
462 }
463#endif /* MAIL_USE_MAILLOCK */
08564963 464 }
08564963 465 }
237e0016 466
e397a017 467#ifdef BSD_SYSTEM
8ca83cfd
RS
468 if (fsync (outdesc) < 0)
469 pfatal_and_delete (outname);
237e0016
RS
470#endif
471
8ca83cfd
RS
472 /* Check to make sure no errors before we zap the inbox. */
473 if (close (outdesc) != 0)
474 pfatal_and_delete (outname);
237e0016 475
63cf923d 476#ifdef MAIL_USE_SYSTEM_LOCK
fea4325c
RS
477 if (! preserve_mail)
478 {
7f75d5c6 479#if defined (STRIDE) || defined (XENIX) || defined (WINDOWSNT)
fea4325c
RS
480 /* Stride, xenix have file locking, but no ftruncate.
481 This mess will do. */
482 close (open (inname, O_CREAT | O_TRUNC | O_RDWR, 0666));
237e0016 483#else
fea4325c
RS
484 ftruncate (indesc, 0L);
485 }
237e0016 486#endif /* STRIDE or XENIX */
63cf923d 487#endif /* MAIL_USE_SYSTEM_LOCK */
4293ba7f
RS
488
489#ifdef MAIL_USE_MMDF
8ca83cfd 490 lk_close (indesc, 0, 0, 0);
4293ba7f 491#else
8ca83cfd 492 close (indesc);
4293ba7f 493#endif
237e0016 494
63cf923d 495#ifndef MAIL_USE_SYSTEM_LOCK
fea4325c
RS
496 if (! preserve_mail)
497 {
498 /* Delete the input file; if we can't, at least get rid of its
499 contents. */
e97dd183 500#ifdef MAIL_UNLINK_SPOOL
fea4325c
RS
501 /* This is generally bad to do, because it destroys the permissions
502 that were set on the file. Better to just empty the file. */
503 if (unlink (inname) < 0 && errno != ENOENT)
e97dd183 504#endif /* MAIL_UNLINK_SPOOL */
fea4325c
RS
505 creat (inname, 0600);
506 }
63cf923d 507#endif /* not MAIL_USE_SYSTEM_LOCK */
8ca83cfd 508
a4deff3c
RS
509#ifdef MAIL_USE_MAILLOCK
510 /* This has to occur in the child, i.e., in the process that
511 acquired the lock! */
512 if (spool_name)
513 mailunlock ();
514#endif
8ca83cfd
RS
515 exit (0);
516 }
517
518 wait (&status);
519 if (!WIFEXITED (status))
520 exit (1);
521 else if (WRETCODE (status) != 0)
522 exit (WRETCODE (status));
523
63cf923d 524#if !defined (MAIL_USE_MMDF) && !defined (MAIL_USE_SYSTEM_LOCK)
a4deff3c
RS
525#ifdef MAIL_USE_MAILLOCK
526 if (! spool_name)
527#endif /* MAIL_USE_MAILLOCK */
528 unlink (lockname);
63cf923d 529#endif /* not MAIL_USE_MMDF and not MAIL_USE_SYSTEM_LOCK */
7f75d5c6
RS
530
531#endif /* ! DISABLE_DIRECT_ACCESS */
532
e2f9d9af 533 return 0;
237e0016 534}
a4deff3c
RS
535
536#ifdef MAIL_USE_MAILLOCK
537/* This function uses stat to confirm that the mail directory is
538 identical to the directory of the input file, rather than just
539 string-comparing the two paths, because one or both of them might
540 be symbolic links pointing to some other directory. */
541static char *
542mail_spool_name (inname)
543 char *inname;
544{
545 struct stat stat1, stat2;
546 char *indir, *fname;
547 int status;
548
549 if (! (fname = rindex (inname, '/')))
550 return NULL;
551
552 fname++;
553
554 if (stat (MAILDIR, &stat1) < 0)
555 return NULL;
556
557 indir = (char *) xmalloc (fname - inname + 1);
558 strncpy (indir, inname, fname - inname);
559 indir[fname-inname] = '\0';
560
561
562 status = stat (indir, &stat2);
563
564 free (indir);
565
566 if (status < 0)
567 return NULL;
568
569 if ((stat1.st_dev == stat2.st_dev) &&
570 (stat1.st_ino == stat2.st_ino))
571 return fname;
572
573 return NULL;
574}
575#endif /* MAIL_USE_MAILLOCK */
237e0016
RS
576\f
577/* Print error message and exit. */
578
e2f9d9af 579void
237e0016
RS
580fatal (s1, s2)
581 char *s1, *s2;
582{
583 if (delete_lockname)
584 unlink (delete_lockname);
585 error (s1, s2);
586 exit (1);
587}
588
589/* Print error message. `s1' is printf control string, `s2' is arg for it. */
590
e2f9d9af 591void
b1ce62a8
RS
592error (s1, s2, s3)
593 char *s1, *s2, *s3;
237e0016 594{
e2f9d9af
DM
595 fprintf (stderr, "movemail: ");
596 fprintf (stderr, s1, s2, s3);
597 fprintf (stderr, "\n");
237e0016
RS
598}
599
e2f9d9af 600void
237e0016
RS
601pfatal_with_name (name)
602 char *name;
603{
e2f9d9af 604 char *s = concat ("", strerror (errno), " for %s");
237e0016
RS
605 fatal (s, name);
606}
607
e2f9d9af 608void
cfa191ff
RS
609pfatal_and_delete (name)
610 char *name;
611{
e2f9d9af 612 char *s = concat ("", strerror (errno), " for %s");
cfa191ff
RS
613 unlink (name);
614 fatal (s, name);
615}
616
237e0016
RS
617/* Return a newly-allocated string whose contents concatenate those of s1, s2, s3. */
618
619char *
620concat (s1, s2, s3)
621 char *s1, *s2, *s3;
622{
623 int len1 = strlen (s1), len2 = strlen (s2), len3 = strlen (s3);
624 char *result = (char *) xmalloc (len1 + len2 + len3 + 1);
625
626 strcpy (result, s1);
627 strcpy (result + len1, s2);
628 strcpy (result + len1 + len2, s3);
629 *(result + len1 + len2 + len3) = 0;
630
631 return result;
632}
633
634/* Like malloc but get fatal error if memory is exhausted. */
635
2583d6d7 636long *
237e0016 637xmalloc (size)
e97dd183 638 unsigned size;
237e0016 639{
2583d6d7 640 long *result = (long *) malloc (size);
237e0016
RS
641 if (!result)
642 fatal ("virtual memory exhausted", 0);
643 return result;
644}
645\f
646/* This is the guts of the interface to the Post Office Protocol. */
647
648#ifdef MAIL_USE_POP
649
7f75d5c6 650#ifndef WINDOWSNT
237e0016
RS
651#include <sys/socket.h>
652#include <netinet/in.h>
653#include <netdb.h>
7f75d5c6
RS
654#else
655#undef _WINSOCKAPI_
656#include <winsock.h>
657#endif
cecf0f21 658#include <pwd.h>
237e0016 659
237e0016
RS
660#define NOTOK (-1)
661#define OK 0
662#define DONE 1
663
664char *progname;
665FILE *sfi;
666FILE *sfo;
2e82e3c3
RS
667char ibuffer[BUFSIZ];
668char obuffer[BUFSIZ];
237e0016
RS
669char Errmsg[80];
670
fea4325c 671popmail (user, outfile, preserve, password)
b1ce62a8
RS
672 char *user;
673 char *outfile;
fea4325c 674 int preserve;
7f75d5c6 675 char *password;
237e0016 676{
b1ce62a8 677 int nmsgs, nbytes;
b1ce62a8
RS
678 register int i;
679 int mbfi;
680 FILE *mbf;
2e82e3c3 681 char *getenv ();
b32701a7 682 popserver server;
237e0016 683
7f75d5c6 684 server = pop_open (0, user, password, POP_NO_GETPASS);
2e82e3c3 685 if (! server)
b1ce62a8 686 {
2e82e3c3
RS
687 error (pop_error);
688 return (1);
237e0016
RS
689 }
690
2e82e3c3 691 if (pop_stat (server, &nmsgs, &nbytes))
b1ce62a8 692 {
2e82e3c3
RS
693 error (pop_error);
694 return (1);
237e0016
RS
695 }
696
b1ce62a8
RS
697 if (!nmsgs)
698 {
2e82e3c3
RS
699 pop_close (server);
700 return (0);
b1ce62a8
RS
701 }
702
703 mbfi = open (outfile, O_WRONLY | O_CREAT | O_EXCL, 0666);
704 if (mbfi < 0)
705 {
2e82e3c3
RS
706 pop_close (server);
707 error ("Error in open: %s, %s", strerror (errno), outfile);
708 return (1);
b1ce62a8
RS
709 }
710 fchown (mbfi, getuid (), -1);
711
7f75d5c6 712 if ((mbf = fdopen (mbfi, "wb")) == NULL)
b1ce62a8 713 {
2e82e3c3
RS
714 pop_close (server);
715 error ("Error in fdopen: %s", strerror (errno));
716 close (mbfi);
717 unlink (outfile);
718 return (1);
b1ce62a8
RS
719 }
720
721 for (i = 1; i <= nmsgs; i++)
722 {
723 mbx_delimit_begin (mbf);
ff804ff5 724 if (pop_retr (server, i, mbf) != OK)
b1ce62a8 725 {
2e82e3c3 726 error (Errmsg);
b1ce62a8 727 close (mbfi);
2e82e3c3 728 return (1);
237e0016 729 }
b1ce62a8
RS
730 mbx_delimit_end (mbf);
731 fflush (mbf);
2e82e3c3
RS
732 if (ferror (mbf))
733 {
734 error ("Error in fflush: %s", strerror (errno));
735 pop_close (server);
736 close (mbfi);
737 return (1);
738 }
237e0016
RS
739 }
740
2e82e3c3
RS
741 /* On AFS, a call to write only modifies the file in the local
742 * workstation's AFS cache. The changes are not written to the server
743 * until a call to fsync or close is made. Users with AFS home
744 * directories have lost mail when over quota because these checks were
745 * not made in previous versions of movemail. */
746
e397a017 747#ifdef BSD_SYSTEM
cfa191ff
RS
748 if (fsync (mbfi) < 0)
749 {
2e82e3c3
RS
750 error ("Error in fsync: %s", strerror (errno));
751 return (1);
cfa191ff 752 }
340ff9de 753#endif
cfa191ff
RS
754
755 if (close (mbfi) == -1)
756 {
2e82e3c3
RS
757 error ("Error in close: %s", strerror (errno));
758 return (1);
cfa191ff
RS
759 }
760
fea4325c
RS
761 if (! preserve)
762 for (i = 1; i <= nmsgs; i++)
763 {
764 if (pop_delete (server, i))
765 {
766 error (pop_error);
767 pop_close (server);
768 return (1);
769 }
770 }
237e0016 771
2e82e3c3 772 if (pop_quit (server))
b1ce62a8 773 {
2e82e3c3
RS
774 error (pop_error);
775 return (1);
237e0016 776 }
237e0016 777
2e82e3c3 778 return (0);
237e0016
RS
779}
780
ff804ff5
KH
781int
782pop_retr (server, msgno, arg)
b32701a7 783 popserver server;
ff804ff5 784 FILE *arg;
237e0016 785{
2e82e3c3
RS
786 extern char *strerror ();
787 char *line;
788 int ret;
237e0016 789
2e82e3c3 790 if (pop_retrieve_first (server, msgno, &line))
b1ce62a8 791 {
2e82e3c3
RS
792 strncpy (Errmsg, pop_error, sizeof (Errmsg));
793 Errmsg[sizeof (Errmsg)-1] = '\0';
794 return (NOTOK);
237e0016
RS
795 }
796
2e82e3c3 797 while (! (ret = pop_retrieve_next (server, &line)))
b1ce62a8 798 {
2e82e3c3
RS
799 if (! line)
800 break;
801
ff804ff5 802 if (mbx_write (line, arg) != OK)
b1ce62a8 803 {
2e82e3c3
RS
804 strcpy (Errmsg, strerror (errno));
805 pop_close (server);
806 return (NOTOK);
237e0016
RS
807 }
808 }
237e0016 809
2e82e3c3 810 if (ret)
b1ce62a8 811 {
2e82e3c3
RS
812 strncpy (Errmsg, pop_error, sizeof (Errmsg));
813 Errmsg[sizeof (Errmsg)-1] = '\0';
814 return (NOTOK);
237e0016
RS
815 }
816
2e82e3c3 817 return (OK);
237e0016
RS
818}
819
2e82e3c3
RS
820/* Do this as a macro instead of using strcmp to save on execution time. */
821#define IS_FROM_LINE(a) ((a[0] == 'F') \
822 && (a[1] == 'r') \
823 && (a[2] == 'o') \
824 && (a[3] == 'm') \
825 && (a[4] == ' '))
237e0016 826
2e82e3c3 827int
b1ce62a8
RS
828mbx_write (line, mbf)
829 char *line;
830 FILE *mbf;
237e0016 831{
2e82e3c3
RS
832 if (IS_FROM_LINE (line))
833 {
834 if (fputc ('>', mbf) == EOF)
835 return (NOTOK);
836 }
837 if (fputs (line, mbf) == EOF)
838 return (NOTOK);
839 if (fputc (0x0a, mbf) == EOF)
840 return (NOTOK);
841 return (OK);
237e0016
RS
842}
843
2e82e3c3 844int
b1ce62a8
RS
845mbx_delimit_begin (mbf)
846 FILE *mbf;
237e0016 847{
2e82e3c3
RS
848 if (fputs ("\f\n0, unseen,,\n", mbf) == EOF)
849 return (NOTOK);
850 return (OK);
237e0016
RS
851}
852
b1ce62a8
RS
853mbx_delimit_end (mbf)
854 FILE *mbf;
237e0016 855{
2e82e3c3
RS
856 if (putc ('\037', mbf) == EOF)
857 return (NOTOK);
858 return (OK);
237e0016
RS
859}
860
861#endif /* MAIL_USE_POP */
e5f7ea68
RM
862\f
863#ifndef HAVE_STRERROR
864char *
865strerror (errnum)
866 int errnum;
867{
868 extern char *sys_errlist[];
869 extern int sys_nerr;
870
871 if (errnum >= 0 && errnum < sys_nerr)
872 return sys_errlist[errnum];
873 return (char *) "Unknown error";
874}
875
876#endif /* ! HAVE_STRERROR */