Convert consecutive FSF copyright years to ranges.
[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.
73b0cd50 3 Copyright (C) 1986, 1992-1994, 1996, 1999, 2001-2011 Free Software Foundation, Inc.
237e0016
RS
4
5This file is part of GNU Emacs.
6
294981c7 7GNU Emacs is free software: you can redistribute it and/or modify
93320c23 8it under the terms of the GNU General Public License as published by
294981c7
GM
9the Free Software Foundation, either version 3 of the License, or
10(at your option) any later version.
93320c23 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 17You should have received a copy of the GNU General Public License
294981c7
GM
18along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
19
237e0016 20
63cf923d
RS
21/* Important notice: defining MAIL_USE_FLOCK or MAIL_USE_LOCKF *will
22 cause loss of mail* if you do it on a system that does not normally
23 use flock as its way of interlocking access to inbox files. The
24 setting of MAIL_USE_FLOCK and MAIL_USE_LOCKF *must agree* with the
25 system's own conventions. It is not a choice that is up to you.
08d0752f
RS
26
27 So, if your system uses lock files rather than flock, then the only way
28 you can get proper operation is to enable movemail to write lockfiles there.
29 This means you must either give that directory access modes
30 that permit everyone to write lockfiles in it, or you must make movemail
31 a setuid or setgid program. */
32
237e0016
RS
33/*
34 * Modified January, 1986 by Michael R. Gretzinger (Project Athena)
35 *
88c40feb 36 * Added POP (Post Office Protocol) service. When compiled -DMAIL_USE_POP
237e0016
RS
37 * movemail will accept input filename arguments of the form
38 * "po:username". This will cause movemail to open a connection to
39 * a pop server running on $MAILHOST (environment variable). Movemail
40 * must be setuid to root in order to work with POP.
177c0ea7 41 *
237e0016
RS
42 * New module: popmail.c
43 * Modified routines:
cfa191ff 44 * main - added code within #ifdef MAIL_USE_POP; added setuid (getuid ())
177c0ea7 45 * after POP code.
237e0016
RS
46 * New routines in movemail.c:
47 * get_errmsg - return pointer to system error message
48 *
2e82e3c3
RS
49 * Modified August, 1993 by Jonathan Kamens (OpenVision Technologies)
50 *
51 * Move all of the POP code into a separate file, "pop.c".
52 * Use strerror instead of get_errmsg.
53 *
237e0016
RS
54 */
55
752fb472 56#include <config.h>
237e0016
RS
57#include <sys/types.h>
58#include <sys/stat.h>
59#include <sys/file.h>
e2f9d9af 60#include <stdio.h>
237e0016 61#include <errno.h>
bd41a17d 62#include <time.h>
cc3b64e8 63
fea4325c 64#include <getopt.h>
f72adc12 65#include <unistd.h>
cc3b64e8
DL
66#ifdef HAVE_FCNTL_H
67#include <fcntl.h>
68#endif
1725ae55
AS
69#ifdef HAVE_STRING_H
70#include <string.h>
71#endif
f72adc12 72#include "syswait.h"
2e82e3c3
RS
73#ifdef MAIL_USE_POP
74#include "pop.h"
75#endif
237e0016 76
91cf09ac
RS
77#ifdef MSDOS
78#undef access
79#endif /* MSDOS */
80
61946d67
RS
81#ifndef DIRECTORY_SEP
82#define DIRECTORY_SEP '/'
83#endif
84#ifndef IS_DIRECTORY_SEP
85#define IS_DIRECTORY_SEP(_c_) ((_c_) == DIRECTORY_SEP)
86#endif
87
7f75d5c6 88#ifdef WINDOWSNT
677a7bcf 89#include "ntlib.h"
7f75d5c6
RS
90#undef access
91#undef unlink
92#define fork() 0
4822b2e5 93#define wait(var) (*(var) = 0)
7f75d5c6
RS
94/* Unfortunately, Samba doesn't seem to properly lock Unix files even
95 though the locking call succeeds (and indeed blocks local access from
96 other NT programs). If you have direct file access using an NFS
97 client or something other than Samba, the locking call might work
677a7bcf
RS
98 properly - make sure it does before you enable this!
99
100 [18-Feb-97 andrewi] I now believe my comment above to be incorrect,
101 since it was based on a misunderstanding of how locking calls are
102 implemented and used on Unix. */
103//#define DISABLE_DIRECT_ACCESS
104
677a7bcf 105#include <fcntl.h>
7f75d5c6
RS
106#endif /* WINDOWSNT */
107
4ec9a77a
RS
108#ifndef F_OK
109#define F_OK 0
110#define X_OK 1
111#define W_OK 2
112#define R_OK 4
113#endif
237e0016 114
76ed5e01 115#ifdef WINDOWSNT
237e0016
RS
116#include <sys/locking.h>
117#endif
118
63cf923d
RS
119#ifdef MAIL_USE_LOCKF
120#define MAIL_USE_SYSTEM_LOCK
121#endif
122
123#ifdef MAIL_USE_FLOCK
124#define MAIL_USE_SYSTEM_LOCK
125#endif
126
4293ba7f
RS
127#ifdef MAIL_USE_MMDF
128extern int lk_open (), lk_close ();
129#endif
130
a4deff3c 131#if !defined (MAIL_USE_SYSTEM_LOCK) && !defined (MAIL_USE_MMDF) && \
dd843b6a
DL
132 (defined (HAVE_LIBMAIL) || defined (HAVE_LIBLOCKFILE)) && \
133 defined (HAVE_MAILLOCK_H)
a4deff3c
RS
134#include <maillock.h>
135/* We can't use maillock unless we know what directory system mail
136 files appear in. */
137#ifdef MAILDIR
138#define MAIL_USE_MAILLOCK
139static char *mail_spool_name ();
140#endif
141#endif
142
1725ae55 143#ifndef HAVE_STRERROR
873fbd0b 144char *strerror (int);
1725ae55 145#endif
e2f9d9af 146
988e88ab
J
147static void fatal (const char *s1, const char *s2, const char *s3) NO_RETURN;
148static void error (const char *s1, const char *s2, const char *s3);
68441b90
DN
149static void pfatal_with_name (char *name) NO_RETURN;
150static void pfatal_and_delete (char *name) NO_RETURN;
988e88ab 151static char *concat (const char *s1, const char *s2, const char *s3);
1725ae55 152static long *xmalloc (unsigned int size);
e2ad23ef 153#ifdef MAIL_USE_POP
1725ae55
AS
154static int popmail (char *mailbox, char *outfile, int preserve, char *password, int reverse_order);
155static int pop_retr (popserver server, int msgno, FILE *arg);
156static int mbx_write (char *line, int len, FILE *mbf);
157static int mbx_delimit_begin (FILE *mbf);
158static int mbx_delimit_end (FILE *mbf);
e2ad23ef 159#endif
237e0016
RS
160
161/* Nonzero means this is name of a lock file to delete on fatal error. */
162char *delete_lockname;
163
e2f9d9af 164int
873fbd0b 165main (int argc, char **argv)
237e0016
RS
166{
167 char *inname, *outname;
168 int indesc, outdesc;
728a982d 169 ssize_t nread;
bba104c1 170 int 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
a2997b0f
KH
186#ifdef MAIL_USE_POP
187 int pop_reverse_order = 0;
188# define ARGSTR "pr"
189#else /* ! MAIL_USE_POP */
190# define ARGSTR "p"
191#endif /* MAIL_USE_POP */
192
51a91f9d
CY
193 uid_t real_gid = getgid();
194 uid_t priv_gid = getegid();
195
9112a2a9
AI
196#ifdef WINDOWSNT
197 /* Ensure all file i/o is in binary mode. */
198 _fmode = _O_BINARY;
199#endif
200
237e0016
RS
201 delete_lockname = 0;
202
a2997b0f 203 while ((c = getopt (argc, argv, ARGSTR)) != EOF)
e2f9d9af 204 {
fea4325c 205 switch (c) {
a2997b0f
KH
206#ifdef MAIL_USE_POP
207 case 'r':
208 pop_reverse_order = 1;
209 break;
210#endif
fea4325c
RS
211 case 'p':
212 preserve_mail++;
213 break;
214 default:
65396510 215 exit (EXIT_FAILURE);
fea4325c
RS
216 }
217 }
218
219 if (
220#ifdef MAIL_USE_POP
221 (argc - optind < 2) || (argc - optind > 3)
222#else
223 (argc - optind != 2)
224#endif
225 )
226 {
fea4325c 227#ifdef MAIL_USE_POP
f213f2c0 228 fprintf (stderr, "Usage: movemail [-p] [-r] inbox destfile%s\n",
bb5618fe 229 " [POP-password]");
fea4325c 230#else
bb5618fe 231 fprintf (stderr, "Usage: movemail [-p] inbox destfile%s\n", "");
fea4325c 232#endif
65396510 233 exit (EXIT_FAILURE);
e2f9d9af 234 }
237e0016 235
fea4325c
RS
236 inname = argv[optind];
237 outname = argv[optind+1];
237e0016 238
4293ba7f
RS
239#ifdef MAIL_USE_MMDF
240 mmdf_init (argv[0]);
241#endif
242
af7bd34e 243 if (*outname == 0)
a9eedf40 244 fatal ("Destination file name is empty", 0, 0);
af7bd34e 245
237e0016 246#ifdef MAIL_USE_POP
12a0565a 247 if (!strncmp (inname, "po:", 3))
237e0016 248 {
b3112191 249 int status;
237e0016 250
fea4325c 251 status = popmail (inname + 3, outname, preserve_mail,
a2997b0f
KH
252 (argc - optind == 3) ? argv[optind+2] : NULL,
253 pop_reverse_order);
237e0016
RS
254 exit (status);
255 }
256
51a91f9d
CY
257 if (setuid (getuid ()) < 0)
258 fatal ("Failed to drop privileges", 0, 0);
259
237e0016
RS
260#endif /* MAIL_USE_POP */
261
7f75d5c6 262#ifndef DISABLE_DIRECT_ACCESS
4293ba7f 263#ifndef MAIL_USE_MMDF
63cf923d 264#ifndef MAIL_USE_SYSTEM_LOCK
a4deff3c
RS
265#ifdef MAIL_USE_MAILLOCK
266 spool_name = mail_spool_name (inname);
267 if (! spool_name)
268#endif
237e0016 269 {
a4deff3c
RS
270 /* Use a lock file named after our first argument with .lock appended:
271 If it exists, the mail file is locked. */
272 /* Note: this locking mechanism is *required* by the mailer
273 (on systems which use it) to prevent loss of mail.
274
275 On systems that use a lock file, extracting the mail without locking
276 WILL occasionally cause loss of mail due to timing errors!
277
278 So, if creation of the lock file fails
279 due to access permission on the mail spool directory,
280 you simply MUST change the permission
281 and/or make movemail a setgid program
282 so it can create lock files properly.
283
284 You might also wish to verify that your system is one
285 which uses lock files for this purpose. Some systems use other methods.
286
287 If your system uses the `flock' system call for mail locking,
288 define MAIL_USE_SYSTEM_LOCK in config.h or the s-*.h file
289 and recompile movemail. If the s- file for your system
290 should define MAIL_USE_SYSTEM_LOCK but does not, send a bug report
291 to bug-gnu-emacs@prep.ai.mit.edu so we can fix it. */
292
293 lockname = concat (inname, ".lock", "");
294 tempname = (char *) xmalloc (strlen (inname) + strlen ("EXXXXXX") + 1);
295 strcpy (tempname, inname);
296 p = tempname + strlen (tempname);
297 while (p != tempname && !IS_DIRECTORY_SEP (p[-1]))
298 p--;
299 *p = 0;
300 strcpy (p, "EXXXXXX");
301 mktemp (tempname);
cfa191ff 302 unlink (tempname);
237e0016 303
a4deff3c 304 while (1)
237e0016 305 {
a4deff3c
RS
306 /* Create the lock file, but not under the lock file name. */
307 /* Give up if cannot do that. */
308 desc = open (tempname, O_WRONLY | O_CREAT | O_EXCL, 0666);
309 if (desc < 0)
310 {
311 char *message = (char *) xmalloc (strlen (tempname) + 50);
4b265472 312 sprintf (message, "creating %s, which would become the lock file",
a4deff3c
RS
313 tempname);
314 pfatal_with_name (message);
315 }
316 close (desc);
317
318 tem = link (tempname, lockname);
097e9c90
CY
319
320#ifdef EPERM
321 if (tem < 0 && errno == EPERM)
322 fatal ("Unable to create hard link between %s and %s",
323 tempname, lockname);
324#endif
325
a4deff3c
RS
326 unlink (tempname);
327 if (tem >= 0)
328 break;
329 sleep (1);
330
331 /* If lock file is five minutes old, unlock it.
332 Five minutes should be good enough to cope with crashes
333 and wedgitude, and long enough to avoid being fooled
334 by time differences between machines. */
335 if (stat (lockname, &st) >= 0)
336 {
337 now = time (0);
338 if (st.st_ctime < now - 300)
339 unlink (lockname);
340 }
237e0016 341 }
237e0016 342
a4deff3c
RS
343 delete_lockname = lockname;
344 }
63cf923d
RS
345#endif /* not MAIL_USE_SYSTEM_LOCK */
346#endif /* not MAIL_USE_MMDF */
237e0016 347
8ca83cfd
RS
348 if (fork () == 0)
349 {
25025815 350 int lockcount = 0;
a4deff3c
RS
351 int status = 0;
352#if defined (MAIL_USE_MAILLOCK) && defined (HAVE_TOUCHLOCK)
bd41a17d 353 time_t touched_lock, now;
a4deff3c 354#endif
25025815 355
fbf4af3a 356 if (setuid (getuid ()) < 0 || setregid (-1, real_gid) < 0)
51a91f9d 357 fatal ("Failed to drop privileges", 0, 0);
8ca83cfd 358
63cf923d
RS
359#ifndef MAIL_USE_MMDF
360#ifdef MAIL_USE_SYSTEM_LOCK
8ca83cfd 361 indesc = open (inname, O_RDWR);
63cf923d 362#else /* if not MAIL_USE_SYSTEM_LOCK */
8ca83cfd 363 indesc = open (inname, O_RDONLY);
63cf923d 364#endif /* not MAIL_USE_SYSTEM_LOCK */
8ca83cfd
RS
365#else /* MAIL_USE_MMDF */
366 indesc = lk_open (inname, O_RDONLY, 0, 0, 10);
4293ba7f
RS
367#endif /* MAIL_USE_MMDF */
368
8ca83cfd
RS
369 if (indesc < 0)
370 pfatal_with_name (inname);
237e0016 371
76ed5e01 372#ifdef BSD_SYSTEM
8ca83cfd
RS
373 /* In case movemail is setuid to root, make sure the user can
374 read the output file. */
375 /* This is desirable for all systems
376 but I don't want to assume all have the umask system call */
377 umask (umask (0) & 0333);
76ed5e01 378#endif /* BSD_SYSTEM */
8ca83cfd
RS
379 outdesc = open (outname, O_WRONLY | O_CREAT | O_EXCL, 0666);
380 if (outdesc < 0)
381 pfatal_with_name (outname);
25025815 382
fbf4af3a 383 if (setregid (-1, priv_gid) < 0)
51a91f9d
CY
384 fatal ("Failed to regain privileges", 0, 0);
385
25025815
RS
386 /* This label exists so we can retry locking
387 after a delay, if it got EAGAIN or EBUSY. */
388 retry_lock:
389
390 /* Try to lock it. */
a4deff3c
RS
391#ifdef MAIL_USE_MAILLOCK
392 if (spool_name)
393 {
394 /* The "0 - " is to make it a negative number if maillock returns
395 non-zero. */
396 status = 0 - maillock (spool_name, 1);
397#ifdef HAVE_TOUCHLOCK
398 touched_lock = time (0);
399#endif
400 lockcount = 5;
401 }
402 else
403#endif /* MAIL_USE_MAILLOCK */
404 {
63cf923d
RS
405#ifdef MAIL_USE_SYSTEM_LOCK
406#ifdef MAIL_USE_LOCKF
a4deff3c 407 status = lockf (indesc, F_LOCK, 0);
63cf923d 408#else /* not MAIL_USE_LOCKF */
7f75d5c6 409#ifdef WINDOWSNT
a4deff3c 410 status = locking (indesc, LK_RLCK, -1L);
237e0016 411#else
a4deff3c 412 status = flock (indesc, LOCK_EX);
237e0016 413#endif
63cf923d
RS
414#endif /* not MAIL_USE_LOCKF */
415#endif /* MAIL_USE_SYSTEM_LOCK */
a4deff3c 416 }
237e0016 417
25025815
RS
418 /* If it fails, retry up to 5 times
419 for certain failure codes. */
420 if (status < 0)
421 {
422 if (++lockcount <= 5)
423 {
424#ifdef EAGAIN
425 if (errno == EAGAIN)
426 {
427 sleep (1);
428 goto retry_lock;
429 }
430#endif
431#ifdef EBUSY
432 if (errno == EBUSY)
433 {
434 sleep (1);
435 goto retry_lock;
436 }
437#endif
438 }
439
440 pfatal_with_name (inname);
441 }
177c0ea7 442
08564963 443 {
8ca83cfd
RS
444 char buf[1024];
445
446 while (1)
08564963 447 {
8ca83cfd 448 nread = read (indesc, buf, sizeof buf);
5e5b35c7
RS
449 if (nread < 0)
450 pfatal_with_name (inname);
8ca83cfd
RS
451 if (nread != write (outdesc, buf, nread))
452 {
453 int saved_errno = errno;
454 unlink (outname);
455 errno = saved_errno;
456 pfatal_with_name (outname);
457 }
458 if (nread < sizeof buf)
459 break;
a4deff3c
RS
460#if defined (MAIL_USE_MAILLOCK) && defined (HAVE_TOUCHLOCK)
461 if (spool_name)
462 {
463 now = time (0);
464 if (now - touched_lock > 60)
465 {
466 touchlock ();
467 touched_lock = now;
468 }
469 }
470#endif /* MAIL_USE_MAILLOCK */
08564963 471 }
08564963 472 }
237e0016 473
e397a017 474#ifdef BSD_SYSTEM
8ca83cfd
RS
475 if (fsync (outdesc) < 0)
476 pfatal_and_delete (outname);
237e0016
RS
477#endif
478
51a91f9d 479 /* Prevent symlink attacks truncating other users' mailboxes */
fbf4af3a 480 if (setregid (-1, real_gid) < 0)
51a91f9d
CY
481 fatal ("Failed to drop privileges", 0, 0);
482
8ca83cfd
RS
483 /* Check to make sure no errors before we zap the inbox. */
484 if (close (outdesc) != 0)
485 pfatal_and_delete (outname);
237e0016 486
63cf923d 487#ifdef MAIL_USE_SYSTEM_LOCK
fea4325c
RS
488 if (! preserve_mail)
489 {
9055082e
PE
490 if (ftruncate (indesc, 0L) != 0)
491 pfatal_with_name (inname);
b1cb2966 492 }
63cf923d 493#endif /* MAIL_USE_SYSTEM_LOCK */
4293ba7f
RS
494
495#ifdef MAIL_USE_MMDF
8ca83cfd 496 lk_close (indesc, 0, 0, 0);
4293ba7f 497#else
8ca83cfd 498 close (indesc);
4293ba7f 499#endif
237e0016 500
63cf923d 501#ifndef MAIL_USE_SYSTEM_LOCK
fea4325c
RS
502 if (! preserve_mail)
503 {
504 /* Delete the input file; if we can't, at least get rid of its
505 contents. */
e97dd183 506#ifdef MAIL_UNLINK_SPOOL
fea4325c
RS
507 /* This is generally bad to do, because it destroys the permissions
508 that were set on the file. Better to just empty the file. */
509 if (unlink (inname) < 0 && errno != ENOENT)
e97dd183 510#endif /* MAIL_UNLINK_SPOOL */
fea4325c
RS
511 creat (inname, 0600);
512 }
63cf923d 513#endif /* not MAIL_USE_SYSTEM_LOCK */
8ca83cfd 514
51a91f9d 515 /* End of mailbox truncation */
fbf4af3a 516 if (setregid (-1, priv_gid) < 0)
51a91f9d
CY
517 fatal ("Failed to regain privileges", 0, 0);
518
a4deff3c
RS
519#ifdef MAIL_USE_MAILLOCK
520 /* This has to occur in the child, i.e., in the process that
521 acquired the lock! */
522 if (spool_name)
523 mailunlock ();
524#endif
65396510 525 exit (EXIT_SUCCESS);
8ca83cfd
RS
526 }
527
528 wait (&status);
529 if (!WIFEXITED (status))
65396510 530 exit (EXIT_FAILURE);
8ca83cfd
RS
531 else if (WRETCODE (status) != 0)
532 exit (WRETCODE (status));
533
63cf923d 534#if !defined (MAIL_USE_MMDF) && !defined (MAIL_USE_SYSTEM_LOCK)
a4deff3c
RS
535#ifdef MAIL_USE_MAILLOCK
536 if (! spool_name)
537#endif /* MAIL_USE_MAILLOCK */
538 unlink (lockname);
63cf923d 539#endif /* not MAIL_USE_MMDF and not MAIL_USE_SYSTEM_LOCK */
7f75d5c6
RS
540
541#endif /* ! DISABLE_DIRECT_ACCESS */
542
65396510 543 return EXIT_SUCCESS;
237e0016 544}
a4deff3c
RS
545
546#ifdef MAIL_USE_MAILLOCK
547/* This function uses stat to confirm that the mail directory is
548 identical to the directory of the input file, rather than just
549 string-comparing the two paths, because one or both of them might
550 be symbolic links pointing to some other directory. */
551static char *
728a982d 552mail_spool_name (char *inname)
a4deff3c
RS
553{
554 struct stat stat1, stat2;
555 char *indir, *fname;
556 int status;
557
8966b757 558 if (! (fname = strrchr (inname, '/')))
a4deff3c
RS
559 return NULL;
560
561 fname++;
562
563 if (stat (MAILDIR, &stat1) < 0)
564 return NULL;
565
566 indir = (char *) xmalloc (fname - inname + 1);
567 strncpy (indir, inname, fname - inname);
568 indir[fname-inname] = '\0';
569
570
571 status = stat (indir, &stat2);
572
573 free (indir);
574
575 if (status < 0)
576 return NULL;
577
c4009c1f
RS
578 if (stat1.st_dev == stat2.st_dev
579 && stat1.st_ino == stat2.st_ino)
a4deff3c
RS
580 return fname;
581
582 return NULL;
583}
584#endif /* MAIL_USE_MAILLOCK */
237e0016
RS
585\f
586/* Print error message and exit. */
587
1725ae55 588static void
988e88ab 589fatal (const char *s1, const char *s2, const char *s3)
237e0016
RS
590{
591 if (delete_lockname)
592 unlink (delete_lockname);
a9eedf40 593 error (s1, s2, s3);
65396510 594 exit (EXIT_FAILURE);
237e0016
RS
595}
596
cc3b64e8
DL
597/* Print error message. `s1' is printf control string, `s2' and `s3'
598 are args for it or null. */
237e0016 599
1725ae55 600static void
988e88ab 601error (const char *s1, const char *s2, const char *s3)
237e0016 602{
e2f9d9af 603 fprintf (stderr, "movemail: ");
cc3b64e8
DL
604 if (s3)
605 fprintf (stderr, s1, s2, s3);
606 else if (s2)
607 fprintf (stderr, s1, s2);
608 else
3b3807f8 609 fprintf (stderr, "%s", s1);
e2f9d9af 610 fprintf (stderr, "\n");
237e0016
RS
611}
612
1725ae55 613static void
873fbd0b 614pfatal_with_name (char *name)
237e0016 615{
a9eedf40 616 fatal ("%s for %s", strerror (errno), name);
237e0016
RS
617}
618
1725ae55 619static void
873fbd0b 620pfatal_and_delete (char *name)
cfa191ff 621{
a9eedf40 622 char *s = strerror (errno);
cfa191ff 623 unlink (name);
a9eedf40 624 fatal ("%s for %s", s, name);
cfa191ff
RS
625}
626
237e0016
RS
627/* Return a newly-allocated string whose contents concatenate those of s1, s2, s3. */
628
1725ae55 629static char *
988e88ab 630concat (const char *s1, const char *s2, const char *s3)
237e0016 631{
728a982d 632 size_t len1 = strlen (s1), len2 = strlen (s2), len3 = strlen (s3);
237e0016
RS
633 char *result = (char *) xmalloc (len1 + len2 + len3 + 1);
634
635 strcpy (result, s1);
636 strcpy (result + len1, s2);
637 strcpy (result + len1 + len2, s3);
638 *(result + len1 + len2 + len3) = 0;
639
640 return result;
641}
642
643/* Like malloc but get fatal error if memory is exhausted. */
644
1725ae55 645static long *
873fbd0b 646xmalloc (unsigned int size)
237e0016 647{
2583d6d7 648 long *result = (long *) malloc (size);
237e0016 649 if (!result)
a9eedf40 650 fatal ("virtual memory exhausted", 0, 0);
237e0016
RS
651 return result;
652}
653\f
654/* This is the guts of the interface to the Post Office Protocol. */
655
656#ifdef MAIL_USE_POP
657
7f75d5c6 658#ifndef WINDOWSNT
237e0016
RS
659#include <sys/socket.h>
660#include <netinet/in.h>
661#include <netdb.h>
7f75d5c6
RS
662#else
663#undef _WINSOCKAPI_
664#include <winsock.h>
665#endif
cecf0f21 666#include <pwd.h>
d228a23c 667#include <string.h>
237e0016 668
237e0016
RS
669#define NOTOK (-1)
670#define OK 0
671#define DONE 1
672
673char *progname;
674FILE *sfi;
675FILE *sfo;
2e82e3c3
RS
676char ibuffer[BUFSIZ];
677char obuffer[BUFSIZ];
752fb472
DL
678char Errmsg[200]; /* POP errors, at least, can exceed
679 the original length of 80. */
237e0016 680
476b2799 681/*
4d90eee4 682 * The full valid syntax for a POP mailbox specification for movemail
476b2799
GM
683 * is "po:username:hostname". The ":hostname" is optional; if it is
684 * omitted, the MAILHOST environment variable will be consulted. Note
685 * that by the time popmail() is called the "po:" has been stripped
686 * off of the front of the mailbox name.
687 *
688 * If the mailbox is in the form "po:username:hostname", then it is
689 * modified by this function -- the second colon is replaced by a
690 * null.
65396510
TTN
691 *
692 * Return a value suitable for passing to `exit'.
476b2799
GM
693 */
694
1725ae55 695static int
873fbd0b 696popmail (char *mailbox, char *outfile, int preserve, char *password, int reverse_order)
237e0016 697{
b1ce62a8 698 int nmsgs, nbytes;
b1ce62a8
RS
699 register int i;
700 int mbfi;
701 FILE *mbf;
873fbd0b 702 char *getenv (const char *);
b32701a7 703 popserver server;
a2997b0f 704 int start, end, increment;
476b2799
GM
705 char *user, *hostname;
706
707 user = mailbox;
8966b757 708 if ((hostname = strchr (mailbox, ':')))
476b2799 709 *hostname++ = '\0';
237e0016 710
476b2799 711 server = pop_open (hostname, user, password, POP_NO_GETPASS);
2e82e3c3 712 if (! server)
b1ce62a8 713 {
cc3b64e8 714 error ("Error connecting to POP server: %s", pop_error, 0);
65396510 715 return EXIT_FAILURE;
237e0016
RS
716 }
717
2e82e3c3 718 if (pop_stat (server, &nmsgs, &nbytes))
b1ce62a8 719 {
cc3b64e8 720 error ("Error getting message count from POP server: %s", pop_error, 0);
65396510 721 return EXIT_FAILURE;
237e0016
RS
722 }
723
b1ce62a8
RS
724 if (!nmsgs)
725 {
2e82e3c3 726 pop_close (server);
65396510 727 return EXIT_SUCCESS;
b1ce62a8
RS
728 }
729
730 mbfi = open (outfile, O_WRONLY | O_CREAT | O_EXCL, 0666);
731 if (mbfi < 0)
732 {
2e82e3c3
RS
733 pop_close (server);
734 error ("Error in open: %s, %s", strerror (errno), outfile);
65396510 735 return EXIT_FAILURE;
b1ce62a8
RS
736 }
737 fchown (mbfi, getuid (), -1);
738
7f75d5c6 739 if ((mbf = fdopen (mbfi, "wb")) == NULL)
b1ce62a8 740 {
2e82e3c3 741 pop_close (server);
cc3b64e8 742 error ("Error in fdopen: %s", strerror (errno), 0);
2e82e3c3
RS
743 close (mbfi);
744 unlink (outfile);
65396510 745 return EXIT_FAILURE;
b1ce62a8
RS
746 }
747
a2997b0f
KH
748 if (reverse_order)
749 {
750 start = nmsgs;
751 end = 1;
752 increment = -1;
753 }
754 else
755 {
756 start = 1;
757 end = nmsgs;
758 increment = 1;
759 }
760
761 for (i = start; i * increment <= end * increment; i += increment)
b1ce62a8
RS
762 {
763 mbx_delimit_begin (mbf);
ff804ff5 764 if (pop_retr (server, i, mbf) != OK)
b1ce62a8 765 {
17a60964 766 error ("%s", Errmsg, 0);
b1ce62a8 767 close (mbfi);
65396510 768 return EXIT_FAILURE;
237e0016 769 }
b1ce62a8
RS
770 mbx_delimit_end (mbf);
771 fflush (mbf);
2e82e3c3
RS
772 if (ferror (mbf))
773 {
cc3b64e8 774 error ("Error in fflush: %s", strerror (errno), 0);
2e82e3c3
RS
775 pop_close (server);
776 close (mbfi);
65396510 777 return EXIT_FAILURE;
2e82e3c3 778 }
237e0016
RS
779 }
780
2e82e3c3
RS
781 /* On AFS, a call to write only modifies the file in the local
782 * workstation's AFS cache. The changes are not written to the server
783 * until a call to fsync or close is made. Users with AFS home
784 * directories have lost mail when over quota because these checks were
785 * not made in previous versions of movemail. */
786
e397a017 787#ifdef BSD_SYSTEM
cfa191ff
RS
788 if (fsync (mbfi) < 0)
789 {
08fa58c9 790 error ("Error in fsync: %s", strerror (errno), 0);
65396510 791 return EXIT_FAILURE;
cfa191ff 792 }
340ff9de 793#endif
cfa191ff
RS
794
795 if (close (mbfi) == -1)
796 {
cc3b64e8 797 error ("Error in close: %s", strerror (errno), 0);
65396510 798 return EXIT_FAILURE;
cfa191ff
RS
799 }
800
fea4325c
RS
801 if (! preserve)
802 for (i = 1; i <= nmsgs; i++)
803 {
804 if (pop_delete (server, i))
805 {
cc3b64e8 806 error ("Error from POP server: %s", pop_error, 0);
fea4325c 807 pop_close (server);
65396510 808 return EXIT_FAILURE;
fea4325c
RS
809 }
810 }
237e0016 811
2e82e3c3 812 if (pop_quit (server))
b1ce62a8 813 {
cc3b64e8 814 error ("Error from POP server: %s", pop_error, 0);
65396510 815 return EXIT_FAILURE;
237e0016 816 }
177c0ea7 817
65396510 818 return EXIT_SUCCESS;
237e0016
RS
819}
820
1725ae55 821static int
873fbd0b 822pop_retr (popserver server, int msgno, FILE *arg)
237e0016 823{
2e82e3c3
RS
824 char *line;
825 int ret;
237e0016 826
2e82e3c3 827 if (pop_retrieve_first (server, msgno, &line))
b1ce62a8 828 {
9b956af2
KH
829 char *error = concat ("Error from POP server: ", pop_error, "");
830 strncpy (Errmsg, error, sizeof (Errmsg));
2e82e3c3 831 Errmsg[sizeof (Errmsg)-1] = '\0';
9b956af2 832 free(error);
2e82e3c3 833 return (NOTOK);
237e0016
RS
834 }
835
d89d0243 836 while ((ret = pop_retrieve_next (server, &line)) >= 0)
b1ce62a8 837 {
2e82e3c3
RS
838 if (! line)
839 break;
840
d89d0243 841 if (mbx_write (line, ret, arg) != OK)
b1ce62a8 842 {
2e82e3c3
RS
843 strcpy (Errmsg, strerror (errno));
844 pop_close (server);
845 return (NOTOK);
237e0016
RS
846 }
847 }
237e0016 848
2e82e3c3 849 if (ret)
b1ce62a8 850 {
9b956af2
KH
851 char *error = concat ("Error from POP server: ", pop_error, "");
852 strncpy (Errmsg, error, sizeof (Errmsg));
2e82e3c3 853 Errmsg[sizeof (Errmsg)-1] = '\0';
9b956af2 854 free(error);
2e82e3c3 855 return (NOTOK);
237e0016
RS
856 }
857
2e82e3c3 858 return (OK);
237e0016
RS
859}
860
2e82e3c3
RS
861/* Do this as a macro instead of using strcmp to save on execution time. */
862#define IS_FROM_LINE(a) ((a[0] == 'F') \
863 && (a[1] == 'r') \
864 && (a[2] == 'o') \
865 && (a[3] == 'm') \
866 && (a[4] == ' '))
237e0016 867
1725ae55 868static int
873fbd0b 869mbx_write (char *line, int len, FILE *mbf)
237e0016 870{
d04f5031 871#ifdef MOVEMAIL_QUOTE_POP_FROM_LINES
2e82e3c3
RS
872 if (IS_FROM_LINE (line))
873 {
874 if (fputc ('>', mbf) == EOF)
875 return (NOTOK);
876 }
d04f5031
PE
877#endif
878 if (line[0] == '\037')
879 {
880 if (fputs ("^_", mbf) == EOF)
881 return (NOTOK);
882 line++;
883 len--;
884 }
177c0ea7 885 if (fwrite (line, 1, len, mbf) != len)
2e82e3c3
RS
886 return (NOTOK);
887 if (fputc (0x0a, mbf) == EOF)
888 return (NOTOK);
889 return (OK);
237e0016
RS
890}
891
1725ae55 892static int
873fbd0b 893mbx_delimit_begin (FILE *mbf)
237e0016 894{
d228a23c
GM
895 time_t now;
896 struct tm *ltime;
897 char fromline[40] = "From movemail ";
898
899 now = time (NULL);
900 ltime = localtime (&now);
901
902 strcat (fromline, asctime (ltime));
903
904 if (fputs (fromline, mbf) == EOF)
2e82e3c3
RS
905 return (NOTOK);
906 return (OK);
237e0016
RS
907}
908
1725ae55 909static int
873fbd0b 910mbx_delimit_end (FILE *mbf)
237e0016 911{
3f32be22 912 if (putc ('\n', mbf) == EOF)
2e82e3c3
RS
913 return (NOTOK);
914 return (OK);
237e0016
RS
915}
916
917#endif /* MAIL_USE_POP */
e5f7ea68
RM
918\f
919#ifndef HAVE_STRERROR
920char *
921strerror (errnum)
922 int errnum;
923{
924 extern char *sys_errlist[];
925 extern int sys_nerr;
926
927 if (errnum >= 0 && errnum < sys_nerr)
928 return sys_errlist[errnum];
929 return (char *) "Unknown error";
930}
931
932#endif /* ! HAVE_STRERROR */
ab5796a9 933
65396510
TTN
934
935/* movemail.c ends here */