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