Commit | Line | Data |
---|---|---|
237e0016 RS |
1 | /* movemail foo bar -- move file foo to file bar, |
2 | locking file foo the way /bin/mail respects. | |
39356651 | 3 | Copyright (C) 1986, 1992, 1993, 1994 Free Software Foundation, Inc. |
237e0016 RS |
4 | |
5 | This file is part of GNU Emacs. | |
6 | ||
93320c23 JA |
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 | |
39356651 | 9 | the Free Software Foundation; either version 2, or (at your option) |
93320c23 JA |
10 | any later version. |
11 | ||
237e0016 | 12 | GNU Emacs is distributed in the hope that it will be useful, |
93320c23 JA |
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. | |
237e0016 | 16 | |
93320c23 JA |
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. */ | |
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 | * | |
36 | * Added POP (Post Office Protocol) service. When compiled -DPOP | |
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. | |
41 | * | |
42 | * New module: popmail.c | |
43 | * Modified routines: | |
cfa191ff | 44 | * main - added code within #ifdef MAIL_USE_POP; added setuid (getuid ()) |
237e0016 RS |
45 | * after POP code. |
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 | ||
3b9ee819 RS |
56 | #define NO_SHORTNAMES /* Tell config not to load remap.h */ |
57 | #include <../src/config.h> | |
237e0016 RS |
58 | #include <sys/types.h> |
59 | #include <sys/stat.h> | |
60 | #include <sys/file.h> | |
e2f9d9af | 61 | #include <stdio.h> |
237e0016 | 62 | #include <errno.h> |
8ca83cfd | 63 | #include <../src/syswait.h> |
2e82e3c3 RS |
64 | #ifdef MAIL_USE_POP |
65 | #include "pop.h" | |
66 | #endif | |
237e0016 | 67 | |
91cf09ac RS |
68 | #ifdef MSDOS |
69 | #undef access | |
70 | #endif /* MSDOS */ | |
71 | ||
237e0016 RS |
72 | #ifdef USG |
73 | #include <fcntl.h> | |
74 | #include <unistd.h> | |
4ec9a77a RS |
75 | #ifndef F_OK |
76 | #define F_OK 0 | |
77 | #define X_OK 1 | |
78 | #define W_OK 2 | |
79 | #define R_OK 4 | |
80 | #endif | |
237e0016 RS |
81 | #endif /* USG */ |
82 | ||
29beb080 RS |
83 | #ifdef HAVE_UNISTD_H |
84 | #include <unistd.h> | |
85 | #endif | |
86 | ||
237e0016 RS |
87 | #ifdef XENIX |
88 | #include <sys/locking.h> | |
89 | #endif | |
90 | ||
63cf923d RS |
91 | #ifdef MAIL_USE_LOCKF |
92 | #define MAIL_USE_SYSTEM_LOCK | |
93 | #endif | |
94 | ||
95 | #ifdef MAIL_USE_FLOCK | |
96 | #define MAIL_USE_SYSTEM_LOCK | |
97 | #endif | |
98 | ||
4293ba7f RS |
99 | #ifdef MAIL_USE_MMDF |
100 | extern int lk_open (), lk_close (); | |
101 | #endif | |
102 | ||
237e0016 RS |
103 | /* Cancel substitutions made by config.h for Emacs. */ |
104 | #undef open | |
105 | #undef read | |
106 | #undef write | |
107 | #undef close | |
108 | ||
e97dd183 | 109 | #ifndef errno |
237e0016 | 110 | extern int errno; |
e97dd183 | 111 | #endif |
e2f9d9af | 112 | char *strerror (); |
e2f9d9af DM |
113 | |
114 | void fatal (); | |
115 | void error (); | |
116 | void pfatal_with_name (); | |
117 | void pfatal_and_delete (); | |
118 | char *concat (); | |
2583d6d7 | 119 | long *xmalloc (); |
e2f9d9af DM |
120 | int popmail (); |
121 | int pop_retr (); | |
122 | int mbx_write (); | |
123 | int mbx_delimit_begin (); | |
124 | int mbx_delimit_end (); | |
237e0016 RS |
125 | |
126 | /* Nonzero means this is name of a lock file to delete on fatal error. */ | |
127 | char *delete_lockname; | |
128 | ||
e2f9d9af | 129 | int |
237e0016 RS |
130 | main (argc, argv) |
131 | int argc; | |
132 | char **argv; | |
133 | { | |
134 | char *inname, *outname; | |
135 | int indesc, outdesc; | |
237e0016 | 136 | int nread; |
8ca83cfd | 137 | WAITTYPE status; |
237e0016 | 138 | |
63cf923d | 139 | #ifndef MAIL_USE_SYSTEM_LOCK |
237e0016 RS |
140 | struct stat st; |
141 | long now; | |
142 | int tem; | |
143 | char *lockname, *p; | |
906ad89d | 144 | char *tempname; |
237e0016 | 145 | int desc; |
63cf923d | 146 | #endif /* not MAIL_USE_SYSTEM_LOCK */ |
237e0016 RS |
147 | |
148 | delete_lockname = 0; | |
149 | ||
150 | if (argc < 3) | |
e2f9d9af | 151 | { |
837a3add | 152 | fprintf (stderr, "Usage: movemail inbox destfile\n"); |
e2f9d9af DM |
153 | exit(1); |
154 | } | |
237e0016 RS |
155 | |
156 | inname = argv[1]; | |
157 | outname = argv[2]; | |
158 | ||
4293ba7f RS |
159 | #ifdef MAIL_USE_MMDF |
160 | mmdf_init (argv[0]); | |
161 | #endif | |
162 | ||
b1ce62a8 | 163 | /* Check access to output file. */ |
237e0016 RS |
164 | if (access (outname, F_OK) == 0 && access (outname, W_OK) != 0) |
165 | pfatal_with_name (outname); | |
166 | ||
167 | /* Also check that outname's directory is writeable to the real uid. */ | |
168 | { | |
91cf09ac | 169 | char *buf = (char *) xmalloc (strlen (outname) + 1); |
e2f9d9af | 170 | char *p; |
237e0016 RS |
171 | strcpy (buf, outname); |
172 | p = buf + strlen (buf); | |
173 | while (p > buf && p[-1] != '/') | |
174 | *--p = 0; | |
175 | if (p == buf) | |
176 | *p++ = '.'; | |
177 | if (access (buf, W_OK) != 0) | |
178 | pfatal_with_name (buf); | |
179 | free (buf); | |
180 | } | |
181 | ||
182 | #ifdef MAIL_USE_POP | |
12a0565a | 183 | if (!strncmp (inname, "po:", 3)) |
237e0016 | 184 | { |
b3112191 | 185 | int status; |
237e0016 | 186 | |
b3112191 | 187 | status = popmail (inname + 3, outname); |
237e0016 RS |
188 | exit (status); |
189 | } | |
190 | ||
cfa191ff | 191 | setuid (getuid ()); |
237e0016 RS |
192 | #endif /* MAIL_USE_POP */ |
193 | ||
b1ce62a8 RS |
194 | /* Check access to input file. */ |
195 | if (access (inname, R_OK | W_OK) != 0) | |
196 | pfatal_with_name (inname); | |
197 | ||
4293ba7f | 198 | #ifndef MAIL_USE_MMDF |
63cf923d | 199 | #ifndef MAIL_USE_SYSTEM_LOCK |
f7f8635d | 200 | /* Use a lock file named after our first argument with .lock appended: |
237e0016 | 201 | If it exists, the mail file is locked. */ |
06000e3c RS |
202 | /* Note: this locking mechanism is *required* by the mailer |
203 | (on systems which use it) to prevent loss of mail. | |
204 | ||
205 | On systems that use a lock file, extracting the mail without locking | |
206 | WILL occasionally cause loss of mail due to timing errors! | |
207 | ||
208 | So, if creation of the lock file fails | |
f7f8635d | 209 | due to access permission on the mail spool directory, |
06000e3c RS |
210 | you simply MUST change the permission |
211 | and/or make movemail a setgid program | |
212 | so it can create lock files properly. | |
213 | ||
214 | You might also wish to verify that your system is one | |
215 | which uses lock files for this purpose. Some systems use other methods. | |
216 | ||
217 | If your system uses the `flock' system call for mail locking, | |
63cf923d | 218 | define MAIL_USE_SYSTEM_LOCK in config.h or the s-*.h file |
06000e3c | 219 | and recompile movemail. If the s- file for your system |
63cf923d | 220 | should define MAIL_USE_SYSTEM_LOCK but does not, send a bug report |
06000e3c RS |
221 | to bug-gnu-emacs@prep.ai.mit.edu so we can fix it. */ |
222 | ||
237e0016 | 223 | lockname = concat (inname, ".lock", ""); |
3f628ebd RS |
224 | tempname = (char *) xmalloc (strlen (inname) + strlen ("EXXXXXX") + 1); |
225 | strcpy (tempname, inname); | |
237e0016 RS |
226 | p = tempname + strlen (tempname); |
227 | while (p != tempname && p[-1] != '/') | |
228 | p--; | |
229 | *p = 0; | |
230 | strcpy (p, "EXXXXXX"); | |
231 | mktemp (tempname); | |
cfa191ff | 232 | unlink (tempname); |
237e0016 RS |
233 | |
234 | while (1) | |
235 | { | |
236 | /* Create the lock file, but not under the lock file name. */ | |
237 | /* Give up if cannot do that. */ | |
8ca83cfd | 238 | desc = open (tempname, O_WRONLY | O_CREAT | O_EXCL, 0666); |
237e0016 | 239 | if (desc < 0) |
d0776f5c RS |
240 | { |
241 | char *message = (char *) malloc (strlen (tempname) + 50); | |
242 | sprintf (message, "%s--see source file lib-src/movemail.c"); | |
243 | pfatal_with_name (message); | |
244 | } | |
237e0016 RS |
245 | close (desc); |
246 | ||
247 | tem = link (tempname, lockname); | |
cfa191ff | 248 | unlink (tempname); |
237e0016 RS |
249 | if (tem >= 0) |
250 | break; | |
251 | sleep (1); | |
252 | ||
66a8ad0f RS |
253 | /* If lock file is five minutes old, unlock it. |
254 | Five minutes should be good enough to cope with crashes | |
255 | and wedgitude, and long enough to avoid being fooled | |
256 | by time differences between machines. */ | |
237e0016 RS |
257 | if (stat (lockname, &st) >= 0) |
258 | { | |
259 | now = time (0); | |
66a8ad0f | 260 | if (st.st_ctime < now - 300) |
cfa191ff | 261 | unlink (lockname); |
237e0016 RS |
262 | } |
263 | } | |
264 | ||
265 | delete_lockname = lockname; | |
63cf923d RS |
266 | #endif /* not MAIL_USE_SYSTEM_LOCK */ |
267 | #endif /* not MAIL_USE_MMDF */ | |
237e0016 | 268 | |
8ca83cfd RS |
269 | if (fork () == 0) |
270 | { | |
d5216c20 | 271 | setuid (getuid ()); |
8ca83cfd | 272 | |
63cf923d RS |
273 | #ifndef MAIL_USE_MMDF |
274 | #ifdef MAIL_USE_SYSTEM_LOCK | |
8ca83cfd | 275 | indesc = open (inname, O_RDWR); |
63cf923d | 276 | #else /* if not MAIL_USE_SYSTEM_LOCK */ |
8ca83cfd | 277 | indesc = open (inname, O_RDONLY); |
63cf923d | 278 | #endif /* not MAIL_USE_SYSTEM_LOCK */ |
8ca83cfd RS |
279 | #else /* MAIL_USE_MMDF */ |
280 | indesc = lk_open (inname, O_RDONLY, 0, 0, 10); | |
4293ba7f RS |
281 | #endif /* MAIL_USE_MMDF */ |
282 | ||
8ca83cfd RS |
283 | if (indesc < 0) |
284 | pfatal_with_name (inname); | |
237e0016 | 285 | |
cfa191ff | 286 | #if defined (BSD) || defined (XENIX) |
8ca83cfd RS |
287 | /* In case movemail is setuid to root, make sure the user can |
288 | read the output file. */ | |
289 | /* This is desirable for all systems | |
290 | but I don't want to assume all have the umask system call */ | |
291 | umask (umask (0) & 0333); | |
237e0016 | 292 | #endif /* BSD or Xenix */ |
8ca83cfd RS |
293 | outdesc = open (outname, O_WRONLY | O_CREAT | O_EXCL, 0666); |
294 | if (outdesc < 0) | |
295 | pfatal_with_name (outname); | |
63cf923d RS |
296 | #ifdef MAIL_USE_SYSTEM_LOCK |
297 | #ifdef MAIL_USE_LOCKF | |
298 | if (lockf (indesc, F_LOCK, 0) < 0) pfatal_with_name (inname); | |
299 | #else /* not MAIL_USE_LOCKF */ | |
237e0016 | 300 | #ifdef XENIX |
8ca83cfd | 301 | if (locking (indesc, LK_RLCK, 0L) < 0) pfatal_with_name (inname); |
237e0016 | 302 | #else |
8ca83cfd | 303 | if (flock (indesc, LOCK_EX) < 0) pfatal_with_name (inname); |
237e0016 | 304 | #endif |
63cf923d RS |
305 | #endif /* not MAIL_USE_LOCKF */ |
306 | #endif /* MAIL_USE_SYSTEM_LOCK */ | |
237e0016 | 307 | |
08564963 | 308 | { |
8ca83cfd RS |
309 | char buf[1024]; |
310 | ||
311 | while (1) | |
08564963 | 312 | { |
8ca83cfd RS |
313 | nread = read (indesc, buf, sizeof buf); |
314 | if (nread != write (outdesc, buf, nread)) | |
315 | { | |
316 | int saved_errno = errno; | |
317 | unlink (outname); | |
318 | errno = saved_errno; | |
319 | pfatal_with_name (outname); | |
320 | } | |
321 | if (nread < sizeof buf) | |
322 | break; | |
08564963 | 323 | } |
08564963 | 324 | } |
237e0016 RS |
325 | |
326 | #ifdef BSD | |
8ca83cfd RS |
327 | if (fsync (outdesc) < 0) |
328 | pfatal_and_delete (outname); | |
237e0016 RS |
329 | #endif |
330 | ||
8ca83cfd RS |
331 | /* Check to make sure no errors before we zap the inbox. */ |
332 | if (close (outdesc) != 0) | |
333 | pfatal_and_delete (outname); | |
237e0016 | 334 | |
63cf923d | 335 | #ifdef MAIL_USE_SYSTEM_LOCK |
cfa191ff | 336 | #if defined (STRIDE) || defined (XENIX) |
8ca83cfd RS |
337 | /* Stride, xenix have file locking, but no ftruncate. This mess will do. */ |
338 | close (open (inname, O_CREAT | O_TRUNC | O_RDWR, 0666)); | |
237e0016 | 339 | #else |
8ca83cfd | 340 | ftruncate (indesc, 0L); |
237e0016 | 341 | #endif /* STRIDE or XENIX */ |
63cf923d | 342 | #endif /* MAIL_USE_SYSTEM_LOCK */ |
4293ba7f RS |
343 | |
344 | #ifdef MAIL_USE_MMDF | |
8ca83cfd | 345 | lk_close (indesc, 0, 0, 0); |
4293ba7f | 346 | #else |
8ca83cfd | 347 | close (indesc); |
4293ba7f | 348 | #endif |
237e0016 | 349 | |
63cf923d | 350 | #ifndef MAIL_USE_SYSTEM_LOCK |
e5f7ea68 RM |
351 | /* Delete the input file; if we can't, at least get rid of its |
352 | contents. */ | |
e97dd183 | 353 | #ifdef MAIL_UNLINK_SPOOL |
8ca83cfd RS |
354 | /* This is generally bad to do, because it destroys the permissions |
355 | that were set on the file. Better to just empty the file. */ | |
356 | if (unlink (inname) < 0 && errno != ENOENT) | |
e97dd183 | 357 | #endif /* MAIL_UNLINK_SPOOL */ |
8ca83cfd | 358 | creat (inname, 0600); |
63cf923d | 359 | #endif /* not MAIL_USE_SYSTEM_LOCK */ |
8ca83cfd RS |
360 | |
361 | exit (0); | |
362 | } | |
363 | ||
364 | wait (&status); | |
365 | if (!WIFEXITED (status)) | |
366 | exit (1); | |
367 | else if (WRETCODE (status) != 0) | |
368 | exit (WRETCODE (status)); | |
369 | ||
63cf923d | 370 | #if !defined (MAIL_USE_MMDF) && !defined (MAIL_USE_SYSTEM_LOCK) |
4293ba7f | 371 | unlink (lockname); |
63cf923d | 372 | #endif /* not MAIL_USE_MMDF and not MAIL_USE_SYSTEM_LOCK */ |
e2f9d9af | 373 | return 0; |
237e0016 RS |
374 | } |
375 | \f | |
376 | /* Print error message and exit. */ | |
377 | ||
e2f9d9af | 378 | void |
237e0016 RS |
379 | fatal (s1, s2) |
380 | char *s1, *s2; | |
381 | { | |
382 | if (delete_lockname) | |
383 | unlink (delete_lockname); | |
384 | error (s1, s2); | |
385 | exit (1); | |
386 | } | |
387 | ||
388 | /* Print error message. `s1' is printf control string, `s2' is arg for it. */ | |
389 | ||
e2f9d9af | 390 | void |
b1ce62a8 RS |
391 | error (s1, s2, s3) |
392 | char *s1, *s2, *s3; | |
237e0016 | 393 | { |
e2f9d9af DM |
394 | fprintf (stderr, "movemail: "); |
395 | fprintf (stderr, s1, s2, s3); | |
396 | fprintf (stderr, "\n"); | |
237e0016 RS |
397 | } |
398 | ||
e2f9d9af | 399 | void |
237e0016 RS |
400 | pfatal_with_name (name) |
401 | char *name; | |
402 | { | |
e2f9d9af | 403 | char *s = concat ("", strerror (errno), " for %s"); |
237e0016 RS |
404 | fatal (s, name); |
405 | } | |
406 | ||
e2f9d9af | 407 | void |
cfa191ff RS |
408 | pfatal_and_delete (name) |
409 | char *name; | |
410 | { | |
e2f9d9af | 411 | char *s = concat ("", strerror (errno), " for %s"); |
cfa191ff RS |
412 | unlink (name); |
413 | fatal (s, name); | |
414 | } | |
415 | ||
237e0016 RS |
416 | /* Return a newly-allocated string whose contents concatenate those of s1, s2, s3. */ |
417 | ||
418 | char * | |
419 | concat (s1, s2, s3) | |
420 | char *s1, *s2, *s3; | |
421 | { | |
422 | int len1 = strlen (s1), len2 = strlen (s2), len3 = strlen (s3); | |
423 | char *result = (char *) xmalloc (len1 + len2 + len3 + 1); | |
424 | ||
425 | strcpy (result, s1); | |
426 | strcpy (result + len1, s2); | |
427 | strcpy (result + len1 + len2, s3); | |
428 | *(result + len1 + len2 + len3) = 0; | |
429 | ||
430 | return result; | |
431 | } | |
432 | ||
433 | /* Like malloc but get fatal error if memory is exhausted. */ | |
434 | ||
2583d6d7 | 435 | long * |
237e0016 | 436 | xmalloc (size) |
e97dd183 | 437 | unsigned size; |
237e0016 | 438 | { |
2583d6d7 | 439 | long *result = (long *) malloc (size); |
237e0016 RS |
440 | if (!result) |
441 | fatal ("virtual memory exhausted", 0); | |
442 | return result; | |
443 | } | |
444 | \f | |
445 | /* This is the guts of the interface to the Post Office Protocol. */ | |
446 | ||
447 | #ifdef MAIL_USE_POP | |
448 | ||
449 | #include <sys/socket.h> | |
450 | #include <netinet/in.h> | |
451 | #include <netdb.h> | |
452 | #include <stdio.h> | |
cecf0f21 | 453 | #include <pwd.h> |
237e0016 RS |
454 | |
455 | #ifdef USG | |
456 | #include <fcntl.h> | |
457 | /* Cancel substitutions made by config.h for Emacs. */ | |
458 | #undef open | |
459 | #undef read | |
460 | #undef write | |
461 | #undef close | |
462 | #endif /* USG */ | |
463 | ||
464 | #define NOTOK (-1) | |
465 | #define OK 0 | |
466 | #define DONE 1 | |
467 | ||
468 | char *progname; | |
469 | FILE *sfi; | |
470 | FILE *sfo; | |
2e82e3c3 RS |
471 | char ibuffer[BUFSIZ]; |
472 | char obuffer[BUFSIZ]; | |
237e0016 RS |
473 | char Errmsg[80]; |
474 | ||
b1ce62a8 RS |
475 | popmail (user, outfile) |
476 | char *user; | |
477 | char *outfile; | |
237e0016 | 478 | { |
b1ce62a8 | 479 | int nmsgs, nbytes; |
b1ce62a8 RS |
480 | register int i; |
481 | int mbfi; | |
482 | FILE *mbf; | |
2e82e3c3 RS |
483 | char *getenv (); |
484 | int mbx_write (); | |
b32701a7 | 485 | popserver server; |
2e82e3c3 | 486 | extern char *strerror (); |
237e0016 | 487 | |
2e82e3c3 RS |
488 | server = pop_open (0, user, 0, POP_NO_GETPASS); |
489 | if (! server) | |
b1ce62a8 | 490 | { |
2e82e3c3 RS |
491 | error (pop_error); |
492 | return (1); | |
237e0016 RS |
493 | } |
494 | ||
2e82e3c3 | 495 | if (pop_stat (server, &nmsgs, &nbytes)) |
b1ce62a8 | 496 | { |
2e82e3c3 RS |
497 | error (pop_error); |
498 | return (1); | |
237e0016 RS |
499 | } |
500 | ||
b1ce62a8 RS |
501 | if (!nmsgs) |
502 | { | |
2e82e3c3 RS |
503 | pop_close (server); |
504 | return (0); | |
b1ce62a8 RS |
505 | } |
506 | ||
507 | mbfi = open (outfile, O_WRONLY | O_CREAT | O_EXCL, 0666); | |
508 | if (mbfi < 0) | |
509 | { | |
2e82e3c3 RS |
510 | pop_close (server); |
511 | error ("Error in open: %s, %s", strerror (errno), outfile); | |
512 | return (1); | |
b1ce62a8 RS |
513 | } |
514 | fchown (mbfi, getuid (), -1); | |
515 | ||
516 | if ((mbf = fdopen (mbfi, "w")) == NULL) | |
517 | { | |
2e82e3c3 RS |
518 | pop_close (server); |
519 | error ("Error in fdopen: %s", strerror (errno)); | |
520 | close (mbfi); | |
521 | unlink (outfile); | |
522 | return (1); | |
b1ce62a8 RS |
523 | } |
524 | ||
525 | for (i = 1; i <= nmsgs; i++) | |
526 | { | |
527 | mbx_delimit_begin (mbf); | |
2e82e3c3 | 528 | if (pop_retr (server, i, mbx_write, mbf) != OK) |
b1ce62a8 | 529 | { |
2e82e3c3 | 530 | error (Errmsg); |
b1ce62a8 | 531 | close (mbfi); |
2e82e3c3 | 532 | return (1); |
237e0016 | 533 | } |
b1ce62a8 RS |
534 | mbx_delimit_end (mbf); |
535 | fflush (mbf); | |
2e82e3c3 RS |
536 | if (ferror (mbf)) |
537 | { | |
538 | error ("Error in fflush: %s", strerror (errno)); | |
539 | pop_close (server); | |
540 | close (mbfi); | |
541 | return (1); | |
542 | } | |
237e0016 RS |
543 | } |
544 | ||
2e82e3c3 RS |
545 | /* On AFS, a call to write only modifies the file in the local |
546 | * workstation's AFS cache. The changes are not written to the server | |
547 | * until a call to fsync or close is made. Users with AFS home | |
548 | * directories have lost mail when over quota because these checks were | |
549 | * not made in previous versions of movemail. */ | |
550 | ||
340ff9de | 551 | #ifdef BSD |
cfa191ff RS |
552 | if (fsync (mbfi) < 0) |
553 | { | |
2e82e3c3 RS |
554 | error ("Error in fsync: %s", strerror (errno)); |
555 | return (1); | |
cfa191ff | 556 | } |
340ff9de | 557 | #endif |
cfa191ff RS |
558 | |
559 | if (close (mbfi) == -1) | |
560 | { | |
2e82e3c3 RS |
561 | error ("Error in close: %s", strerror (errno)); |
562 | return (1); | |
cfa191ff RS |
563 | } |
564 | ||
b1ce62a8 RS |
565 | for (i = 1; i <= nmsgs; i++) |
566 | { | |
2e82e3c3 | 567 | if (pop_delete (server, i)) |
b1ce62a8 | 568 | { |
2e82e3c3 RS |
569 | error (pop_error); |
570 | pop_close (server); | |
571 | return (1); | |
237e0016 RS |
572 | } |
573 | } | |
574 | ||
2e82e3c3 | 575 | if (pop_quit (server)) |
b1ce62a8 | 576 | { |
2e82e3c3 RS |
577 | error (pop_error); |
578 | return (1); | |
237e0016 | 579 | } |
237e0016 | 580 | |
2e82e3c3 | 581 | return (0); |
237e0016 RS |
582 | } |
583 | ||
2e82e3c3 | 584 | pop_retr (server, msgno, action, arg) |
b32701a7 | 585 | popserver server; |
b1ce62a8 | 586 | int (*action)(); |
237e0016 | 587 | { |
2e82e3c3 RS |
588 | extern char *strerror (); |
589 | char *line; | |
590 | int ret; | |
237e0016 | 591 | |
2e82e3c3 | 592 | if (pop_retrieve_first (server, msgno, &line)) |
b1ce62a8 | 593 | { |
2e82e3c3 RS |
594 | strncpy (Errmsg, pop_error, sizeof (Errmsg)); |
595 | Errmsg[sizeof (Errmsg)-1] = '\0'; | |
596 | return (NOTOK); | |
237e0016 RS |
597 | } |
598 | ||
2e82e3c3 | 599 | while (! (ret = pop_retrieve_next (server, &line))) |
b1ce62a8 | 600 | { |
2e82e3c3 RS |
601 | if (! line) |
602 | break; | |
603 | ||
604 | if ((*action)(line, arg) != OK) | |
b1ce62a8 | 605 | { |
2e82e3c3 RS |
606 | strcpy (Errmsg, strerror (errno)); |
607 | pop_close (server); | |
608 | return (NOTOK); | |
237e0016 RS |
609 | } |
610 | } | |
237e0016 | 611 | |
2e82e3c3 | 612 | if (ret) |
b1ce62a8 | 613 | { |
2e82e3c3 RS |
614 | strncpy (Errmsg, pop_error, sizeof (Errmsg)); |
615 | Errmsg[sizeof (Errmsg)-1] = '\0'; | |
616 | return (NOTOK); | |
237e0016 RS |
617 | } |
618 | ||
2e82e3c3 | 619 | return (OK); |
237e0016 RS |
620 | } |
621 | ||
2e82e3c3 RS |
622 | /* Do this as a macro instead of using strcmp to save on execution time. */ |
623 | #define IS_FROM_LINE(a) ((a[0] == 'F') \ | |
624 | && (a[1] == 'r') \ | |
625 | && (a[2] == 'o') \ | |
626 | && (a[3] == 'm') \ | |
627 | && (a[4] == ' ')) | |
237e0016 | 628 | |
2e82e3c3 | 629 | int |
b1ce62a8 RS |
630 | mbx_write (line, mbf) |
631 | char *line; | |
632 | FILE *mbf; | |
237e0016 | 633 | { |
2e82e3c3 RS |
634 | if (IS_FROM_LINE (line)) |
635 | { | |
636 | if (fputc ('>', mbf) == EOF) | |
637 | return (NOTOK); | |
638 | } | |
639 | if (fputs (line, mbf) == EOF) | |
640 | return (NOTOK); | |
641 | if (fputc (0x0a, mbf) == EOF) | |
642 | return (NOTOK); | |
643 | return (OK); | |
237e0016 RS |
644 | } |
645 | ||
2e82e3c3 | 646 | int |
b1ce62a8 RS |
647 | mbx_delimit_begin (mbf) |
648 | FILE *mbf; | |
237e0016 | 649 | { |
2e82e3c3 RS |
650 | if (fputs ("\f\n0, unseen,,\n", mbf) == EOF) |
651 | return (NOTOK); | |
652 | return (OK); | |
237e0016 RS |
653 | } |
654 | ||
b1ce62a8 RS |
655 | mbx_delimit_end (mbf) |
656 | FILE *mbf; | |
237e0016 | 657 | { |
2e82e3c3 RS |
658 | if (putc ('\037', mbf) == EOF) |
659 | return (NOTOK); | |
660 | return (OK); | |
237e0016 RS |
661 | } |
662 | ||
663 | #endif /* MAIL_USE_POP */ | |
e5f7ea68 RM |
664 | \f |
665 | #ifndef HAVE_STRERROR | |
666 | char * | |
667 | strerror (errnum) | |
668 | int errnum; | |
669 | { | |
670 | extern char *sys_errlist[]; | |
671 | extern int sys_nerr; | |
672 | ||
673 | if (errnum >= 0 && errnum < sys_nerr) | |
674 | return sys_errlist[errnum]; | |
675 | return (char *) "Unknown error"; | |
676 | } | |
677 | ||
678 | #endif /* ! HAVE_STRERROR */ |