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