428
|
1 /* movemail foo bar -- move file foo to file bar,
|
438
|
2 locking file foo.
|
428
|
3 Copyright (C) 1986, 1992, 1993, 1994, 1996 Free Software Foundation, Inc.
|
|
4
|
438
|
5 This file is part of XEmacs.
|
428
|
6
|
438
|
7 XEmacs is free software; you can redistribute it and/or modify it
|
|
8 under the terms of the GNU General Public License as published by the
|
|
9 Free Software Foundation; either version 2, or (at your option) any
|
|
10 later version.
|
428
|
11
|
438
|
12 XEmacs is distributed in the hope that it will be useful, but WITHOUT
|
|
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
15 for more details.
|
428
|
16
|
|
17 You should have received a copy of the GNU General Public License
|
438
|
18 along with XEmacs; see the file COPYING. If not, write to
|
428
|
19 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
438
|
20 Boston, MA 02111-1307, USA.
|
|
21
|
|
22 Please mail bugs and suggestions to the XEmacs maintainer.
|
|
23 */
|
428
|
24
|
438
|
25 /* Important notice:
|
|
26 *
|
|
27 * You *must* coordinate the locking method used by movemail with that
|
|
28 * used by your mail delivery agent, as well as that of the other mail
|
|
29 * user agents on your system. movemail allows you to do this at run
|
|
30 * time via the -m flag. Moreover, it uses a default determined by
|
|
31 * the MAIL_LOCK_DOT, MAIL_LOCK_LOCKF, MAIL_LOCK_FLOCK,
|
|
32 * MAIL_LOCK_LOCKING, and MAIL_LOCK_MMDF preprocessor settings.
|
|
33 */
|
428
|
34
|
438
|
35 /*
|
|
36 * Mike Sperber <sperber@informatik.uni-tuebingen.de> reorganized
|
|
37 * everything that has to with locking in December 1999.
|
|
38 */
|
428
|
39
|
|
40 /*
|
|
41 * Modified January, 1986 by Michael R. Gretzinger (Project Athena)
|
|
42 *
|
|
43 * Added POP (Post Office Protocol) service. When compiled -DMAIL_USE_POP
|
|
44 * movemail will accept input filename arguments of the form
|
|
45 * "po:username". This will cause movemail to open a connection to
|
|
46 * a pop server running on $MAILHOST (environment variable). Movemail
|
|
47 * must be setuid to root in order to work with POP.
|
|
48 *
|
|
49 * New module: popmail.c
|
|
50 * Modified routines:
|
|
51 * main - added code within #ifdef MAIL_USE_POP; added setuid (getuid ())
|
|
52 * after POP code.
|
|
53 * New routines in movemail.c:
|
|
54 * get_errmsg - return pointer to system error message
|
|
55 *
|
|
56 * Modified August, 1993 by Jonathan Kamens (OpenVision Technologies)
|
|
57 *
|
|
58 * Move all of the POP code into a separate file, "pop.c".
|
|
59 * Use strerror instead of get_errmsg.
|
|
60 *
|
|
61 */
|
|
62
|
|
63 #define NO_SHORTNAMES /* Tell config not to load remap.h */
|
438
|
64 #include <config.h>
|
428
|
65 #include <sys/types.h>
|
|
66 #include <sys/stat.h>
|
|
67 #include <stdio.h>
|
|
68 #include <errno.h>
|
|
69 #include "../src/sysfile.h"
|
|
70 #include "../src/syswait.h"
|
442
|
71 #ifndef WIN32_NATIVE
|
428
|
72 #include "../src/systime.h"
|
|
73 #endif
|
|
74 #include <stdlib.h>
|
|
75 #include <string.h>
|
|
76 #include "getopt.h"
|
|
77 #ifdef MAIL_USE_POP
|
|
78 #include "pop.h"
|
|
79 #include "../src/regex.h"
|
|
80 #endif
|
|
81
|
|
82 extern char *optarg;
|
|
83 extern int optind, opterr;
|
|
84
|
|
85 #ifndef HAVE_STRERROR
|
|
86 char * strerror (int errnum);
|
|
87 #endif /* HAVE_STRERROR */
|
|
88
|
|
89 #ifndef DIRECTORY_SEP
|
|
90 #define DIRECTORY_SEP '/'
|
|
91 #endif
|
|
92 #ifndef IS_DIRECTORY_SEP
|
|
93 #define IS_DIRECTORY_SEP(_c_) ((_c_) == DIRECTORY_SEP)
|
|
94 #endif
|
|
95
|
442
|
96 #ifdef WIN32_NATIVE
|
428
|
97 #undef access
|
|
98 #undef unlink
|
|
99 #define fork() 0
|
|
100 #define sys_wait(var) (*(var) = 0)
|
|
101 /* Unfortunately, Samba doesn't seem to properly lock Unix files even
|
|
102 though the locking call succeeds (and indeed blocks local access from
|
|
103 other NT programs). If you have direct file access using an NFS
|
|
104 client or something other than Samba, the locking call might work
|
|
105 properly - make sure it does before you enable this! */
|
|
106 #define DISABLE_DIRECT_ACCESS
|
|
107 #include <io.h>
|
442
|
108 #endif /* WIN32_NATIVE */
|
428
|
109
|
438
|
110 #if defined (HAVE_UNISTD_H)
|
428
|
111 #include <unistd.h>
|
|
112 #endif /* unistd.h */
|
|
113 #ifndef F_OK
|
|
114 #define F_OK 0
|
|
115 #define X_OK 1
|
|
116 #define W_OK 2
|
|
117 #define R_OK 4
|
|
118 #endif /* No F_OK */
|
|
119
|
438
|
120 #if defined (HAVE_FCNTL_H)
|
428
|
121 #include <fcntl.h>
|
|
122 #endif /* fcntl.h */
|
|
123
|
438
|
124 #ifdef HAVE_LOCKING
|
428
|
125 #include <sys/locking.h>
|
|
126 #endif
|
|
127
|
438
|
128 #ifdef HAVE_MMDF
|
428
|
129 extern int lk_open (), lk_close ();
|
|
130 #endif
|
|
131
|
|
132 /* Cancel substitutions made by config.h for Emacs. */
|
|
133 #undef open
|
|
134 #undef read
|
|
135 #undef write
|
|
136 #undef close
|
|
137
|
|
138 static void fatal (char *, char*);
|
|
139 static void error (char *, char *, char *);
|
438
|
140 static void usage(int);
|
428
|
141 static void pfatal_with_name (char *);
|
|
142 static void pfatal_and_delete (char *);
|
|
143 static char *concat (char *, char *, char *);
|
647
|
144 static long *xmalloc (int);
|
428
|
145 #ifdef MAIL_USE_POP
|
|
146 static int popmail (char *, char *, char *);
|
|
147 static int pop_retr (popserver server, int msgno,
|
440
|
148 int (*action)(char *, FILE *), FILE *arg);
|
428
|
149 static int mbx_write (char *, FILE *);
|
|
150 static int mbx_delimit_begin (FILE *);
|
|
151 static int mbx_delimit_end (FILE *);
|
|
152 static struct re_pattern_buffer* compile_regex (char* regexp_pattern);
|
|
153 static int pop_search_top (popserver server, int msgno, int lines,
|
|
154 struct re_pattern_buffer* regexp);
|
|
155 #endif
|
|
156
|
|
157 int verbose=0;
|
|
158 #ifdef MAIL_USE_POP
|
|
159 int reverse=0;
|
|
160 int keep_messages=0;
|
|
161 struct re_pattern_buffer* regexp_pattern=0;
|
|
162 int match_lines=10;
|
|
163 #endif
|
|
164
|
|
165 #define VERBOSE(x) if (verbose) { printf x; fflush(stdout); }
|
|
166
|
|
167 struct option longopts[] =
|
|
168 {
|
|
169 { "inbox", required_argument, NULL, 'i' },
|
|
170 { "outfile", required_argument, NULL, 'o' },
|
|
171 #ifdef MAIL_USE_POP
|
|
172 { "password", required_argument, NULL, 'p' },
|
|
173 { "reverse-pop-order", no_argument, NULL, 'x' },
|
|
174 { "keep-messages", no_argument, NULL, 'k' },
|
|
175 { "regex", required_argument, NULL, 'r' },
|
|
176 { "match-lines", required_argument, NULL, 'l' },
|
|
177 #endif
|
438
|
178 { "lock-method", required_argument, NULL, 'm' },
|
|
179 { "help", no_argument, NULL, 'h' },
|
428
|
180 { "verbose", no_argument, NULL, 'v' },
|
|
181 { 0 }
|
|
182 };
|
|
183
|
438
|
184 #define DOTLOCKING 0
|
|
185 #define FLOCKING 1
|
|
186 #define LOCKFING 2
|
|
187 #define MMDF 3
|
|
188 #define LOCKING 4
|
|
189
|
|
190 #if defined(MAIL_LOCK_FLOCK) && defined(HAVE_FLOCK)
|
|
191 #define DEFAULT_LOCKING FLOCKING
|
|
192 #elif defined(MAIL_LOCK_LOCKF) && defined(HAVE_LOCKF)
|
|
193 #define DEFAULT_LOCKING LOCKFING
|
|
194 #elif defined(MAIL_LOCK_MMDF) && defined(HAVE_MMDF)
|
|
195 #define DEFAULT_LOCKING MMDF
|
|
196 #elif defined(MAIL_LOCK_LOCKING) && defined(HAVE_LOCKING)
|
|
197 #define DEFAULT_LOCKING LOCKING
|
|
198 #else
|
|
199 #define DEFAULT_LOCKING DOTLOCKING
|
|
200 #endif
|
|
201
|
442
|
202 #ifndef DISABLE_DIRECT_ACCESS
|
438
|
203 static void lock_dot(char *);
|
442
|
204 #endif
|
438
|
205 static void unlock_dot(char *);
|
|
206 static int parse_lock_method(char *);
|
|
207 static char *unparse_lock_method(int);
|
|
208
|
428
|
209 int
|
|
210 main (int argc, char *argv[])
|
|
211 {
|
|
212 char *inname=0, *outname=0, *poppass=0;
|
|
213 #ifndef DISABLE_DIRECT_ACCESS
|
|
214 int indesc, outdesc;
|
|
215 int nread;
|
|
216 int status;
|
|
217 #endif
|
|
218
|
438
|
219 int lock_method = DEFAULT_LOCKING;
|
|
220
|
|
221 char *maybe_lock_env;
|
428
|
222
|
438
|
223 maybe_lock_env = getenv("EMACSLOCKMETHOD");
|
|
224 if (maybe_lock_env)
|
|
225 {
|
|
226 printf("maybe-lock_env: %s\n", maybe_lock_env);
|
|
227 lock_method = parse_lock_method(maybe_lock_env);
|
|
228 }
|
428
|
229
|
438
|
230 for (;;)
|
428
|
231 {
|
|
232 #ifdef MAIL_USE_POP
|
438
|
233 char* optstring = "i:o:m:p:l:r:xvhk";
|
428
|
234 #else
|
438
|
235 char* optstring = "i:o:m:vh";
|
428
|
236 #endif
|
|
237 int opt = getopt_long (argc, argv, optstring, longopts, 0);
|
|
238
|
|
239 if (opt == EOF)
|
|
240 break;
|
|
241
|
|
242 switch (opt)
|
|
243 {
|
|
244 case 0:
|
|
245 break;
|
|
246 case 1: /* one of the standard arguments seen */
|
|
247 if (!inname)
|
|
248 inname = optarg;
|
|
249 else if (!outname)
|
|
250 outname = optarg;
|
|
251 else
|
|
252 poppass = optarg;
|
|
253 break;
|
|
254
|
|
255 case 'i': /* infile */
|
|
256 inname = optarg;
|
|
257 break;
|
|
258
|
|
259 case 'o': /* outfile */
|
|
260 outname = optarg;
|
|
261 break;
|
|
262 #ifdef MAIL_USE_POP
|
|
263 case 'p': /* pop password */
|
|
264 poppass = optarg;
|
|
265 break;
|
|
266 case 'k': keep_messages=1; break;
|
|
267 case 'x': reverse = 1; break;
|
|
268 case 'l': /* lines to match */
|
|
269 match_lines = atoi (optarg);
|
|
270 break;
|
|
271
|
|
272 case 'r': /* regular expression */
|
|
273 regexp_pattern = compile_regex (optarg);
|
|
274 break;
|
|
275 #endif
|
438
|
276
|
|
277 case 'm':
|
|
278 lock_method = parse_lock_method(optarg);
|
|
279 break;
|
|
280 case 'h':
|
|
281 usage(lock_method);
|
|
282 exit(0);
|
|
283 case 'v':
|
|
284 verbose = 1;
|
|
285 break;
|
428
|
286 }
|
|
287 }
|
|
288
|
|
289 while (optind < argc)
|
|
290 {
|
|
291 if (!inname)
|
|
292 inname = argv[optind];
|
|
293 else if (!outname)
|
|
294 outname = argv[optind];
|
|
295 else
|
|
296 poppass = argv[optind];
|
|
297 optind++;
|
|
298 }
|
|
299
|
|
300 if (!inname || !outname)
|
|
301 {
|
438
|
302 usage(lock_method);
|
428
|
303 exit(1);
|
|
304 }
|
|
305
|
438
|
306 #ifdef HAVE_MMDF
|
|
307 if (lock_method == MMDF)
|
|
308 mmdf_init (argv[0]);
|
428
|
309 #endif
|
|
310
|
|
311 if (*outname == 0)
|
|
312 fatal ("Destination file name is empty", 0);
|
|
313
|
438
|
314 VERBOSE(("checking access to output file\n"));
|
428
|
315 /* Check access to output file. */
|
|
316 if (access (outname, F_OK) == 0 && access (outname, W_OK) != 0)
|
|
317 pfatal_with_name (outname);
|
|
318
|
|
319 /* Also check that outname's directory is writable to the real uid. */
|
|
320 {
|
|
321 char *buf = (char *) xmalloc (strlen (outname) + 1);
|
|
322 char *cp;
|
|
323 strcpy (buf, outname);
|
|
324 cp = buf + strlen (buf);
|
|
325 while (cp > buf && !IS_DIRECTORY_SEP (cp[-1]))
|
|
326 *--cp = 0;
|
|
327 if (cp == buf)
|
|
328 *cp++ = '.';
|
|
329 if (access (buf, W_OK) != 0)
|
|
330 pfatal_with_name (buf);
|
|
331 free (buf);
|
|
332 }
|
|
333
|
|
334 #ifdef MAIL_USE_POP
|
|
335 if (!strncmp (inname, "po:", 3))
|
|
336 {
|
|
337 int retcode = popmail (inname + 3, outname, poppass);
|
|
338 exit (retcode);
|
|
339 }
|
|
340
|
442
|
341 #ifndef WIN32_NATIVE
|
428
|
342 setuid (getuid ());
|
|
343 #endif
|
|
344 #endif /* MAIL_USE_POP */
|
|
345
|
|
346 #ifndef DISABLE_DIRECT_ACCESS
|
|
347
|
|
348 /* Check access to input file. */
|
|
349 if (access (inname, R_OK | W_OK) != 0)
|
|
350 pfatal_with_name (inname);
|
|
351
|
438
|
352
|
|
353 if (fork () == 0)
|
|
354 {
|
|
355 setuid (getuid ());
|
|
356
|
|
357 VERBOSE(("opening input file\n"));
|
|
358
|
|
359 switch (lock_method)
|
|
360 {
|
|
361 case DOTLOCKING:
|
|
362 indesc = open (inname, O_RDONLY);
|
|
363 break;
|
|
364 #ifdef HAVE_LOCKF
|
|
365 case LOCKFING:
|
|
366 indesc = open (inname, O_RDWR);
|
|
367 break;
|
|
368 #endif
|
|
369 #ifdef HAVE_FLOCK
|
|
370 case FLOCKING:
|
|
371 indesc = open (inname, O_RDWR);
|
|
372 break;
|
|
373 #endif
|
|
374 #ifdef HAVE_LOCKING
|
|
375 case LOCKING:
|
|
376 indesc = open (inname, O_RDWR);
|
|
377 break;
|
|
378 #endif
|
|
379 #ifdef HAVE_MMDF
|
|
380 case MMDF:
|
|
381 indesc = lk_open (inname, O_RDONLY, 0, 0, 10);
|
|
382 break;
|
|
383 #endif
|
|
384 default: abort();
|
|
385 }
|
|
386
|
|
387 if (indesc < 0)
|
|
388 pfatal_with_name (inname);
|
|
389
|
|
390 #ifdef HAVE_UMASK
|
|
391 /* In case movemail is setuid to root, make sure the user can
|
|
392 read the output file. */
|
|
393 umask (umask (0) & 0333);
|
|
394 #endif
|
|
395
|
|
396 outdesc = open (outname, O_WRONLY | O_CREAT | O_EXCL, 0666);
|
|
397 if (outdesc < 0)
|
|
398 pfatal_with_name (outname);
|
|
399
|
|
400 VERBOSE(("locking input file\n"));
|
428
|
401
|
438
|
402 switch (lock_method)
|
|
403 {
|
|
404 #ifdef HAVE_LOCKF
|
|
405 case LOCKFING:
|
|
406 if (lockf (indesc, F_LOCK, 0) < 0)
|
|
407 pfatal_with_name (inname);
|
|
408 break;
|
|
409 #endif
|
|
410 #ifdef HAVE_FLOCK
|
|
411 case FLOCKING:
|
|
412 if (flock (indesc, LOCK_EX) < 0)
|
|
413 pfatal_with_name (inname);
|
|
414 break;
|
|
415 #endif
|
|
416 #ifdef HAVE_LOCKING
|
|
417 case LOCKING:
|
|
418 if (locking (indesc, LK_RLCK, -1L) < 0)
|
|
419 pfatal_with_name (inname);
|
|
420 break;
|
|
421 #endif
|
|
422 case DOTLOCKING:
|
|
423 lock_dot(inname);
|
|
424 break;
|
|
425 }
|
|
426
|
|
427 VERBOSE(("copying input file to output file\n"));
|
|
428
|
|
429 {
|
|
430 char buf[1024];
|
|
431
|
|
432 while (1)
|
|
433 {
|
|
434 nread = read (indesc, buf, sizeof buf);
|
|
435 if (nread != write (outdesc, buf, nread))
|
|
436 {
|
|
437 int saved_errno = errno;
|
|
438 unlink (outname);
|
|
439 errno = saved_errno;
|
|
440 pfatal_with_name (outname);
|
|
441 }
|
647
|
442 if (nread < (int) sizeof (buf))
|
438
|
443 break;
|
|
444 }
|
|
445 }
|
|
446
|
|
447 #ifdef HAVE_FSYNC
|
|
448 if (fsync (outdesc) < 0)
|
|
449 pfatal_and_delete (outname);
|
|
450 #endif
|
|
451
|
|
452 /* Check to make sure no errors before we zap the inbox. */
|
|
453 if (close (outdesc) != 0)
|
|
454 pfatal_and_delete (outname);
|
|
455
|
|
456 VERBOSE(("deleting or truncating input file\n"));
|
428
|
457
|
438
|
458 switch (lock_method)
|
|
459 {
|
|
460 case LOCKFING:
|
|
461 case FLOCKING:
|
|
462 case LOCKING:
|
|
463 #ifdef HAVE_FTRUNCATE
|
|
464 ftruncate (indesc, 0L);
|
|
465 #else
|
|
466 close (open (inname, O_CREAT | O_TRUNC | O_RDWR, 0666));
|
|
467 #endif
|
|
468 close (indesc);
|
|
469 break;
|
|
470 #ifdef HAVE_MMDF
|
|
471 case MMDF:
|
|
472 lk_close (indesc, 0, 0, 0);
|
|
473 break;
|
|
474 #endif
|
|
475 case DOTLOCKING:
|
|
476 creat (inname, 0600);
|
|
477 break;
|
|
478 }
|
|
479
|
|
480 exit (0);
|
|
481 }
|
428
|
482
|
438
|
483 wait (&status);
|
|
484 if (!WIFEXITED (status))
|
|
485 exit (1);
|
|
486 else if (WEXITSTATUS (status) != 0)
|
|
487 exit (WEXITSTATUS (status));
|
|
488
|
|
489 if (lock_method == DOTLOCKING)
|
|
490 unlock_dot(inname);
|
|
491
|
|
492 #endif /* not DISABLE_DIRECT_ACCESS */
|
|
493
|
|
494 return 0;
|
|
495 }
|
|
496
|
|
497 static void
|
|
498 usage(int lock_method)
|
|
499 {
|
|
500 printf ("Usage: movemail [-rvxkh] [-l lines ] [-m method ] [-i] inbox [-o] destfile [[-p] POP-password]\n");
|
|
501 printf("where method is one of: dot");
|
|
502 #ifdef HAVE_LOCKF
|
|
503 printf(", lockf");
|
|
504 #endif
|
|
505 #ifdef HAVE_FLOCK
|
|
506 printf(", flock");
|
|
507 #endif
|
|
508 #ifdef HAVE_MMDF
|
|
509 printf(", mmdf");
|
|
510 #endif
|
|
511 #ifdef HAVE_LOCKING
|
|
512 printf(", locking");
|
|
513 #endif
|
|
514 printf("\nDefault is: %s\n", unparse_lock_method(lock_method));
|
|
515
|
|
516 }
|
428
|
517
|
438
|
518 static char *
|
|
519 unparse_lock_method(int lock_method)
|
|
520 {
|
|
521 switch (lock_method)
|
|
522 {
|
|
523 case DOTLOCKING: return "dot";
|
|
524 case FLOCKING: return "flock";
|
|
525 case LOCKFING: return "lockf";
|
|
526 case LOCKING: return "locking";
|
|
527 case MMDF: return "mmdf";
|
|
528 default: abort();return 0;
|
|
529 }
|
|
530 }
|
428
|
531
|
438
|
532 static int
|
|
533 parse_lock_method(char *method_name)
|
|
534 {
|
|
535 if (!strcmp("dot", method_name) || !strcmp("file", method_name))
|
|
536 return DOTLOCKING;
|
|
537 #ifdef HAVE_LOCKF
|
|
538 else if (!strcmp("lockf", method_name))
|
|
539 return LOCKFING;
|
|
540 #endif
|
|
541 #ifdef HAVE_FLOCK
|
|
542 else if (!strcmp("flock", method_name))
|
|
543 return FLOCKING;
|
|
544 #endif
|
|
545 #ifdef HAVE_MMDF
|
|
546 else if (!strcmp("mmdf", method_name))
|
|
547 return MMDF;
|
|
548 #endif
|
|
549 #ifdef HAVE_LOCKING
|
|
550 else if (!strcmp("locking", method_name))
|
|
551 return LOCKING;
|
|
552 #endif
|
|
553 else
|
|
554 fatal("invalid lock method: %s", method_name);
|
|
555 return 0; /* unreached */
|
|
556 }
|
|
557
|
|
558 static char *
|
|
559 dot_filename(char *filename)
|
|
560 {
|
|
561 return concat (filename, ".lock", "");
|
|
562 }
|
|
563
|
|
564 static char *dotlock_filename = NULL;
|
|
565
|
442
|
566 #ifndef DISABLE_DIRECT_ACCESS
|
438
|
567 static void
|
|
568 lock_dot(char *filename)
|
|
569 {
|
|
570 struct stat st;
|
|
571 long now;
|
|
572 int tem;
|
|
573 char *lockname, *p;
|
|
574 char *tempname;
|
|
575 int desc;
|
|
576
|
|
577 dotlock_filename = (char *) xmalloc(strlen(filename) + 1);
|
|
578
|
|
579 /* Use a lock file named after our first argument with .lock appended:
|
|
580 If it exists, the mail file is locked. */
|
|
581
|
|
582 lockname = dot_filename(filename);
|
|
583 tempname = (char *) xmalloc (strlen (filename) + strlen ("EXXXXXX") + 1);
|
|
584 strcpy (tempname, filename);
|
428
|
585 p = tempname + strlen (tempname);
|
|
586 while (p != tempname && !IS_DIRECTORY_SEP (p[-1]))
|
|
587 p--;
|
|
588 *p = 0;
|
|
589 strcpy (p, "EXXXXXX");
|
567
|
590 #ifndef HAVE_MKSTEMP
|
428
|
591 mktemp (tempname);
|
|
592 unlink (tempname);
|
567
|
593 #endif
|
428
|
594
|
438
|
595 for (;;)
|
428
|
596 {
|
|
597 /* Create the lock file, but not under the lock file name. */
|
|
598 /* Give up if cannot do that. */
|
567
|
599 #ifdef HAVE_MKSTEMP
|
|
600 desc = mkstemp (tempname);
|
|
601 #else
|
428
|
602 desc = open (tempname, O_WRONLY | O_CREAT | O_EXCL, 0666);
|
567
|
603 #endif
|
428
|
604 if (desc < 0)
|
|
605 {
|
|
606 char *message = (char *) xmalloc (strlen (tempname) + 50);
|
|
607 sprintf (message, "%s--see source file lib-src/movemail.c",
|
|
608 tempname);
|
|
609 pfatal_with_name (message);
|
|
610 }
|
|
611 close (desc);
|
|
612
|
|
613 tem = link (tempname, lockname);
|
|
614 unlink (tempname);
|
|
615 if (tem >= 0)
|
|
616 break;
|
|
617 sleep (1);
|
|
618
|
|
619 /* If lock file is five minutes old, unlock it.
|
|
620 Five minutes should be good enough to cope with crashes
|
|
621 and wedgitude, and long enough to avoid being fooled
|
|
622 by time differences between machines. */
|
|
623 if (stat (lockname, &st) >= 0)
|
|
624 {
|
|
625 now = time (0);
|
|
626 if (st.st_ctime < now - 300)
|
|
627 unlink (lockname);
|
|
628 }
|
|
629 }
|
438
|
630 strcpy(dotlock_filename, filename);
|
|
631 }
|
442
|
632 #endif /* not DISABLE_DIRECT_ACCESS */
|
428
|
633
|
438
|
634 static void
|
|
635 unlock_dot(char *filename)
|
|
636 {
|
|
637 unlink(dot_filename(filename));
|
|
638 }
|
428
|
639
|
438
|
640 static void
|
|
641 maybe_unlock_dot(void)
|
|
642 {
|
|
643 if (dotlock_filename)
|
|
644 unlock_dot(dotlock_filename);
|
|
645 }
|
428
|
646
|
|
647 /* Print error message and exit. */
|
|
648
|
|
649 static void
|
|
650 fatal (char *s1, char *s2)
|
|
651 {
|
438
|
652 maybe_unlock_dot();
|
428
|
653 error (s1, s2, NULL);
|
|
654 exit (1);
|
|
655 }
|
|
656
|
|
657 /* Print error message. `s1' is printf control string, `s2' is arg for it. */
|
|
658
|
|
659 static void
|
|
660 error (char *s1, char *s2, char *s3)
|
|
661 {
|
|
662 fprintf (stderr, "movemail: ");
|
|
663 fprintf (stderr, s1, s2, s3);
|
|
664 fprintf (stderr, "\n");
|
|
665 }
|
|
666
|
|
667 static void
|
|
668 pfatal_with_name (char *name)
|
|
669 {
|
|
670 char *s = concat ("", strerror (errno), " for %s");
|
|
671 fatal (s, name);
|
|
672 }
|
|
673
|
|
674 static void
|
|
675 pfatal_and_delete (char *name)
|
|
676 {
|
|
677 char *s = concat ("", strerror (errno), " for %s");
|
|
678 unlink (name);
|
|
679 fatal (s, name);
|
|
680 }
|
|
681
|
|
682 /* Return a newly-allocated string whose contents concatenate those of s1, s2, s3. */
|
|
683
|
|
684 static char *
|
|
685 concat (char *s1, char *s2, char *s3)
|
|
686 {
|
|
687 int len1 = strlen (s1), len2 = strlen (s2), len3 = strlen (s3);
|
|
688 char *result = (char *) xmalloc (len1 + len2 + len3 + 1);
|
|
689
|
|
690 strcpy (result, s1);
|
|
691 strcpy (result + len1, s2);
|
|
692 strcpy (result + len1 + len2, s3);
|
|
693 *(result + len1 + len2 + len3) = 0;
|
|
694
|
|
695 return result;
|
|
696 }
|
|
697
|
|
698 /* Like malloc but get fatal error if memory is exhausted. */
|
|
699
|
|
700 static long *
|
647
|
701 xmalloc (int size)
|
428
|
702 {
|
|
703 long *result = (long *) malloc (size);
|
|
704 if (!result)
|
|
705 fatal ("virtual memory exhausted", 0);
|
|
706 return result;
|
|
707 }
|
438
|
708
|
428
|
709 /* This is the guts of the interface to the Post Office Protocol. */
|
|
710
|
|
711 #ifdef MAIL_USE_POP
|
|
712
|
442
|
713 #ifndef WIN32_NATIVE
|
428
|
714 #include <sys/socket.h>
|
|
715 #include <netinet/in.h>
|
|
716 #include <netdb.h>
|
|
717 #else
|
|
718 #undef _WINSOCKAPI_
|
|
719 #include <winsock.h>
|
|
720 #endif
|
|
721 #include <stdio.h>
|
442
|
722 #include "../src/syspwd.h"
|
428
|
723
|
|
724 #define POP_ERROR (-1)
|
|
725 #define POP_RETRIEVED (0)
|
|
726 #define POP_DONE (1)
|
|
727
|
|
728 char *progname;
|
|
729 FILE *sfi;
|
|
730 FILE *sfo;
|
|
731 char ibuffer[BUFSIZ];
|
|
732 char obuffer[BUFSIZ];
|
|
733 char Errmsg[80];
|
|
734
|
|
735 static int
|
|
736 popmail (char *user, char *outfile, char *password)
|
|
737 {
|
|
738 int nmsgs, nbytes;
|
|
739 register int i, idx;
|
|
740 int mbfi;
|
|
741 short* retrieved_list;
|
|
742 FILE *mbf;
|
|
743 popserver server;
|
|
744
|
438
|
745 VERBOSE(("opening server\n"));
|
428
|
746 server = pop_open (0, user, password, POP_NO_GETPASS);
|
|
747 if (! server)
|
|
748 {
|
2584
|
749 error ("%s", pop_error, NULL);
|
428
|
750 return (1);
|
|
751 }
|
|
752
|
438
|
753 VERBOSE(("stat'ing messages\n"));
|
428
|
754 if (pop_stat (server, &nmsgs, &nbytes))
|
|
755 {
|
2584
|
756 error ("%s", pop_error, NULL);
|
428
|
757 return (1);
|
|
758 }
|
|
759
|
|
760 if (!nmsgs)
|
|
761 {
|
|
762 VERBOSE(("closing server\n"));
|
|
763 pop_close (server);
|
|
764 return (0);
|
|
765 }
|
|
766
|
|
767 /* build a retrieved table */
|
|
768 retrieved_list = (short*) xmalloc (sizeof (short) * (nmsgs+1));
|
|
769 memset (retrieved_list, 0, sizeof (short) * (nmsgs+1));
|
|
770
|
|
771 mbfi = open (outfile, O_WRONLY | O_CREAT | O_EXCL, 0666);
|
|
772 if (mbfi < 0)
|
|
773 {
|
|
774 pop_close (server);
|
|
775 error ("Error in open: %s, %s", strerror (errno), outfile);
|
|
776 return (1);
|
|
777 }
|
442
|
778 #if !defined(CYGWIN) && !defined(WIN32_NATIVE)
|
440
|
779 fchown (mbfi, getuid (), (gid_t) -1);
|
428
|
780 #endif
|
|
781
|
|
782 if ((mbf = fdopen (mbfi, "wb")) == NULL)
|
|
783 {
|
|
784 pop_close (server);
|
|
785 error ("Error in fdopen: %s", strerror (errno), NULL);
|
|
786 close (mbfi);
|
|
787 unlink (outfile);
|
|
788 return (1);
|
|
789 }
|
|
790
|
|
791 for (idx = 0; idx < nmsgs; idx++)
|
|
792 {
|
|
793 i = reverse ? nmsgs - idx : idx + 1;
|
438
|
794 VERBOSE(("checking message %d \n", i));
|
428
|
795
|
|
796 if (!regexp_pattern
|
|
797 ||
|
|
798 pop_search_top (server, i, match_lines, regexp_pattern) == POP_RETRIEVED)
|
|
799 {
|
438
|
800 VERBOSE(("retrieving message %d \n", i));
|
428
|
801 mbx_delimit_begin (mbf);
|
|
802 if (pop_retr (server, i, mbx_write, mbf) != POP_RETRIEVED)
|
|
803 {
|
2584
|
804 error ("%s", Errmsg, NULL);
|
428
|
805 close (mbfi);
|
|
806 return (1);
|
|
807 }
|
|
808
|
|
809 retrieved_list[i]=1;
|
|
810
|
|
811 mbx_delimit_end (mbf);
|
|
812 fflush (mbf);
|
|
813 if (ferror (mbf))
|
|
814 {
|
|
815 error ("Error in fflush: %s", strerror (errno), NULL);
|
|
816 pop_close (server);
|
|
817 close (mbfi);
|
|
818 return (1);
|
|
819 }
|
|
820 }
|
|
821 }
|
|
822
|
|
823 /* On AFS, a call to write only modifies the file in the local
|
|
824 * workstation's AFS cache. The changes are not written to the server
|
|
825 * until a call to fsync or close is made. Users with AFS home
|
|
826 * directories have lost mail when over quota because these checks were
|
|
827 * not made in previous versions of movemail. */
|
|
828
|
438
|
829 #ifdef HAVE_FSYNC
|
428
|
830 if (fsync (mbfi) < 0)
|
|
831 {
|
|
832 error ("Error in fsync: %s", strerror (errno), NULL);
|
|
833 return (1);
|
|
834 }
|
|
835 #endif
|
|
836
|
|
837 if (close (mbfi) == -1)
|
|
838 {
|
|
839 error ("Error in close: %s", strerror (errno), NULL);
|
|
840 return (1);
|
|
841 }
|
|
842
|
|
843 if (!keep_messages)
|
|
844 {
|
|
845 for (i = 1; i <= nmsgs; i++)
|
|
846 {
|
|
847 if (retrieved_list[i] == 1)
|
|
848 {
|
438
|
849 VERBOSE(("deleting message %d \n", i));
|
428
|
850 if (pop_delete (server, i))
|
|
851 {
|
2584
|
852 error ("%s", pop_error, NULL);
|
428
|
853 pop_close (server);
|
|
854 return (1);
|
|
855 }
|
|
856 }
|
|
857 }
|
|
858 }
|
|
859
|
|
860 VERBOSE(("closing server \n"));
|
|
861 if (pop_quit (server))
|
|
862 {
|
2584
|
863 error ("%s", pop_error, NULL);
|
428
|
864 return (1);
|
|
865 }
|
|
866
|
|
867 return (0);
|
|
868 }
|
|
869
|
|
870 static int
|
440
|
871 pop_retr (popserver server, int msgno, int (*action)(char *, FILE *), FILE *arg)
|
428
|
872 {
|
|
873 char *line;
|
|
874 int ret;
|
|
875
|
|
876 if (pop_retrieve_first (server, msgno, &line))
|
|
877 {
|
|
878 strncpy (Errmsg, pop_error, sizeof (Errmsg));
|
|
879 Errmsg[sizeof (Errmsg)-1] = '\0';
|
|
880 return (POP_ERROR);
|
|
881 }
|
|
882
|
|
883 while (! (ret = pop_retrieve_next (server, &line)))
|
|
884 {
|
|
885 if (! line)
|
|
886 break;
|
|
887
|
|
888 if ((*action)(line, arg) != POP_RETRIEVED)
|
|
889 {
|
|
890 strcpy (Errmsg, strerror (errno));
|
|
891 pop_close (server);
|
|
892 return (POP_ERROR);
|
|
893 }
|
|
894 }
|
|
895
|
|
896 if (ret)
|
|
897 {
|
|
898 strncpy (Errmsg, pop_error, sizeof (Errmsg));
|
|
899 Errmsg[sizeof (Errmsg)-1] = '\0';
|
|
900 return (POP_ERROR);
|
|
901 }
|
|
902
|
|
903 return (POP_RETRIEVED);
|
|
904 }
|
|
905
|
|
906 /* search the top lines of each message looking for a match */
|
|
907 static int
|
|
908 pop_search_top (popserver server, int msgno, int lines, struct re_pattern_buffer* regexp)
|
|
909 {
|
|
910 char *line;
|
|
911 int ret;
|
|
912 int match = POP_DONE;
|
|
913
|
|
914 if (pop_top_first (server, msgno, lines, &line))
|
|
915 {
|
|
916 strncpy (Errmsg, pop_error, sizeof (Errmsg));
|
|
917 Errmsg[sizeof (Errmsg)-1] = '\0';
|
|
918 return (POP_ERROR);
|
|
919 }
|
|
920
|
|
921 while (! (ret = pop_top_next (server, &line)))
|
|
922 {
|
|
923 if (! line)
|
|
924 break;
|
|
925
|
|
926 /* VERBOSE (("checking %s\n", line));*/
|
|
927 if (match != POP_RETRIEVED)
|
|
928 {
|
|
929 if ((ret = re_match (regexp, line, strlen (line), 0, 0)) == -2 )
|
|
930 {
|
|
931 strcpy (Errmsg, "error in regular expression");
|
|
932 pop_close (server);
|
|
933 return (POP_ERROR);
|
|
934 }
|
|
935 else if (ret >=0)
|
|
936 {
|
|
937 match = POP_RETRIEVED;
|
|
938 }
|
|
939 }
|
|
940 }
|
|
941
|
|
942 if (ret)
|
|
943 {
|
|
944 strncpy (Errmsg, pop_error, sizeof (Errmsg));
|
|
945 Errmsg[sizeof (Errmsg)-1] = '\0';
|
|
946 return (POP_ERROR);
|
|
947 }
|
|
948
|
|
949 return match;
|
|
950 }
|
|
951
|
|
952 /* Do this as a macro instead of using strcmp to save on execution time. */
|
|
953 #define IS_FROM_LINE(a) ((a[0] == 'F') \
|
|
954 && (a[1] == 'r') \
|
|
955 && (a[2] == 'o') \
|
|
956 && (a[3] == 'm') \
|
|
957 && (a[4] == ' '))
|
|
958
|
|
959 static int
|
|
960 mbx_write (char *line, FILE *mbf)
|
|
961 {
|
|
962 if (IS_FROM_LINE (line))
|
|
963 {
|
|
964 if (fputc ('>', mbf) == EOF)
|
|
965 return (POP_ERROR);
|
|
966 }
|
|
967 if (fputs (line, mbf) == EOF)
|
|
968 return (POP_ERROR);
|
|
969 if (fputc (0x0a, mbf) == EOF)
|
|
970 return (POP_ERROR);
|
|
971 return (POP_RETRIEVED);
|
|
972 }
|
|
973
|
|
974 static int
|
|
975 mbx_delimit_begin (FILE *mbf)
|
|
976 {
|
|
977 if (fputs ("\f\n0, unseen,,\n", mbf) == EOF)
|
|
978 return (POP_ERROR);
|
|
979 return (POP_RETRIEVED);
|
|
980 }
|
|
981
|
|
982 static int
|
|
983 mbx_delimit_end (FILE *mbf)
|
|
984 {
|
|
985 if (putc ('\037', mbf) == EOF)
|
|
986 return (POP_ERROR);
|
|
987 return (POP_RETRIEVED);
|
|
988 }
|
|
989
|
|
990 /* Turn a name, which is an ed-style (but Emacs syntax) regular
|
|
991 expression, into a real regular expression by compiling it. */
|
|
992 static struct re_pattern_buffer*
|
|
993 compile_regex (char* pattern)
|
|
994 {
|
|
995 char *err;
|
|
996 struct re_pattern_buffer *patbuf=0;
|
|
997
|
|
998 patbuf = (struct re_pattern_buffer*) xmalloc (sizeof (struct re_pattern_buffer));
|
|
999 patbuf->translate = NULL;
|
|
1000 patbuf->fastmap = NULL;
|
|
1001 patbuf->buffer = NULL;
|
|
1002 patbuf->allocated = 0;
|
|
1003
|
|
1004 err = (char*) re_compile_pattern (pattern, strlen (pattern), patbuf);
|
|
1005 if (err != NULL)
|
|
1006 {
|
|
1007 error ("%s while compiling pattern", err, NULL);
|
|
1008 return 0;
|
|
1009 }
|
|
1010
|
|
1011 return patbuf;
|
|
1012 }
|
|
1013
|
|
1014
|
|
1015
|
|
1016 #endif /* MAIL_USE_POP */
|
438
|
1017
|
428
|
1018 #ifndef HAVE_STRERROR
|
|
1019 char *
|
|
1020 strerror (int errnum)
|
|
1021 {
|
|
1022 extern char *sys_errlist[];
|
|
1023 extern int sys_nerr;
|
|
1024
|
|
1025 if (errnum >= 0 && errnum < sys_nerr)
|
|
1026 return sys_errlist[errnum];
|
|
1027 return (char *) "Unknown error";
|
|
1028 }
|
|
1029
|
|
1030 #endif /* ! HAVE_STRERROR */
|