Mercurial > emacs
annotate lib-src/movemail.c @ 580:c8860f81ccf7
*** empty log message ***
author | Jim Blandy <jimb@redhat.com> |
---|---|
date | Sun, 15 Mar 1992 03:47:41 +0000 |
parents | 3dfecf91a09b |
children | 3db1540d4b97 |
rev | line source |
---|---|
23 | 1 /* movemail foo bar -- move file foo to file bar, |
2 locking file foo the way /bin/mail respects. | |
580 | 3 Copyright (C) 1986, 1992 Free Software Foundation, Inc. |
23 | 4 |
5 This file is part of GNU Emacs. | |
6 | |
38 | 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 1, or (at your option) | |
10 any later version. | |
23 | 11 |
38 | 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 | |
19 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */ | |
23 | 20 |
510
4eaef1578a15
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
447
diff
changeset
|
21 /* Important notice: defining MAIL_USE_FLOCK *will cause loss of mail* |
4eaef1578a15
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
447
diff
changeset
|
22 if you do it on a system that does not normally use flock as its way of |
4eaef1578a15
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
447
diff
changeset
|
23 interlocking access to inbox files. The setting of MAIL_USE_FLOCK |
4eaef1578a15
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
447
diff
changeset
|
24 *must agree* with the system's own conventions. |
4eaef1578a15
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
447
diff
changeset
|
25 It is not a choice that is up to you. |
4eaef1578a15
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
447
diff
changeset
|
26 |
4eaef1578a15
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
447
diff
changeset
|
27 So, if your system uses lock files rather than flock, then the only way |
4eaef1578a15
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
447
diff
changeset
|
28 you can get proper operation is to enable movemail to write lockfiles there. |
4eaef1578a15
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
447
diff
changeset
|
29 This means you must either give that directory access modes |
4eaef1578a15
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
447
diff
changeset
|
30 that permit everyone to write lockfiles in it, or you must make movemail |
4eaef1578a15
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
447
diff
changeset
|
31 a setuid or setgid program. */ |
4eaef1578a15
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
447
diff
changeset
|
32 |
23 | 33 /* |
34 * Modified January, 1986 by Michael R. Gretzinger (Project Athena) | |
35 * | |
36 * Added POP (Post Office Protocol) service. When compiled -DPOP | |
37 * movemail will accept input filename arguments of the form | |
38 * "po:username". This will cause movemail to open a connection to | |
39 * a pop server running on $MAILHOST (environment variable). Movemail | |
40 * must be setuid to root in order to work with POP. | |
41 * | |
42 * New module: popmail.c | |
43 * Modified routines: | |
447
2e226dcdaf0f
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
351
diff
changeset
|
44 * main - added code within #ifdef MAIL_USE_POP; added setuid (getuid ()) |
23 | 45 * after POP code. |
46 * New routines in movemail.c: | |
47 * get_errmsg - return pointer to system error message | |
48 * | |
49 */ | |
50 | |
51 #include <sys/types.h> | |
52 #include <sys/stat.h> | |
53 #include <sys/file.h> | |
54 #include <errno.h> | |
55 #define NO_SHORTNAMES /* Tell config not to load remap.h */ | |
56 #include "../src/config.h" | |
57 | |
58 #ifdef USG | |
59 #include <fcntl.h> | |
60 #include <unistd.h> | |
27 | 61 #ifndef F_OK |
62 #define F_OK 0 | |
63 #define X_OK 1 | |
64 #define W_OK 2 | |
65 #define R_OK 4 | |
66 #endif | |
23 | 67 #endif /* USG */ |
68 | |
69 #ifdef XENIX | |
70 #include <sys/locking.h> | |
71 #endif | |
72 | |
25 | 73 #ifdef MAIL_USE_MMDF |
74 extern int lk_open (), lk_close (); | |
75 #endif | |
76 | |
23 | 77 /* Cancel substitutions made by config.h for Emacs. */ |
78 #undef open | |
79 #undef read | |
80 #undef write | |
81 #undef close | |
82 | |
571 | 83 char *malloc (); |
23 | 84 char *concat (); |
571 | 85 char *xmalloc (); |
86 #ifndef errno | |
23 | 87 extern int errno; |
571 | 88 #endif |
23 | 89 |
90 /* Nonzero means this is name of a lock file to delete on fatal error. */ | |
91 char *delete_lockname; | |
92 | |
93 main (argc, argv) | |
94 int argc; | |
95 char **argv; | |
96 { | |
97 char *inname, *outname; | |
98 int indesc, outdesc; | |
99 char buf[1024]; | |
100 int nread; | |
101 | |
102 #ifndef MAIL_USE_FLOCK | |
103 struct stat st; | |
104 long now; | |
105 int tem; | |
106 char *lockname, *p; | |
107 char tempname[40]; | |
108 int desc; | |
109 #endif /* not MAIL_USE_FLOCK */ | |
110 | |
111 delete_lockname = 0; | |
112 | |
113 if (argc < 3) | |
114 fatal ("two arguments required"); | |
115 | |
116 inname = argv[1]; | |
117 outname = argv[2]; | |
118 | |
25 | 119 #ifdef MAIL_USE_MMDF |
120 mmdf_init (argv[0]); | |
121 #endif | |
122 | |
120 | 123 /* Check access to output file. */ |
23 | 124 if (access (outname, F_OK) == 0 && access (outname, W_OK) != 0) |
125 pfatal_with_name (outname); | |
126 | |
127 /* Also check that outname's directory is writeable to the real uid. */ | |
128 { | |
129 char *buf = (char *) malloc (strlen (outname) + 1); | |
130 char *p, q; | |
131 strcpy (buf, outname); | |
132 p = buf + strlen (buf); | |
133 while (p > buf && p[-1] != '/') | |
134 *--p = 0; | |
135 if (p == buf) | |
136 *p++ = '.'; | |
137 if (access (buf, W_OK) != 0) | |
138 pfatal_with_name (buf); | |
139 free (buf); | |
140 } | |
141 | |
142 #ifdef MAIL_USE_POP | |
143 if (!bcmp (inname, "po:", 3)) | |
144 { | |
145 int status; char *user; | |
146 | |
147 user = (char *) rindex (inname, ':') + 1; | |
148 status = popmail (user, outname); | |
149 exit (status); | |
150 } | |
151 | |
447
2e226dcdaf0f
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
351
diff
changeset
|
152 setuid (getuid ()); |
23 | 153 #endif /* MAIL_USE_POP */ |
154 | |
120 | 155 /* Check access to input file. */ |
156 if (access (inname, R_OK | W_OK) != 0) | |
157 pfatal_with_name (inname); | |
158 | |
25 | 159 #ifndef MAIL_USE_MMDF |
23 | 160 #ifndef MAIL_USE_FLOCK |
161 /* Use a lock file named /usr/spool/mail/$USER.lock: | |
162 If it exists, the mail file is locked. */ | |
351
5729b1cc3942
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
127
diff
changeset
|
163 /* Note: this locking mechanism is *required* by the mailer |
5729b1cc3942
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
127
diff
changeset
|
164 (on systems which use it) to prevent loss of mail. |
5729b1cc3942
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
127
diff
changeset
|
165 |
5729b1cc3942
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
127
diff
changeset
|
166 On systems that use a lock file, extracting the mail without locking |
5729b1cc3942
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
127
diff
changeset
|
167 WILL occasionally cause loss of mail due to timing errors! |
5729b1cc3942
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
127
diff
changeset
|
168 |
5729b1cc3942
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
127
diff
changeset
|
169 So, if creation of the lock file fails |
5729b1cc3942
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
127
diff
changeset
|
170 due to access permission on /usr/spool/mail, |
5729b1cc3942
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
127
diff
changeset
|
171 you simply MUST change the permission |
5729b1cc3942
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
127
diff
changeset
|
172 and/or make movemail a setgid program |
5729b1cc3942
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
127
diff
changeset
|
173 so it can create lock files properly. |
5729b1cc3942
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
127
diff
changeset
|
174 |
5729b1cc3942
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
127
diff
changeset
|
175 You might also wish to verify that your system is one |
5729b1cc3942
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
127
diff
changeset
|
176 which uses lock files for this purpose. Some systems use other methods. |
5729b1cc3942
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
127
diff
changeset
|
177 |
5729b1cc3942
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
127
diff
changeset
|
178 If your system uses the `flock' system call for mail locking, |
5729b1cc3942
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
127
diff
changeset
|
179 define MAIL_USE_FLOCK in config.h or the s-*.h file |
5729b1cc3942
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
127
diff
changeset
|
180 and recompile movemail. If the s- file for your system |
5729b1cc3942
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
127
diff
changeset
|
181 should define MAIL_USE_FLOCK but does not, send a bug report |
5729b1cc3942
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
127
diff
changeset
|
182 to bug-gnu-emacs@prep.ai.mit.edu so we can fix it. */ |
5729b1cc3942
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
127
diff
changeset
|
183 |
23 | 184 lockname = concat (inname, ".lock", ""); |
185 strcpy (tempname, inname); | |
186 p = tempname + strlen (tempname); | |
187 while (p != tempname && p[-1] != '/') | |
188 p--; | |
189 *p = 0; | |
190 strcpy (p, "EXXXXXX"); | |
191 mktemp (tempname); | |
447
2e226dcdaf0f
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
351
diff
changeset
|
192 unlink (tempname); |
23 | 193 |
194 while (1) | |
195 { | |
196 /* Create the lock file, but not under the lock file name. */ | |
197 /* Give up if cannot do that. */ | |
198 desc = open (tempname, O_WRONLY | O_CREAT, 0666); | |
199 if (desc < 0) | |
351
5729b1cc3942
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
127
diff
changeset
|
200 pfatal_with_name ("lock file--see source file etc/movemail.c"); |
23 | 201 close (desc); |
202 | |
203 tem = link (tempname, lockname); | |
447
2e226dcdaf0f
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
351
diff
changeset
|
204 unlink (tempname); |
23 | 205 if (tem >= 0) |
206 break; | |
207 sleep (1); | |
208 | |
209 /* If lock file is a minute old, unlock it. */ | |
210 if (stat (lockname, &st) >= 0) | |
211 { | |
212 now = time (0); | |
213 if (st.st_ctime < now - 60) | |
447
2e226dcdaf0f
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
351
diff
changeset
|
214 unlink (lockname); |
23 | 215 } |
216 } | |
217 | |
218 delete_lockname = lockname; | |
219 #endif /* not MAIL_USE_FLOCK */ | |
220 | |
221 #ifdef MAIL_USE_FLOCK | |
222 indesc = open (inname, O_RDWR); | |
223 #else /* if not MAIL_USE_FLOCK */ | |
224 indesc = open (inname, O_RDONLY); | |
225 #endif /* not MAIL_USE_FLOCK */ | |
25 | 226 #else /* MAIL_USE_MMDF */ |
227 indesc = lk_open (inname, O_RDONLY, 0, 0, 10); | |
228 #endif /* MAIL_USE_MMDF */ | |
229 | |
23 | 230 if (indesc < 0) |
231 pfatal_with_name (inname); | |
232 | |
447
2e226dcdaf0f
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
351
diff
changeset
|
233 #if defined (BSD) || defined (XENIX) |
23 | 234 /* In case movemail is setuid to root, make sure the user can |
235 read the output file. */ | |
236 /* This is desirable for all systems | |
237 but I don't want to assume all have the umask system call */ | |
238 umask (umask (0) & 0333); | |
239 #endif /* BSD or Xenix */ | |
240 outdesc = open (outname, O_WRONLY | O_CREAT | O_EXCL, 0666); | |
241 if (outdesc < 0) | |
242 pfatal_with_name (outname); | |
243 #ifdef MAIL_USE_FLOCK | |
244 #ifdef XENIX | |
245 if (locking (indesc, LK_RLCK, 0L) < 0) pfatal_with_name (inname); | |
246 #else | |
247 if (flock (indesc, LOCK_EX) < 0) pfatal_with_name (inname); | |
248 #endif | |
249 #endif /* MAIL_USE_FLOCK */ | |
250 | |
251 while (1) | |
252 { | |
253 nread = read (indesc, buf, sizeof buf); | |
254 if (nread != write (outdesc, buf, nread)) | |
255 { | |
256 int saved_errno = errno; | |
447
2e226dcdaf0f
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
351
diff
changeset
|
257 unlink (outname); |
23 | 258 errno = saved_errno; |
259 pfatal_with_name (outname); | |
260 } | |
261 if (nread < sizeof buf) | |
262 break; | |
263 } | |
264 | |
265 #ifdef BSD | |
447
2e226dcdaf0f
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
351
diff
changeset
|
266 if (fsync (outdesc) < 0) |
2e226dcdaf0f
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
351
diff
changeset
|
267 pfatal_and_delete (outname); |
23 | 268 #endif |
269 | |
270 /* Check to make sure no errors before we zap the inbox. */ | |
271 if (close (outdesc) != 0) | |
447
2e226dcdaf0f
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
351
diff
changeset
|
272 pfatal_and_delete (outname); |
23 | 273 |
274 #ifdef MAIL_USE_FLOCK | |
447
2e226dcdaf0f
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
351
diff
changeset
|
275 #if defined (STRIDE) || defined (XENIX) |
23 | 276 /* Stride, xenix have file locking, but no ftruncate. This mess will do. */ |
447
2e226dcdaf0f
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
351
diff
changeset
|
277 close (open (inname, O_CREAT | O_TRUNC | O_RDWR, 0666)); |
23 | 278 #else |
447
2e226dcdaf0f
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
351
diff
changeset
|
279 ftruncate (indesc, 0L); |
23 | 280 #endif /* STRIDE or XENIX */ |
281 #endif /* MAIL_USE_FLOCK */ | |
25 | 282 |
283 #ifdef MAIL_USE_MMDF | |
284 lk_close (indesc, 0, 0, 0); | |
285 #else | |
23 | 286 close (indesc); |
25 | 287 #endif |
23 | 288 |
289 #ifndef MAIL_USE_FLOCK | |
290 /* Delete the input file; if we can't, at least get rid of its contents. */ | |
571 | 291 #ifdef MAIL_UNLINK_SPOOL |
292 /* This is generally bad to do, because it destroys the permissions | |
293 that were set on the file. Better to just empty the file. */ | |
294 if (unlink (inname) < 0 && errno != ENOENT) | |
295 #endif /* MAIL_UNLINK_SPOOL */ | |
296 creat (inname, 0600); | |
25 | 297 #ifndef MAIL_USE_MMDF |
298 unlink (lockname); | |
299 #endif /* not MAIL_USE_MMDF */ | |
23 | 300 #endif /* not MAIL_USE_FLOCK */ |
301 exit (0); | |
302 } | |
303 | |
304 /* Print error message and exit. */ | |
305 | |
306 fatal (s1, s2) | |
307 char *s1, *s2; | |
308 { | |
309 if (delete_lockname) | |
310 unlink (delete_lockname); | |
311 error (s1, s2); | |
312 exit (1); | |
313 } | |
314 | |
315 /* Print error message. `s1' is printf control string, `s2' is arg for it. */ | |
316 | |
120 | 317 error (s1, s2, s3) |
318 char *s1, *s2, *s3; | |
23 | 319 { |
320 printf ("movemail: "); | |
120 | 321 printf (s1, s2, s3); |
23 | 322 printf ("\n"); |
323 } | |
324 | |
325 pfatal_with_name (name) | |
326 char *name; | |
327 { | |
328 extern int errno, sys_nerr; | |
329 extern char *sys_errlist[]; | |
330 char *s; | |
331 | |
332 if (errno < sys_nerr) | |
333 s = concat ("", sys_errlist[errno], " for %s"); | |
334 else | |
335 s = "cannot open %s"; | |
336 fatal (s, name); | |
337 } | |
338 | |
447
2e226dcdaf0f
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
351
diff
changeset
|
339 pfatal_and_delete (name) |
2e226dcdaf0f
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
351
diff
changeset
|
340 char *name; |
2e226dcdaf0f
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
351
diff
changeset
|
341 { |
2e226dcdaf0f
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
351
diff
changeset
|
342 extern int errno, sys_nerr; |
2e226dcdaf0f
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
351
diff
changeset
|
343 extern char *sys_errlist[]; |
2e226dcdaf0f
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
351
diff
changeset
|
344 char *s; |
2e226dcdaf0f
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
351
diff
changeset
|
345 |
2e226dcdaf0f
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
351
diff
changeset
|
346 if (errno < sys_nerr) |
2e226dcdaf0f
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
351
diff
changeset
|
347 s = concat ("", sys_errlist[errno], " for %s"); |
2e226dcdaf0f
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
351
diff
changeset
|
348 else |
2e226dcdaf0f
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
351
diff
changeset
|
349 s = "cannot open %s"; |
2e226dcdaf0f
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
351
diff
changeset
|
350 |
2e226dcdaf0f
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
351
diff
changeset
|
351 unlink (name); |
2e226dcdaf0f
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
351
diff
changeset
|
352 fatal (s, name); |
2e226dcdaf0f
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
351
diff
changeset
|
353 } |
2e226dcdaf0f
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
351
diff
changeset
|
354 |
23 | 355 /* Return a newly-allocated string whose contents concatenate those of s1, s2, s3. */ |
356 | |
357 char * | |
358 concat (s1, s2, s3) | |
359 char *s1, *s2, *s3; | |
360 { | |
361 int len1 = strlen (s1), len2 = strlen (s2), len3 = strlen (s3); | |
362 char *result = (char *) xmalloc (len1 + len2 + len3 + 1); | |
363 | |
364 strcpy (result, s1); | |
365 strcpy (result + len1, s2); | |
366 strcpy (result + len1 + len2, s3); | |
367 *(result + len1 + len2 + len3) = 0; | |
368 | |
369 return result; | |
370 } | |
371 | |
372 /* Like malloc but get fatal error if memory is exhausted. */ | |
373 | |
571 | 374 char * |
23 | 375 xmalloc (size) |
571 | 376 unsigned size; |
23 | 377 { |
571 | 378 char *result = malloc (size); |
23 | 379 if (!result) |
380 fatal ("virtual memory exhausted", 0); | |
381 return result; | |
382 } | |
383 | |
384 /* This is the guts of the interface to the Post Office Protocol. */ | |
385 | |
386 #ifdef MAIL_USE_POP | |
387 | |
388 #include <sys/socket.h> | |
389 #include <netinet/in.h> | |
390 #include <netdb.h> | |
391 #include <stdio.h> | |
392 | |
393 #ifdef USG | |
394 #include <fcntl.h> | |
395 /* Cancel substitutions made by config.h for Emacs. */ | |
396 #undef open | |
397 #undef read | |
398 #undef write | |
399 #undef close | |
400 #endif /* USG */ | |
401 | |
402 #define NOTOK (-1) | |
403 #define OK 0 | |
404 #define DONE 1 | |
405 | |
406 char *progname; | |
407 FILE *sfi; | |
408 FILE *sfo; | |
409 char Errmsg[80]; | |
410 | |
411 static int debug = 0; | |
412 | |
120 | 413 char *get_errmsg (); |
414 char *getenv (); | |
415 int mbx_write (); | |
416 | |
417 popmail (user, outfile) | |
418 char *user; | |
419 char *outfile; | |
23 | 420 { |
120 | 421 char *host; |
422 int nmsgs, nbytes; | |
423 char response[128]; | |
424 register int i; | |
425 int mbfi; | |
426 FILE *mbf; | |
23 | 427 |
120 | 428 host = getenv ("MAILHOST"); |
429 if (host == NULL) | |
430 { | |
431 fatal ("no MAILHOST defined"); | |
23 | 432 } |
433 | |
120 | 434 if (pop_init (host) == NOTOK) |
435 { | |
447
2e226dcdaf0f
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
351
diff
changeset
|
436 fatal (Errmsg); |
23 | 437 } |
438 | |
120 | 439 if (getline (response, sizeof response, sfi) != OK) |
440 { | |
447
2e226dcdaf0f
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
351
diff
changeset
|
441 fatal (response); |
23 | 442 } |
443 | |
447
2e226dcdaf0f
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
351
diff
changeset
|
444 if (pop_command ("USER %s", user) == NOTOK |
120 | 445 || pop_command ("RPOP %s", user) == NOTOK) |
446 { | |
447 pop_command ("QUIT"); | |
447
2e226dcdaf0f
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
351
diff
changeset
|
448 fatal (Errmsg); |
23 | 449 } |
450 | |
120 | 451 if (pop_stat (&nmsgs, &nbytes) == NOTOK) |
452 { | |
453 pop_command ("QUIT"); | |
447
2e226dcdaf0f
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
351
diff
changeset
|
454 fatal (Errmsg); |
120 | 455 } |
23 | 456 |
120 | 457 if (!nmsgs) |
458 { | |
459 pop_command ("QUIT"); | |
460 return 0; | |
23 | 461 } |
462 | |
120 | 463 mbfi = open (outfile, O_WRONLY | O_CREAT | O_EXCL, 0666); |
464 if (mbfi < 0) | |
465 { | |
466 pop_command ("QUIT"); | |
447
2e226dcdaf0f
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
351
diff
changeset
|
467 pfatal_and_delete (outfile); |
120 | 468 } |
469 fchown (mbfi, getuid (), -1); | |
470 | |
471 if ((mbf = fdopen (mbfi, "w")) == NULL) | |
472 { | |
473 pop_command ("QUIT"); | |
447
2e226dcdaf0f
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
351
diff
changeset
|
474 pfatal_and_delete (outfile); |
120 | 475 } |
476 | |
477 for (i = 1; i <= nmsgs; i++) | |
478 { | |
479 mbx_delimit_begin (mbf); | |
480 if (pop_retr (i, mbx_write, mbf) != OK) | |
481 { | |
482 pop_command ("QUIT"); | |
483 close (mbfi); | |
447
2e226dcdaf0f
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
351
diff
changeset
|
484 unlink (outfile); |
2e226dcdaf0f
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
351
diff
changeset
|
485 fatal (Errmsg); |
120 | 486 } |
487 mbx_delimit_end (mbf); | |
488 fflush (mbf); | |
489 } | |
490 | |
447
2e226dcdaf0f
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
351
diff
changeset
|
491 if (fsync (mbfi) < 0) |
2e226dcdaf0f
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
351
diff
changeset
|
492 { |
2e226dcdaf0f
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
351
diff
changeset
|
493 pop_command ("QUIT"); |
2e226dcdaf0f
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
351
diff
changeset
|
494 pfatal_and_delete (outfile); |
2e226dcdaf0f
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
351
diff
changeset
|
495 } |
2e226dcdaf0f
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
351
diff
changeset
|
496 |
2e226dcdaf0f
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
351
diff
changeset
|
497 if (close (mbfi) == -1) |
2e226dcdaf0f
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
351
diff
changeset
|
498 { |
2e226dcdaf0f
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
351
diff
changeset
|
499 pop_command ("QUIT"); |
2e226dcdaf0f
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
351
diff
changeset
|
500 pfatal_and_delete (outfile); |
2e226dcdaf0f
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
351
diff
changeset
|
501 } |
2e226dcdaf0f
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
351
diff
changeset
|
502 |
120 | 503 for (i = 1; i <= nmsgs; i++) |
504 { | |
505 if (pop_command ("DELE %d", i) == NOTOK) | |
506 { | |
447
2e226dcdaf0f
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
351
diff
changeset
|
507 /* Better to ignore this failure. */ |
23 | 508 } |
509 } | |
510 | |
120 | 511 pop_command ("QUIT"); |
447
2e226dcdaf0f
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
351
diff
changeset
|
512 return (0); |
23 | 513 } |
514 | |
120 | 515 pop_init (host) |
516 char *host; | |
23 | 517 { |
120 | 518 register struct hostent *hp; |
519 register struct servent *sp; | |
520 int lport = IPPORT_RESERVED - 1; | |
521 struct sockaddr_in sin; | |
522 register int s; | |
23 | 523 |
120 | 524 hp = gethostbyname (host); |
525 if (hp == NULL) | |
526 { | |
527 sprintf (Errmsg, "MAILHOST unknown: %s", host); | |
528 return NOTOK; | |
23 | 529 } |
530 | |
120 | 531 sp = getservbyname ("pop", "tcp"); |
532 if (sp == 0) | |
533 { | |
534 strcpy (Errmsg, "tcp/pop: unknown service"); | |
535 return NOTOK; | |
23 | 536 } |
537 | |
120 | 538 sin.sin_family = hp->h_addrtype; |
539 bcopy (hp->h_addr, (char *)&sin.sin_addr, hp->h_length); | |
540 sin.sin_port = sp->s_port; | |
541 s = rresvport (&lport); | |
542 if (s < 0) | |
543 { | |
544 sprintf (Errmsg, "error creating socket: %s", get_errmsg ()); | |
545 return NOTOK; | |
23 | 546 } |
547 | |
120 | 548 if (connect (s, (char *)&sin, sizeof sin) < 0) |
549 { | |
550 sprintf (Errmsg, "error during connect: %s", get_errmsg ()); | |
551 close (s); | |
552 return NOTOK; | |
23 | 553 } |
554 | |
120 | 555 sfi = fdopen (s, "r"); |
556 sfo = fdopen (s, "w"); | |
557 if (sfi == NULL || sfo == NULL) | |
558 { | |
559 sprintf (Errmsg, "error in fdopen: %s", get_errmsg ()); | |
560 close (s); | |
561 return NOTOK; | |
23 | 562 } |
563 | |
120 | 564 return OK; |
23 | 565 } |
566 | |
120 | 567 pop_command (fmt, a, b, c, d) |
568 char *fmt; | |
23 | 569 { |
120 | 570 char buf[128]; |
571 char errmsg[64]; | |
572 | |
573 sprintf (buf, fmt, a, b, c, d); | |
23 | 574 |
120 | 575 if (debug) fprintf (stderr, "---> %s\n", buf); |
576 if (putline (buf, Errmsg, sfo) == NOTOK) return NOTOK; | |
23 | 577 |
120 | 578 if (getline (buf, sizeof buf, sfi) != OK) |
579 { | |
580 strcpy (Errmsg, buf); | |
581 return NOTOK; | |
23 | 582 } |
583 | |
447
2e226dcdaf0f
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
351
diff
changeset
|
584 if (debug) |
2e226dcdaf0f
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
351
diff
changeset
|
585 fprintf (stderr, "<--- %s\n", buf); |
120 | 586 if (*buf != '+') |
587 { | |
588 strcpy (Errmsg, buf); | |
589 return NOTOK; | |
590 } | |
591 else | |
592 { | |
593 return OK; | |
23 | 594 } |
595 } | |
596 | |
597 | |
120 | 598 pop_stat (nmsgs, nbytes) |
599 int *nmsgs, *nbytes; | |
23 | 600 { |
120 | 601 char buf[128]; |
23 | 602 |
447
2e226dcdaf0f
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
351
diff
changeset
|
603 if (debug) |
2e226dcdaf0f
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
351
diff
changeset
|
604 fprintf (stderr, "---> STAT\n"); |
2e226dcdaf0f
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
351
diff
changeset
|
605 if (putline ("STAT", Errmsg, sfo) == NOTOK) |
2e226dcdaf0f
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
351
diff
changeset
|
606 return NOTOK; |
23 | 607 |
120 | 608 if (getline (buf, sizeof buf, sfi) != OK) |
609 { | |
610 strcpy (Errmsg, buf); | |
611 return NOTOK; | |
23 | 612 } |
613 | |
120 | 614 if (debug) fprintf (stderr, "<--- %s\n", buf); |
615 if (*buf != '+') | |
616 { | |
617 strcpy (Errmsg, buf); | |
618 return NOTOK; | |
619 } | |
620 else | |
621 { | |
622 sscanf (buf, "+OK %d %d", nmsgs, nbytes); | |
623 return OK; | |
23 | 624 } |
625 } | |
626 | |
120 | 627 pop_retr (msgno, action, arg) |
628 int (*action)(); | |
23 | 629 { |
120 | 630 char buf[128]; |
23 | 631 |
120 | 632 sprintf (buf, "RETR %d", msgno); |
633 if (debug) fprintf (stderr, "%s\n", buf); | |
634 if (putline (buf, Errmsg, sfo) == NOTOK) return NOTOK; | |
23 | 635 |
120 | 636 if (getline (buf, sizeof buf, sfi) != OK) |
637 { | |
638 strcpy (Errmsg, buf); | |
639 return NOTOK; | |
23 | 640 } |
641 | |
120 | 642 while (1) |
643 { | |
644 switch (multiline (buf, sizeof buf, sfi)) | |
645 { | |
23 | 646 case OK: |
120 | 647 (*action)(buf, arg); |
648 break; | |
23 | 649 case DONE: |
120 | 650 return OK; |
23 | 651 case NOTOK: |
120 | 652 strcpy (Errmsg, buf); |
653 return NOTOK; | |
23 | 654 } |
655 } | |
656 } | |
657 | |
120 | 658 getline (buf, n, f) |
659 char *buf; | |
660 register int n; | |
661 FILE *f; | |
23 | 662 { |
120 | 663 register char *p; |
664 int c; | |
23 | 665 |
120 | 666 p = buf; |
667 while (--n > 0 && (c = fgetc (f)) != EOF) | |
668 if ((*p++ = c) == '\n') break; | |
23 | 669 |
120 | 670 if (ferror (f)) |
671 { | |
672 strcpy (buf, "error on connection"); | |
673 return NOTOK; | |
674 } | |
675 | |
676 if (c == EOF && p == buf) | |
677 { | |
678 strcpy (buf, "connection closed by foreign host"); | |
679 return DONE; | |
23 | 680 } |
681 | |
120 | 682 *p = NULL; |
683 if (*--p == '\n') *p = NULL; | |
684 if (*--p == '\r') *p = NULL; | |
685 return OK; | |
23 | 686 } |
687 | |
120 | 688 multiline (buf, n, f) |
689 char *buf; | |
690 register int n; | |
691 FILE *f; | |
23 | 692 { |
447
2e226dcdaf0f
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
351
diff
changeset
|
693 if (getline (buf, n, f) != OK) |
2e226dcdaf0f
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
351
diff
changeset
|
694 return NOTOK; |
120 | 695 if (*buf == '.') |
696 { | |
697 if (*(buf+1) == NULL) | |
447
2e226dcdaf0f
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
351
diff
changeset
|
698 return DONE; |
120 | 699 else |
447
2e226dcdaf0f
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
351
diff
changeset
|
700 strcpy (buf, buf+1); |
23 | 701 } |
120 | 702 return OK; |
23 | 703 } |
704 | |
705 char * | |
120 | 706 get_errmsg () |
23 | 707 { |
120 | 708 extern int errno, sys_nerr; |
709 extern char *sys_errlist[]; | |
710 char *s; | |
23 | 711 |
120 | 712 if (errno < sys_nerr) |
713 s = sys_errlist[errno]; | |
714 else | |
715 s = "unknown error"; | |
716 return (s); | |
23 | 717 } |
718 | |
120 | 719 putline (buf, err, f) |
720 char *buf; | |
721 char *err; | |
722 FILE *f; | |
23 | 723 { |
120 | 724 fprintf (f, "%s\r\n", buf); |
725 fflush (f); | |
726 if (ferror (f)) | |
727 { | |
728 strcpy (err, "lost connection"); | |
729 return NOTOK; | |
23 | 730 } |
120 | 731 return OK; |
23 | 732 } |
733 | |
120 | 734 mbx_write (line, mbf) |
735 char *line; | |
736 FILE *mbf; | |
23 | 737 { |
120 | 738 fputs (line, mbf); |
739 fputc (0x0a, mbf); | |
23 | 740 } |
741 | |
120 | 742 mbx_delimit_begin (mbf) |
743 FILE *mbf; | |
23 | 744 { |
127
762710f7381a
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
120
diff
changeset
|
745 fputs ("\f\n0, unseen,,\n", mbf); |
23 | 746 } |
747 | |
120 | 748 mbx_delimit_end (mbf) |
749 FILE *mbf; | |
23 | 750 { |
120 | 751 putc ('\037', mbf); |
23 | 752 } |
753 | |
754 #endif /* MAIL_USE_POP */ |