0
|
1 /* movemail foo bar -- move file foo to file bar,
|
412
|
2 locking file foo the way /bin/mail respects.
|
118
|
3 Copyright (C) 1986, 1992, 1993, 1994, 1996 Free Software Foundation, Inc.
|
0
|
4
|
412
|
5 This file is part of GNU Emacs.
|
0
|
6
|
412
|
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.
|
0
|
11
|
412
|
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.
|
0
|
16
|
|
17 You should have received a copy of the GNU General Public License
|
412
|
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,
|
412
|
20 Boston, MA 02111-1307, USA. */
|
0
|
21
|
412
|
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.
|
0
|
27
|
412
|
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. */
|
0
|
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 */
|
384
|
58 #define DONT_ENCAPSULATE
|
412
|
59 #include <../src/config.h>
|
0
|
60 #include <sys/types.h>
|
|
61 #include <sys/stat.h>
|
118
|
62 #include <stdio.h>
|
0
|
63 #include <errno.h>
|
384
|
64 #include "../src/sysfile.h"
|
157
|
65 #include "../src/syswait.h"
|
412
|
66 #ifndef WINDOWSNT
|
163
|
67 #include "../src/systime.h"
|
384
|
68 #endif
|
122
|
69 #include <stdlib.h>
|
|
70 #include <string.h>
|
377
|
71 #include "getopt.h"
|
118
|
72 #ifdef MAIL_USE_POP
|
|
73 #include "pop.h"
|
380
|
74 #include "../src/regex.h"
|
118
|
75 #endif
|
54
|
76
|
377
|
77 extern char *optarg;
|
|
78 extern int optind, opterr;
|
|
79
|
175
|
80 #ifndef HAVE_STRERROR
|
388
|
81 char * strerror (int errnum);
|
175
|
82 #endif /* HAVE_STRERROR */
|
|
83
|
412
|
84 #ifdef MSDOS
|
|
85 #undef access
|
|
86 #endif /* MSDOS */
|
|
87
|
118
|
88 #ifndef DIRECTORY_SEP
|
|
89 #define DIRECTORY_SEP '/'
|
|
90 #endif
|
|
91 #ifndef IS_DIRECTORY_SEP
|
|
92 #define IS_DIRECTORY_SEP(_c_) ((_c_) == DIRECTORY_SEP)
|
|
93 #endif
|
|
94
|
412
|
95 #ifdef WINDOWSNT
|
118
|
96 #undef access
|
|
97 #undef unlink
|
|
98 #define fork() 0
|
|
99 #define sys_wait(var) (*(var) = 0)
|
|
100 /* Unfortunately, Samba doesn't seem to properly lock Unix files even
|
|
101 though the locking call succeeds (and indeed blocks local access from
|
|
102 other NT programs). If you have direct file access using an NFS
|
|
103 client or something other than Samba, the locking call might work
|
|
104 properly - make sure it does before you enable this! */
|
|
105 #define DISABLE_DIRECT_ACCESS
|
288
|
106 #include <io.h>
|
412
|
107 #endif /* WINDOWSNT */
|
118
|
108
|
412
|
109 #if defined (HAVE_UNISTD_H) || defined (USG)
|
0
|
110 #include <unistd.h>
|
163
|
111 #endif /* unistd.h */
|
0
|
112 #ifndef F_OK
|
|
113 #define F_OK 0
|
|
114 #define X_OK 1
|
|
115 #define W_OK 2
|
|
116 #define R_OK 4
|
163
|
117 #endif /* No F_OK */
|
0
|
118
|
412
|
119 #if defined (HAVE_FCNTL_H) || defined (USG)
|
163
|
120 #include <fcntl.h>
|
|
121 #endif /* fcntl.h */
|
0
|
122
|
412
|
123 #if defined (XENIX) || defined (WINDOWSNT)
|
0
|
124 #include <sys/locking.h>
|
|
125 #endif
|
|
126
|
412
|
127 #ifdef MAIL_USE_LOCKF
|
|
128 #define MAIL_USE_SYSTEM_LOCK
|
|
129 #endif
|
|
130
|
|
131 #ifdef MAIL_USE_FLOCK
|
|
132 #define MAIL_USE_SYSTEM_LOCK
|
|
133 #endif
|
|
134
|
|
135 #ifdef MAIL_USE_MMDF
|
0
|
136 extern int lk_open (), lk_close ();
|
|
137 #endif
|
|
138
|
|
139 /* Cancel substitutions made by config.h for Emacs. */
|
|
140 #undef open
|
|
141 #undef read
|
|
142 #undef write
|
|
143 #undef close
|
|
144
|
173
|
145 static void fatal (char *, char*);
|
|
146 static void error (char *, char *, char *);
|
|
147 static void pfatal_with_name (char *);
|
|
148 static void pfatal_and_delete (char *);
|
|
149 static char *concat (char *, char *, char *);
|
|
150 static long *xmalloc (unsigned int);
|
213
|
151 #ifdef MAIL_USE_POP
|
173
|
152 static int popmail (char *, char *, char *);
|
424
|
153 static int pop_retr (popserver server, int msgno,
|
|
154 int (*action)(char *, FILE *), void *arg);
|
173
|
155 static int mbx_write (char *, FILE *);
|
|
156 static int mbx_delimit_begin (FILE *);
|
|
157 static int mbx_delimit_end (FILE *);
|
377
|
158 static struct re_pattern_buffer* compile_regex (char* regexp_pattern);
|
|
159 static int pop_search_top (popserver server, int msgno, int lines,
|
|
160 struct re_pattern_buffer* regexp);
|
213
|
161 #endif
|
0
|
162
|
412
|
163 /* Nonzero means this is name of a lock file to delete on fatal error. */
|
|
164 char *delete_lockname;
|
|
165
|
377
|
166 int verbose=0;
|
|
167 #ifdef MAIL_USE_POP
|
|
168 int reverse=0;
|
|
169 int keep_messages=0;
|
|
170 struct re_pattern_buffer* regexp_pattern=0;
|
|
171 int match_lines=10;
|
|
172 #endif
|
|
173
|
|
174 #define VERBOSE(x) if (verbose) { printf x; fflush(stdout); }
|
|
175
|
|
176 struct option longopts[] =
|
|
177 {
|
|
178 { "inbox", required_argument, NULL, 'i' },
|
|
179 { "outfile", required_argument, NULL, 'o' },
|
|
180 #ifdef MAIL_USE_POP
|
|
181 { "password", required_argument, NULL, 'p' },
|
|
182 { "reverse-pop-order", no_argument, NULL, 'x' },
|
|
183 { "keep-messages", no_argument, NULL, 'k' },
|
|
184 { "regex", required_argument, NULL, 'r' },
|
|
185 { "match-lines", required_argument, NULL, 'l' },
|
|
186 #endif
|
|
187 { "verbose", no_argument, NULL, 'v' },
|
|
188 { 0 }
|
|
189 };
|
|
190
|
118
|
191 int
|
173
|
192 main (int argc, char *argv[])
|
0
|
193 {
|
377
|
194 char *inname=0, *outname=0, *poppass=0;
|
288
|
195 #ifndef DISABLE_DIRECT_ACCESS
|
0
|
196 int indesc, outdesc;
|
|
197 int nread;
|
155
|
198 int status;
|
288
|
199 #endif
|
0
|
200
|
412
|
201 #ifndef MAIL_USE_SYSTEM_LOCK
|
|
202 struct stat st;
|
|
203 long now;
|
|
204 int tem;
|
|
205 char *lockname, *p;
|
|
206 char *tempname;
|
|
207 int desc;
|
|
208 #endif /* not MAIL_USE_SYSTEM_LOCK */
|
0
|
209
|
412
|
210 delete_lockname = 0;
|
0
|
211
|
412
|
212 while (1)
|
118
|
213 {
|
377
|
214 #ifdef MAIL_USE_POP
|
412
|
215 char* optstring = "i:o:p:l:r:xvk";
|
377
|
216 #else
|
412
|
217 char* optstring = "i:o:v";
|
377
|
218 #endif
|
|
219 int opt = getopt_long (argc, argv, optstring, longopts, 0);
|
|
220
|
|
221 if (opt == EOF)
|
|
222 break;
|
|
223
|
|
224 switch (opt)
|
|
225 {
|
|
226 case 0:
|
|
227 break;
|
|
228 case 1: /* one of the standard arguments seen */
|
|
229 if (!inname)
|
|
230 inname = optarg;
|
|
231 else if (!outname)
|
|
232 outname = optarg;
|
|
233 else
|
|
234 poppass = optarg;
|
|
235 break;
|
|
236
|
|
237 case 'i': /* infile */
|
|
238 inname = optarg;
|
|
239 break;
|
|
240
|
|
241 case 'o': /* outfile */
|
|
242 outname = optarg;
|
|
243 break;
|
|
244 #ifdef MAIL_USE_POP
|
|
245 case 'p': /* pop password */
|
|
246 poppass = optarg;
|
|
247 break;
|
|
248 case 'k': keep_messages=1; break;
|
|
249 case 'x': reverse = 1; break;
|
|
250 case 'l': /* lines to match */
|
|
251 match_lines = atoi (optarg);
|
|
252 break;
|
|
253
|
|
254 case 'r': /* regular expression */
|
|
255 regexp_pattern = compile_regex (optarg);
|
|
256 break;
|
|
257 #endif
|
412
|
258 case 'v': verbose = 1; break;
|
377
|
259 }
|
|
260 }
|
|
261
|
|
262 while (optind < argc)
|
|
263 {
|
|
264 if (!inname)
|
|
265 inname = argv[optind];
|
|
266 else if (!outname)
|
|
267 outname = argv[optind];
|
|
268 else
|
|
269 poppass = argv[optind];
|
|
270 optind++;
|
|
271 }
|
|
272
|
|
273 if (!inname || !outname)
|
|
274 {
|
412
|
275 fprintf (stderr, "Usage: movemail [-rvxk] [-l lines ] [-i] inbox [-o] destfile [[-p] POP-password]\n");
|
371
|
276 exit(1);
|
321
|
277 }
|
|
278
|
412
|
279 #ifdef MAIL_USE_MMDF
|
|
280 mmdf_init (argv[0]);
|
0
|
281 #endif
|
|
282
|
118
|
283 if (*outname == 0)
|
|
284 fatal ("Destination file name is empty", 0);
|
|
285
|
0
|
286 /* Check access to output file. */
|
|
287 if (access (outname, F_OK) == 0 && access (outname, W_OK) != 0)
|
|
288 pfatal_with_name (outname);
|
|
289
|
118
|
290 /* Also check that outname's directory is writable to the real uid. */
|
0
|
291 {
|
|
292 char *buf = (char *) xmalloc (strlen (outname) + 1);
|
173
|
293 char *cp;
|
0
|
294 strcpy (buf, outname);
|
173
|
295 cp = buf + strlen (buf);
|
|
296 while (cp > buf && !IS_DIRECTORY_SEP (cp[-1]))
|
|
297 *--cp = 0;
|
|
298 if (cp == buf)
|
|
299 *cp++ = '.';
|
0
|
300 if (access (buf, W_OK) != 0)
|
|
301 pfatal_with_name (buf);
|
|
302 free (buf);
|
|
303 }
|
|
304
|
|
305 #ifdef MAIL_USE_POP
|
118
|
306 if (!strncmp (inname, "po:", 3))
|
0
|
307 {
|
377
|
308 int retcode = popmail (inname + 3, outname, poppass);
|
272
|
309 exit (retcode);
|
0
|
310 }
|
|
311
|
412
|
312 #ifndef WINDOWSNT
|
0
|
313 setuid (getuid ());
|
384
|
314 #endif
|
0
|
315 #endif /* MAIL_USE_POP */
|
|
316
|
118
|
317 #ifndef DISABLE_DIRECT_ACCESS
|
|
318
|
0
|
319 /* Check access to input file. */
|
|
320 if (access (inname, R_OK | W_OK) != 0)
|
|
321 pfatal_with_name (inname);
|
|
322
|
412
|
323 #ifndef MAIL_USE_MMDF
|
|
324 #ifndef MAIL_USE_SYSTEM_LOCK
|
|
325 /* Use a lock file named after our first argument with .lock appended:
|
|
326 If it exists, the mail file is locked. */
|
|
327 /* Note: this locking mechanism is *required* by the mailer
|
|
328 (on systems which use it) to prevent loss of mail.
|
0
|
329
|
412
|
330 On systems that use a lock file, extracting the mail without locking
|
|
331 WILL occasionally cause loss of mail due to timing errors!
|
0
|
332
|
412
|
333 So, if creation of the lock file fails
|
|
334 due to access permission on the mail spool directory,
|
|
335 you simply MUST change the permission
|
|
336 and/or make movemail a setgid program
|
|
337 so it can create lock files properly.
|
0
|
338
|
412
|
339 You might also wish to verify that your system is one
|
|
340 which uses lock files for this purpose. Some systems use other methods.
|
0
|
341
|
412
|
342 If your system uses the `flock' system call for mail locking,
|
|
343 define MAIL_USE_SYSTEM_LOCK in config.h or the s-*.h file
|
|
344 and recompile movemail. If the s- file for your system
|
|
345 should define MAIL_USE_SYSTEM_LOCK but does not, send a bug report
|
|
346 to bug-gnu-emacs@prep.ai.mit.edu so we can fix it. */
|
0
|
347
|
412
|
348 lockname = concat (inname, ".lock", "");
|
|
349 tempname = (char *) xmalloc (strlen (inname) + strlen ("EXXXXXX") + 1);
|
|
350 strcpy (tempname, inname);
|
0
|
351 p = tempname + strlen (tempname);
|
118
|
352 while (p != tempname && !IS_DIRECTORY_SEP (p[-1]))
|
0
|
353 p--;
|
|
354 *p = 0;
|
|
355 strcpy (p, "EXXXXXX");
|
|
356 mktemp (tempname);
|
|
357 unlink (tempname);
|
|
358
|
412
|
359 while (1)
|
0
|
360 {
|
|
361 /* Create the lock file, but not under the lock file name. */
|
|
362 /* Give up if cannot do that. */
|
|
363 desc = open (tempname, O_WRONLY | O_CREAT | O_EXCL, 0666);
|
|
364 if (desc < 0)
|
118
|
365 {
|
|
366 char *message = (char *) xmalloc (strlen (tempname) + 50);
|
|
367 sprintf (message, "%s--see source file lib-src/movemail.c",
|
|
368 tempname);
|
|
369 pfatal_with_name (message);
|
|
370 }
|
0
|
371 close (desc);
|
|
372
|
|
373 tem = link (tempname, lockname);
|
|
374 unlink (tempname);
|
|
375 if (tem >= 0)
|
|
376 break;
|
|
377 sleep (1);
|
|
378
|
118
|
379 /* If lock file is five minutes old, unlock it.
|
|
380 Five minutes should be good enough to cope with crashes
|
|
381 and wedgitude, and long enough to avoid being fooled
|
|
382 by time differences between machines. */
|
0
|
383 if (stat (lockname, &st) >= 0)
|
|
384 {
|
|
385 now = time (0);
|
118
|
386 if (st.st_ctime < now - 300)
|
|
387 unlink (lockname);
|
0
|
388 }
|
|
389 }
|
412
|
390
|
|
391 delete_lockname = lockname;
|
|
392 #endif /* not MAIL_USE_SYSTEM_LOCK */
|
|
393 #endif /* not MAIL_USE_MMDF */
|
|
394
|
|
395 if (fork () == 0)
|
|
396 {
|
|
397 setuid (getuid ());
|
|
398
|
|
399 #ifndef MAIL_USE_MMDF
|
|
400 #ifdef MAIL_USE_SYSTEM_LOCK
|
|
401 indesc = open (inname, O_RDWR);
|
|
402 #else /* if not MAIL_USE_SYSTEM_LOCK */
|
|
403 indesc = open (inname, O_RDONLY);
|
|
404 #endif /* not MAIL_USE_SYSTEM_LOCK */
|
|
405 #else /* MAIL_USE_MMDF */
|
|
406 indesc = lk_open (inname, O_RDONLY, 0, 0, 10);
|
|
407 #endif /* MAIL_USE_MMDF */
|
|
408
|
|
409 if (indesc < 0)
|
|
410 pfatal_with_name (inname);
|
|
411
|
|
412 #if defined (BSD) || defined (XENIX)
|
|
413 /* In case movemail is setuid to root, make sure the user can
|
|
414 read the output file. */
|
|
415 /* This is desirable for all systems
|
|
416 but I don't want to assume all have the umask system call */
|
|
417 umask (umask (0) & 0333);
|
|
418 #endif /* BSD or Xenix */
|
|
419 outdesc = open (outname, O_WRONLY | O_CREAT | O_EXCL, 0666);
|
|
420 if (outdesc < 0)
|
|
421 pfatal_with_name (outname);
|
|
422 #ifdef MAIL_USE_SYSTEM_LOCK
|
|
423 #ifdef MAIL_USE_LOCKF
|
|
424 if (lockf (indesc, F_LOCK, 0) < 0) pfatal_with_name (inname);
|
|
425 #else /* not MAIL_USE_LOCKF */
|
|
426 #ifdef XENIX
|
|
427 if (locking (indesc, LK_RLCK, 0L) < 0) pfatal_with_name (inname);
|
|
428 #else
|
|
429 #ifdef WINDOWSNT
|
|
430 if (locking (indesc, LK_RLCK, -1L) < 0) pfatal_with_name (inname);
|
|
431 #else
|
|
432 if (flock (indesc, LOCK_EX) < 0) pfatal_with_name (inname);
|
|
433 #endif
|
|
434 #endif
|
|
435 #endif /* not MAIL_USE_LOCKF */
|
|
436 #endif /* MAIL_USE_SYSTEM_LOCK */
|
|
437
|
|
438 {
|
|
439 char buf[1024];
|
0
|
440
|
412
|
441 while (1)
|
|
442 {
|
|
443 nread = read (indesc, buf, sizeof buf);
|
|
444 if (nread != write (outdesc, buf, nread))
|
|
445 {
|
|
446 int saved_errno = errno;
|
|
447 unlink (outname);
|
|
448 errno = saved_errno;
|
|
449 pfatal_with_name (outname);
|
|
450 }
|
|
451 if (nread < sizeof buf)
|
|
452 break;
|
|
453 }
|
|
454 }
|
|
455
|
|
456 #ifdef BSD
|
|
457 if (fsync (outdesc) < 0)
|
|
458 pfatal_and_delete (outname);
|
|
459 #endif
|
|
460
|
|
461 /* Check to make sure no errors before we zap the inbox. */
|
|
462 if (close (outdesc) != 0)
|
|
463 pfatal_and_delete (outname);
|
|
464
|
|
465 #ifdef MAIL_USE_SYSTEM_LOCK
|
|
466 #if defined (STRIDE) || defined (XENIX) || defined (WINDOWSNT)
|
|
467 /* Stride, xenix have file locking, but no ftruncate. This mess will do. */
|
|
468 close (open (inname, O_CREAT | O_TRUNC | O_RDWR, 0666));
|
|
469 #else
|
|
470 ftruncate (indesc, 0L);
|
|
471 #endif /* STRIDE or XENIX */
|
|
472 #endif /* MAIL_USE_SYSTEM_LOCK */
|
|
473
|
|
474 #ifdef MAIL_USE_MMDF
|
|
475 lk_close (indesc, 0, 0, 0);
|
|
476 #else
|
|
477 close (indesc);
|
|
478 #endif
|
|
479
|
|
480 #ifndef MAIL_USE_SYSTEM_LOCK
|
|
481 /* Delete the input file; if we can't, at least get rid of its
|
|
482 contents. */
|
|
483 #ifdef MAIL_UNLINK_SPOOL
|
|
484 /* This is generally bad to do, because it destroys the permissions
|
|
485 that were set on the file. Better to just empty the file. */
|
|
486 if (unlink (inname) < 0 && errno != ENOENT)
|
|
487 #endif /* MAIL_UNLINK_SPOOL */
|
|
488 creat (inname, 0600);
|
|
489 #endif /* not MAIL_USE_SYSTEM_LOCK */
|
|
490
|
|
491 exit (0);
|
|
492 }
|
|
493
|
|
494 wait (&status);
|
|
495 if (!WIFEXITED (status))
|
|
496 exit (1);
|
|
497 else if (WEXITSTATUS (status) != 0)
|
|
498 exit (WEXITSTATUS (status));
|
|
499
|
|
500 #if !defined (MAIL_USE_MMDF) && !defined (MAIL_USE_SYSTEM_LOCK)
|
|
501 unlink (lockname);
|
|
502 #endif /* not MAIL_USE_MMDF and not MAIL_USE_SYSTEM_LOCK */
|
|
503
|
|
504 #endif /* ! DISABLE_DIRECT_ACCESS */
|
|
505
|
|
506 return 0;
|
398
|
507 }
|
412
|
508
|
0
|
509 /* Print error message and exit. */
|
|
510
|
173
|
511 static void
|
|
512 fatal (char *s1, char *s2)
|
0
|
513 {
|
412
|
514 if (delete_lockname)
|
|
515 unlink (delete_lockname);
|
173
|
516 error (s1, s2, NULL);
|
0
|
517 exit (1);
|
|
518 }
|
|
519
|
|
520 /* Print error message. `s1' is printf control string, `s2' is arg for it. */
|
|
521
|
173
|
522 static void
|
|
523 error (char *s1, char *s2, char *s3)
|
0
|
524 {
|
118
|
525 fprintf (stderr, "movemail: ");
|
|
526 fprintf (stderr, s1, s2, s3);
|
|
527 fprintf (stderr, "\n");
|
0
|
528 }
|
|
529
|
173
|
530 static void
|
|
531 pfatal_with_name (char *name)
|
0
|
532 {
|
118
|
533 char *s = concat ("", strerror (errno), " for %s");
|
0
|
534 fatal (s, name);
|
|
535 }
|
|
536
|
173
|
537 static void
|
|
538 pfatal_and_delete (char *name)
|
0
|
539 {
|
118
|
540 char *s = concat ("", strerror (errno), " for %s");
|
0
|
541 unlink (name);
|
|
542 fatal (s, name);
|
|
543 }
|
|
544
|
|
545 /* Return a newly-allocated string whose contents concatenate those of s1, s2, s3. */
|
|
546
|
173
|
547 static char *
|
|
548 concat (char *s1, char *s2, char *s3)
|
0
|
549 {
|
|
550 int len1 = strlen (s1), len2 = strlen (s2), len3 = strlen (s3);
|
|
551 char *result = (char *) xmalloc (len1 + len2 + len3 + 1);
|
|
552
|
|
553 strcpy (result, s1);
|
|
554 strcpy (result + len1, s2);
|
|
555 strcpy (result + len1 + len2, s3);
|
|
556 *(result + len1 + len2 + len3) = 0;
|
|
557
|
|
558 return result;
|
|
559 }
|
|
560
|
|
561 /* Like malloc but get fatal error if memory is exhausted. */
|
|
562
|
173
|
563 static long *
|
|
564 xmalloc (unsigned int size)
|
0
|
565 {
|
118
|
566 long *result = (long *) malloc (size);
|
0
|
567 if (!result)
|
118
|
568 fatal ("virtual memory exhausted", 0);
|
0
|
569 return result;
|
|
570 }
|
412
|
571
|
0
|
572 /* This is the guts of the interface to the Post Office Protocol. */
|
|
573
|
|
574 #ifdef MAIL_USE_POP
|
|
575
|
412
|
576 #ifndef WINDOWSNT
|
0
|
577 #include <sys/socket.h>
|
|
578 #include <netinet/in.h>
|
|
579 #include <netdb.h>
|
118
|
580 #else
|
|
581 #undef _WINSOCKAPI_
|
|
582 #include <winsock.h>
|
|
583 #endif
|
0
|
584 #include <stdio.h>
|
412
|
585 #include <pwd.h>
|
0
|
586
|
377
|
587 #define POP_ERROR (-1)
|
|
588 #define POP_RETRIEVED (0)
|
|
589 #define POP_DONE (1)
|
0
|
590
|
|
591 char *progname;
|
|
592 FILE *sfi;
|
|
593 FILE *sfo;
|
118
|
594 char ibuffer[BUFSIZ];
|
|
595 char obuffer[BUFSIZ];
|
0
|
596 char Errmsg[80];
|
|
597
|
173
|
598 static int
|
|
599 popmail (char *user, char *outfile, char *password)
|
0
|
600 {
|
|
601 int nmsgs, nbytes;
|
377
|
602 register int i, idx;
|
0
|
603 int mbfi;
|
377
|
604 short* retrieved_list;
|
0
|
605 FILE *mbf;
|
118
|
606 popserver server;
|
0
|
607
|
412
|
608 VERBOSE(("opening server\r"));
|
118
|
609 server = pop_open (0, user, password, POP_NO_GETPASS);
|
|
610 if (! server)
|
0
|
611 {
|
173
|
612 error (pop_error, NULL, NULL);
|
118
|
613 return (1);
|
0
|
614 }
|
|
615
|
412
|
616 VERBOSE(("stat'ing messages\r"));
|
118
|
617 if (pop_stat (server, &nmsgs, &nbytes))
|
0
|
618 {
|
173
|
619 error (pop_error, NULL, NULL);
|
118
|
620 return (1);
|
0
|
621 }
|
|
622
|
|
623 if (!nmsgs)
|
118
|
624 {
|
377
|
625 VERBOSE(("closing server\n"));
|
118
|
626 pop_close (server);
|
|
627 return (0);
|
|
628 }
|
0
|
629
|
377
|
630 /* build a retrieved table */
|
|
631 retrieved_list = (short*) xmalloc (sizeof (short) * (nmsgs+1));
|
|
632 memset (retrieved_list, 0, sizeof (short) * (nmsgs+1));
|
|
633
|
0
|
634 mbfi = open (outfile, O_WRONLY | O_CREAT | O_EXCL, 0666);
|
|
635 if (mbfi < 0)
|
118
|
636 {
|
|
637 pop_close (server);
|
|
638 error ("Error in open: %s, %s", strerror (errno), outfile);
|
|
639 return (1);
|
|
640 }
|
412
|
641 #if !defined(__CYGWIN32__) && !defined(WINDOWSNT)
|
|
642 fchown (mbfi, getuid (), -1);
|
259
|
643 #endif
|
0
|
644
|
118
|
645 if ((mbf = fdopen (mbfi, "wb")) == NULL)
|
0
|
646 {
|
118
|
647 pop_close (server);
|
173
|
648 error ("Error in fdopen: %s", strerror (errno), NULL);
|
118
|
649 close (mbfi);
|
|
650 unlink (outfile);
|
|
651 return (1);
|
0
|
652 }
|
|
653
|
377
|
654 for (idx = 0; idx < nmsgs; idx++)
|
0
|
655 {
|
377
|
656 i = reverse ? nmsgs - idx : idx + 1;
|
412
|
657 VERBOSE(("checking message %d \r", i));
|
377
|
658
|
|
659 if (!regexp_pattern
|
|
660 ||
|
|
661 pop_search_top (server, i, match_lines, regexp_pattern) == POP_RETRIEVED)
|
118
|
662 {
|
412
|
663 VERBOSE(("retrieving message %d \r", i));
|
377
|
664 mbx_delimit_begin (mbf);
|
|
665 if (pop_retr (server, i, mbx_write, mbf) != POP_RETRIEVED)
|
|
666 {
|
|
667 error (Errmsg, NULL, NULL);
|
|
668 close (mbfi);
|
|
669 return (1);
|
|
670 }
|
|
671
|
|
672 retrieved_list[i]=1;
|
|
673
|
|
674 mbx_delimit_end (mbf);
|
|
675 fflush (mbf);
|
|
676 if (ferror (mbf))
|
|
677 {
|
|
678 error ("Error in fflush: %s", strerror (errno), NULL);
|
|
679 pop_close (server);
|
|
680 close (mbfi);
|
|
681 return (1);
|
|
682 }
|
118
|
683 }
|
0
|
684 }
|
|
685
|
118
|
686 /* On AFS, a call to write only modifies the file in the local
|
|
687 * workstation's AFS cache. The changes are not written to the server
|
|
688 * until a call to fsync or close is made. Users with AFS home
|
|
689 * directories have lost mail when over quota because these checks were
|
|
690 * not made in previous versions of movemail. */
|
|
691
|
412
|
692 #ifdef BSD
|
0
|
693 if (fsync (mbfi) < 0)
|
|
694 {
|
173
|
695 error ("Error in fsync: %s", strerror (errno), NULL);
|
118
|
696 return (1);
|
0
|
697 }
|
118
|
698 #endif
|
0
|
699
|
|
700 if (close (mbfi) == -1)
|
|
701 {
|
173
|
702 error ("Error in close: %s", strerror (errno), NULL);
|
118
|
703 return (1);
|
0
|
704 }
|
|
705
|
377
|
706 if (!keep_messages)
|
0
|
707 {
|
377
|
708 for (i = 1; i <= nmsgs; i++)
|
118
|
709 {
|
377
|
710 if (retrieved_list[i] == 1)
|
|
711 {
|
412
|
712 VERBOSE(("deleting message %d \r", i));
|
377
|
713 if (pop_delete (server, i))
|
|
714 {
|
|
715 error (pop_error, NULL, NULL);
|
|
716 pop_close (server);
|
|
717 return (1);
|
|
718 }
|
|
719 }
|
118
|
720 }
|
0
|
721 }
|
|
722
|
377
|
723 VERBOSE(("closing server \n"));
|
118
|
724 if (pop_quit (server))
|
|
725 {
|
173
|
726 error (pop_error, NULL, NULL);
|
118
|
727 return (1);
|
|
728 }
|
|
729
|
0
|
730 return (0);
|
|
731 }
|
|
732
|
173
|
733 static int
|
424
|
734 pop_retr (popserver server, int msgno, int (*action)(char *, FILE *), void *arg)
|
0
|
735 {
|
118
|
736 char *line;
|
|
737 int ret;
|
70
|
738
|
118
|
739 if (pop_retrieve_first (server, msgno, &line))
|
70
|
740 {
|
118
|
741 strncpy (Errmsg, pop_error, sizeof (Errmsg));
|
|
742 Errmsg[sizeof (Errmsg)-1] = '\0';
|
377
|
743 return (POP_ERROR);
|
0
|
744 }
|
48
|
745
|
118
|
746 while (! (ret = pop_retrieve_next (server, &line)))
|
70
|
747 {
|
118
|
748 if (! line)
|
|
749 break;
|
0
|
750
|
377
|
751 if ((*action)(line, arg) != POP_RETRIEVED)
|
118
|
752 {
|
|
753 strcpy (Errmsg, strerror (errno));
|
|
754 pop_close (server);
|
377
|
755 return (POP_ERROR);
|
118
|
756 }
|
48
|
757 }
|
70
|
758
|
118
|
759 if (ret)
|
70
|
760 {
|
118
|
761 strncpy (Errmsg, pop_error, sizeof (Errmsg));
|
|
762 Errmsg[sizeof (Errmsg)-1] = '\0';
|
377
|
763 return (POP_ERROR);
|
|
764 }
|
|
765
|
|
766 return (POP_RETRIEVED);
|
|
767 }
|
|
768
|
|
769 /* search the top lines of each message looking for a match */
|
|
770 static int
|
|
771 pop_search_top (popserver server, int msgno, int lines, struct re_pattern_buffer* regexp)
|
|
772 {
|
|
773 char *line;
|
|
774 int ret;
|
|
775 int match = POP_DONE;
|
|
776
|
|
777 if (pop_top_first (server, msgno, lines, &line))
|
|
778 {
|
|
779 strncpy (Errmsg, pop_error, sizeof (Errmsg));
|
|
780 Errmsg[sizeof (Errmsg)-1] = '\0';
|
|
781 return (POP_ERROR);
|
70
|
782 }
|
118
|
783
|
377
|
784 while (! (ret = pop_top_next (server, &line)))
|
|
785 {
|
|
786 if (! line)
|
|
787 break;
|
|
788
|
|
789 /* VERBOSE (("checking %s\n", line));*/
|
|
790 if (match != POP_RETRIEVED)
|
|
791 {
|
|
792 if ((ret = re_match (regexp, line, strlen (line), 0, 0)) == -2 )
|
|
793 {
|
|
794 strcpy (Errmsg, "error in regular expression");
|
|
795 pop_close (server);
|
|
796 return (POP_ERROR);
|
|
797 }
|
|
798 else if (ret >=0)
|
|
799 {
|
|
800 match = POP_RETRIEVED;
|
|
801 }
|
|
802 }
|
|
803 }
|
|
804
|
|
805 if (ret)
|
|
806 {
|
|
807 strncpy (Errmsg, pop_error, sizeof (Errmsg));
|
|
808 Errmsg[sizeof (Errmsg)-1] = '\0';
|
|
809 return (POP_ERROR);
|
|
810 }
|
|
811
|
|
812 return match;
|
0
|
813 }
|
|
814
|
118
|
815 /* Do this as a macro instead of using strcmp to save on execution time. */
|
|
816 #define IS_FROM_LINE(a) ((a[0] == 'F') \
|
|
817 && (a[1] == 'r') \
|
|
818 && (a[2] == 'o') \
|
|
819 && (a[3] == 'm') \
|
|
820 && (a[4] == ' '))
|
0
|
821
|
173
|
822 static int
|
|
823 mbx_write (char *line, FILE *mbf)
|
70
|
824 {
|
118
|
825 if (IS_FROM_LINE (line))
|
|
826 {
|
|
827 if (fputc ('>', mbf) == EOF)
|
377
|
828 return (POP_ERROR);
|
118
|
829 }
|
|
830 if (fputs (line, mbf) == EOF)
|
377
|
831 return (POP_ERROR);
|
118
|
832 if (fputc (0x0a, mbf) == EOF)
|
377
|
833 return (POP_ERROR);
|
|
834 return (POP_RETRIEVED);
|
70
|
835 }
|
|
836
|
173
|
837 static int
|
|
838 mbx_delimit_begin (FILE *mbf)
|
70
|
839 {
|
118
|
840 if (fputs ("\f\n0, unseen,,\n", mbf) == EOF)
|
377
|
841 return (POP_ERROR);
|
|
842 return (POP_RETRIEVED);
|
70
|
843 }
|
|
844
|
173
|
845 static int
|
|
846 mbx_delimit_end (FILE *mbf)
|
70
|
847 {
|
118
|
848 if (putc ('\037', mbf) == EOF)
|
377
|
849 return (POP_ERROR);
|
|
850 return (POP_RETRIEVED);
|
0
|
851 }
|
|
852
|
377
|
853 /* Turn a name, which is an ed-style (but Emacs syntax) regular
|
|
854 expression, into a real regular expression by compiling it. */
|
|
855 static struct re_pattern_buffer*
|
380
|
856 compile_regex (char* pattern)
|
377
|
857 {
|
|
858 char *err;
|
|
859 struct re_pattern_buffer *patbuf=0;
|
|
860
|
|
861 patbuf = (struct re_pattern_buffer*) xmalloc (sizeof (struct re_pattern_buffer));
|
|
862 patbuf->translate = NULL;
|
|
863 patbuf->fastmap = NULL;
|
|
864 patbuf->buffer = NULL;
|
|
865 patbuf->allocated = 0;
|
|
866
|
380
|
867 err = (char*) re_compile_pattern (pattern, strlen (pattern), patbuf);
|
377
|
868 if (err != NULL)
|
|
869 {
|
|
870 error ("%s while compiling pattern", err, NULL);
|
|
871 return 0;
|
|
872 }
|
|
873
|
|
874 return patbuf;
|
|
875 }
|
|
876
|
|
877
|
|
878
|
0
|
879 #endif /* MAIL_USE_POP */
|
412
|
880
|
0
|
881 #ifndef HAVE_STRERROR
|
388
|
882 char *
|
173
|
883 strerror (int errnum)
|
0
|
884 {
|
|
885 extern char *sys_errlist[];
|
|
886 extern int sys_nerr;
|
|
887
|
|
888 if (errnum >= 0 && errnum < sys_nerr)
|
|
889 return sys_errlist[errnum];
|
|
890 return (char *) "Unknown error";
|
|
891 }
|
|
892
|
|
893 #endif /* ! HAVE_STRERROR */
|