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