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