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