Mercurial > emacs
annotate lib-src/movemail.c @ 601:3db1540d4b97
*** empty log message ***
author | Richard M. Stallman <rms@gnu.org> |
---|---|
date | Sat, 28 Mar 1992 21:00:42 +0000 |
parents | c8860f81ccf7 |
children | 63a8e7b3c547 |
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; | |
601
3db1540d4b97
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
580
diff
changeset
|
107 char *tempname; |
23 | 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", ""); |
601
3db1540d4b97
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
580
diff
changeset
|
185 tempname = (char *) xmalloc (strlen (inname) + 20); |
23 | 186 strcpy (tempname, inname); |
187 p = tempname + strlen (tempname); | |
188 while (p != tempname && p[-1] != '/') | |
189 p--; | |
190 *p = 0; | |
191 strcpy (p, "EXXXXXX"); | |
192 mktemp (tempname); | |
447
2e226dcdaf0f
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
351
diff
changeset
|
193 unlink (tempname); |
23 | 194 |
195 while (1) | |
196 { | |
197 /* Create the lock file, but not under the lock file name. */ | |
198 /* Give up if cannot do that. */ | |
199 desc = open (tempname, O_WRONLY | O_CREAT, 0666); | |
200 if (desc < 0) | |
351
5729b1cc3942
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
127
diff
changeset
|
201 pfatal_with_name ("lock file--see source file etc/movemail.c"); |
23 | 202 close (desc); |
203 | |
204 tem = link (tempname, lockname); | |
447
2e226dcdaf0f
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
351
diff
changeset
|
205 unlink (tempname); |
23 | 206 if (tem >= 0) |
207 break; | |
208 sleep (1); | |
209 | |
210 /* If lock file is a minute old, unlock it. */ | |
211 if (stat (lockname, &st) >= 0) | |
212 { | |
213 now = time (0); | |
214 if (st.st_ctime < now - 60) | |
447
2e226dcdaf0f
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
351
diff
changeset
|
215 unlink (lockname); |
23 | 216 } |
217 } | |
218 | |
219 delete_lockname = lockname; | |
220 #endif /* not MAIL_USE_FLOCK */ | |
221 | |
222 #ifdef MAIL_USE_FLOCK | |
223 indesc = open (inname, O_RDWR); | |
224 #else /* if not MAIL_USE_FLOCK */ | |
225 indesc = open (inname, O_RDONLY); | |
226 #endif /* not MAIL_USE_FLOCK */ | |
25 | 227 #else /* MAIL_USE_MMDF */ |
228 indesc = lk_open (inname, O_RDONLY, 0, 0, 10); | |
229 #endif /* MAIL_USE_MMDF */ | |
230 | |
23 | 231 if (indesc < 0) |
232 pfatal_with_name (inname); | |
233 | |
447
2e226dcdaf0f
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
351
diff
changeset
|
234 #if defined (BSD) || defined (XENIX) |
23 | 235 /* In case movemail is setuid to root, make sure the user can |
236 read the output file. */ | |
237 /* This is desirable for all systems | |
238 but I don't want to assume all have the umask system call */ | |
239 umask (umask (0) & 0333); | |
240 #endif /* BSD or Xenix */ | |
241 outdesc = open (outname, O_WRONLY | O_CREAT | O_EXCL, 0666); | |
242 if (outdesc < 0) | |
243 pfatal_with_name (outname); | |
244 #ifdef MAIL_USE_FLOCK | |
245 #ifdef XENIX | |
246 if (locking (indesc, LK_RLCK, 0L) < 0) pfatal_with_name (inname); | |
247 #else | |
248 if (flock (indesc, LOCK_EX) < 0) pfatal_with_name (inname); | |
249 #endif | |
250 #endif /* MAIL_USE_FLOCK */ | |
251 | |
252 while (1) | |
253 { | |
254 nread = read (indesc, buf, sizeof buf); | |
255 if (nread != write (outdesc, buf, nread)) | |
256 { | |
257 int saved_errno = errno; | |
447
2e226dcdaf0f
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
351
diff
changeset
|
258 unlink (outname); |
23 | 259 errno = saved_errno; |
260 pfatal_with_name (outname); | |
261 } | |
262 if (nread < sizeof buf) | |
263 break; | |
264 } | |
265 | |
266 #ifdef BSD | |
447
2e226dcdaf0f
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
351
diff
changeset
|
267 if (fsync (outdesc) < 0) |
2e226dcdaf0f
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
351
diff
changeset
|
268 pfatal_and_delete (outname); |
23 | 269 #endif |
270 | |
271 /* Check to make sure no errors before we zap the inbox. */ | |
272 if (close (outdesc) != 0) | |
447
2e226dcdaf0f
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
351
diff
changeset
|
273 pfatal_and_delete (outname); |
23 | 274 |
275 #ifdef MAIL_USE_FLOCK | |
447
2e226dcdaf0f
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
351
diff
changeset
|
276 #if defined (STRIDE) || defined (XENIX) |
23 | 277 /* 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
|
278 close (open (inname, O_CREAT | O_TRUNC | O_RDWR, 0666)); |
23 | 279 #else |
447
2e226dcdaf0f
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
351
diff
changeset
|
280 ftruncate (indesc, 0L); |
23 | 281 #endif /* STRIDE or XENIX */ |
282 #endif /* MAIL_USE_FLOCK */ | |
25 | 283 |
284 #ifdef MAIL_USE_MMDF | |
285 lk_close (indesc, 0, 0, 0); | |
286 #else | |
23 | 287 close (indesc); |
25 | 288 #endif |
23 | 289 |
290 #ifndef MAIL_USE_FLOCK | |
291 /* Delete the input file; if we can't, at least get rid of its contents. */ | |
571 | 292 #ifdef MAIL_UNLINK_SPOOL |
293 /* This is generally bad to do, because it destroys the permissions | |
294 that were set on the file. Better to just empty the file. */ | |
295 if (unlink (inname) < 0 && errno != ENOENT) | |
296 #endif /* MAIL_UNLINK_SPOOL */ | |
297 creat (inname, 0600); | |
25 | 298 #ifndef MAIL_USE_MMDF |
299 unlink (lockname); | |
300 #endif /* not MAIL_USE_MMDF */ | |
23 | 301 #endif /* not MAIL_USE_FLOCK */ |
302 exit (0); | |
303 } | |
304 | |
305 /* Print error message and exit. */ | |
306 | |
307 fatal (s1, s2) | |
308 char *s1, *s2; | |
309 { | |
310 if (delete_lockname) | |
311 unlink (delete_lockname); | |
312 error (s1, s2); | |
313 exit (1); | |
314 } | |
315 | |
316 /* Print error message. `s1' is printf control string, `s2' is arg for it. */ | |
317 | |
120 | 318 error (s1, s2, s3) |
319 char *s1, *s2, *s3; | |
23 | 320 { |
321 printf ("movemail: "); | |
120 | 322 printf (s1, s2, s3); |
23 | 323 printf ("\n"); |
324 } | |
325 | |
326 pfatal_with_name (name) | |
327 char *name; | |
328 { | |
329 extern int errno, sys_nerr; | |
330 extern char *sys_errlist[]; | |
331 char *s; | |
332 | |
333 if (errno < sys_nerr) | |
334 s = concat ("", sys_errlist[errno], " for %s"); | |
335 else | |
336 s = "cannot open %s"; | |
337 fatal (s, name); | |
338 } | |
339 | |
447
2e226dcdaf0f
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
351
diff
changeset
|
340 pfatal_and_delete (name) |
2e226dcdaf0f
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
351
diff
changeset
|
341 char *name; |
2e226dcdaf0f
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
351
diff
changeset
|
342 { |
2e226dcdaf0f
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
351
diff
changeset
|
343 extern int errno, sys_nerr; |
2e226dcdaf0f
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
351
diff
changeset
|
344 extern char *sys_errlist[]; |
2e226dcdaf0f
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
351
diff
changeset
|
345 char *s; |
2e226dcdaf0f
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
351
diff
changeset
|
346 |
2e226dcdaf0f
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
351
diff
changeset
|
347 if (errno < sys_nerr) |
2e226dcdaf0f
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
351
diff
changeset
|
348 s = concat ("", sys_errlist[errno], " for %s"); |
2e226dcdaf0f
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
351
diff
changeset
|
349 else |
2e226dcdaf0f
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
351
diff
changeset
|
350 s = "cannot open %s"; |
2e226dcdaf0f
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
351
diff
changeset
|
351 |
2e226dcdaf0f
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
351
diff
changeset
|
352 unlink (name); |
2e226dcdaf0f
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
351
diff
changeset
|
353 fatal (s, name); |
2e226dcdaf0f
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
351
diff
changeset
|
354 } |
2e226dcdaf0f
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
351
diff
changeset
|
355 |
23 | 356 /* Return a newly-allocated string whose contents concatenate those of s1, s2, s3. */ |
357 | |
358 char * | |
359 concat (s1, s2, s3) | |
360 char *s1, *s2, *s3; | |
361 { | |
362 int len1 = strlen (s1), len2 = strlen (s2), len3 = strlen (s3); | |
363 char *result = (char *) xmalloc (len1 + len2 + len3 + 1); | |
364 | |
365 strcpy (result, s1); | |
366 strcpy (result + len1, s2); | |
367 strcpy (result + len1 + len2, s3); | |
368 *(result + len1 + len2 + len3) = 0; | |
369 | |
370 return result; | |
371 } | |
372 | |
373 /* Like malloc but get fatal error if memory is exhausted. */ | |
374 | |
571 | 375 char * |
23 | 376 xmalloc (size) |
571 | 377 unsigned size; |
23 | 378 { |
571 | 379 char *result = malloc (size); |
23 | 380 if (!result) |
381 fatal ("virtual memory exhausted", 0); | |
382 return result; | |
383 } | |
384 | |
385 /* This is the guts of the interface to the Post Office Protocol. */ | |
386 | |
387 #ifdef MAIL_USE_POP | |
388 | |
389 #include <sys/socket.h> | |
390 #include <netinet/in.h> | |
391 #include <netdb.h> | |
392 #include <stdio.h> | |
393 | |
394 #ifdef USG | |
395 #include <fcntl.h> | |
396 /* Cancel substitutions made by config.h for Emacs. */ | |
397 #undef open | |
398 #undef read | |
399 #undef write | |
400 #undef close | |
401 #endif /* USG */ | |
402 | |
403 #define NOTOK (-1) | |
404 #define OK 0 | |
405 #define DONE 1 | |
406 | |
407 char *progname; | |
408 FILE *sfi; | |
409 FILE *sfo; | |
410 char Errmsg[80]; | |
411 | |
412 static int debug = 0; | |
413 | |
120 | 414 char *get_errmsg (); |
415 char *getenv (); | |
416 int mbx_write (); | |
417 | |
418 popmail (user, outfile) | |
419 char *user; | |
420 char *outfile; | |
23 | 421 { |
120 | 422 char *host; |
423 int nmsgs, nbytes; | |
424 char response[128]; | |
425 register int i; | |
426 int mbfi; | |
427 FILE *mbf; | |
23 | 428 |
120 | 429 host = getenv ("MAILHOST"); |
430 if (host == NULL) | |
431 { | |
432 fatal ("no MAILHOST defined"); | |
23 | 433 } |
434 | |
120 | 435 if (pop_init (host) == NOTOK) |
436 { | |
447
2e226dcdaf0f
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
351
diff
changeset
|
437 fatal (Errmsg); |
23 | 438 } |
439 | |
120 | 440 if (getline (response, sizeof response, sfi) != OK) |
441 { | |
447
2e226dcdaf0f
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
351
diff
changeset
|
442 fatal (response); |
23 | 443 } |
444 | |
447
2e226dcdaf0f
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
351
diff
changeset
|
445 if (pop_command ("USER %s", user) == NOTOK |
120 | 446 || pop_command ("RPOP %s", user) == NOTOK) |
447 { | |
448 pop_command ("QUIT"); | |
447
2e226dcdaf0f
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
351
diff
changeset
|
449 fatal (Errmsg); |
23 | 450 } |
451 | |
120 | 452 if (pop_stat (&nmsgs, &nbytes) == NOTOK) |
453 { | |
454 pop_command ("QUIT"); | |
447
2e226dcdaf0f
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
351
diff
changeset
|
455 fatal (Errmsg); |
120 | 456 } |
23 | 457 |
120 | 458 if (!nmsgs) |
459 { | |
460 pop_command ("QUIT"); | |
461 return 0; | |
23 | 462 } |
463 | |
120 | 464 mbfi = open (outfile, O_WRONLY | O_CREAT | O_EXCL, 0666); |
465 if (mbfi < 0) | |
466 { | |
467 pop_command ("QUIT"); | |
447
2e226dcdaf0f
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
351
diff
changeset
|
468 pfatal_and_delete (outfile); |
120 | 469 } |
470 fchown (mbfi, getuid (), -1); | |
471 | |
472 if ((mbf = fdopen (mbfi, "w")) == NULL) | |
473 { | |
474 pop_command ("QUIT"); | |
447
2e226dcdaf0f
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
351
diff
changeset
|
475 pfatal_and_delete (outfile); |
120 | 476 } |
477 | |
478 for (i = 1; i <= nmsgs; i++) | |
479 { | |
480 mbx_delimit_begin (mbf); | |
481 if (pop_retr (i, mbx_write, mbf) != OK) | |
482 { | |
483 pop_command ("QUIT"); | |
484 close (mbfi); | |
447
2e226dcdaf0f
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
351
diff
changeset
|
485 unlink (outfile); |
2e226dcdaf0f
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
351
diff
changeset
|
486 fatal (Errmsg); |
120 | 487 } |
488 mbx_delimit_end (mbf); | |
489 fflush (mbf); | |
490 } | |
491 | |
447
2e226dcdaf0f
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
351
diff
changeset
|
492 if (fsync (mbfi) < 0) |
2e226dcdaf0f
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
351
diff
changeset
|
493 { |
2e226dcdaf0f
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
351
diff
changeset
|
494 pop_command ("QUIT"); |
2e226dcdaf0f
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
351
diff
changeset
|
495 pfatal_and_delete (outfile); |
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 |
2e226dcdaf0f
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
351
diff
changeset
|
498 if (close (mbfi) == -1) |
2e226dcdaf0f
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
351
diff
changeset
|
499 { |
2e226dcdaf0f
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
351
diff
changeset
|
500 pop_command ("QUIT"); |
2e226dcdaf0f
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
351
diff
changeset
|
501 pfatal_and_delete (outfile); |
2e226dcdaf0f
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
351
diff
changeset
|
502 } |
2e226dcdaf0f
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
351
diff
changeset
|
503 |
120 | 504 for (i = 1; i <= nmsgs; i++) |
505 { | |
506 if (pop_command ("DELE %d", i) == NOTOK) | |
507 { | |
447
2e226dcdaf0f
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
351
diff
changeset
|
508 /* Better to ignore this failure. */ |
23 | 509 } |
510 } | |
511 | |
120 | 512 pop_command ("QUIT"); |
447
2e226dcdaf0f
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
351
diff
changeset
|
513 return (0); |
23 | 514 } |
515 | |
120 | 516 pop_init (host) |
517 char *host; | |
23 | 518 { |
120 | 519 register struct hostent *hp; |
520 register struct servent *sp; | |
521 int lport = IPPORT_RESERVED - 1; | |
522 struct sockaddr_in sin; | |
523 register int s; | |
23 | 524 |
120 | 525 hp = gethostbyname (host); |
526 if (hp == NULL) | |
527 { | |
528 sprintf (Errmsg, "MAILHOST unknown: %s", host); | |
529 return NOTOK; | |
23 | 530 } |
531 | |
120 | 532 sp = getservbyname ("pop", "tcp"); |
533 if (sp == 0) | |
534 { | |
535 strcpy (Errmsg, "tcp/pop: unknown service"); | |
536 return NOTOK; | |
23 | 537 } |
538 | |
120 | 539 sin.sin_family = hp->h_addrtype; |
540 bcopy (hp->h_addr, (char *)&sin.sin_addr, hp->h_length); | |
541 sin.sin_port = sp->s_port; | |
542 s = rresvport (&lport); | |
543 if (s < 0) | |
544 { | |
545 sprintf (Errmsg, "error creating socket: %s", get_errmsg ()); | |
546 return NOTOK; | |
23 | 547 } |
548 | |
120 | 549 if (connect (s, (char *)&sin, sizeof sin) < 0) |
550 { | |
551 sprintf (Errmsg, "error during connect: %s", get_errmsg ()); | |
552 close (s); | |
553 return NOTOK; | |
23 | 554 } |
555 | |
120 | 556 sfi = fdopen (s, "r"); |
557 sfo = fdopen (s, "w"); | |
558 if (sfi == NULL || sfo == NULL) | |
559 { | |
560 sprintf (Errmsg, "error in fdopen: %s", get_errmsg ()); | |
561 close (s); | |
562 return NOTOK; | |
23 | 563 } |
564 | |
120 | 565 return OK; |
23 | 566 } |
567 | |
120 | 568 pop_command (fmt, a, b, c, d) |
569 char *fmt; | |
23 | 570 { |
120 | 571 char buf[128]; |
572 char errmsg[64]; | |
573 | |
574 sprintf (buf, fmt, a, b, c, d); | |
23 | 575 |
120 | 576 if (debug) fprintf (stderr, "---> %s\n", buf); |
577 if (putline (buf, Errmsg, sfo) == NOTOK) return NOTOK; | |
23 | 578 |
120 | 579 if (getline (buf, sizeof buf, sfi) != OK) |
580 { | |
581 strcpy (Errmsg, buf); | |
582 return NOTOK; | |
23 | 583 } |
584 | |
447
2e226dcdaf0f
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
351
diff
changeset
|
585 if (debug) |
2e226dcdaf0f
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
351
diff
changeset
|
586 fprintf (stderr, "<--- %s\n", buf); |
120 | 587 if (*buf != '+') |
588 { | |
589 strcpy (Errmsg, buf); | |
590 return NOTOK; | |
591 } | |
592 else | |
593 { | |
594 return OK; | |
23 | 595 } |
596 } | |
597 | |
598 | |
120 | 599 pop_stat (nmsgs, nbytes) |
600 int *nmsgs, *nbytes; | |
23 | 601 { |
120 | 602 char buf[128]; |
23 | 603 |
447
2e226dcdaf0f
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
351
diff
changeset
|
604 if (debug) |
2e226dcdaf0f
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
351
diff
changeset
|
605 fprintf (stderr, "---> STAT\n"); |
2e226dcdaf0f
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
351
diff
changeset
|
606 if (putline ("STAT", Errmsg, sfo) == NOTOK) |
2e226dcdaf0f
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
351
diff
changeset
|
607 return NOTOK; |
23 | 608 |
120 | 609 if (getline (buf, sizeof buf, sfi) != OK) |
610 { | |
611 strcpy (Errmsg, buf); | |
612 return NOTOK; | |
23 | 613 } |
614 | |
120 | 615 if (debug) fprintf (stderr, "<--- %s\n", buf); |
616 if (*buf != '+') | |
617 { | |
618 strcpy (Errmsg, buf); | |
619 return NOTOK; | |
620 } | |
621 else | |
622 { | |
623 sscanf (buf, "+OK %d %d", nmsgs, nbytes); | |
624 return OK; | |
23 | 625 } |
626 } | |
627 | |
120 | 628 pop_retr (msgno, action, arg) |
629 int (*action)(); | |
23 | 630 { |
120 | 631 char buf[128]; |
23 | 632 |
120 | 633 sprintf (buf, "RETR %d", msgno); |
634 if (debug) fprintf (stderr, "%s\n", buf); | |
635 if (putline (buf, Errmsg, sfo) == NOTOK) return NOTOK; | |
23 | 636 |
120 | 637 if (getline (buf, sizeof buf, sfi) != OK) |
638 { | |
639 strcpy (Errmsg, buf); | |
640 return NOTOK; | |
23 | 641 } |
642 | |
120 | 643 while (1) |
644 { | |
645 switch (multiline (buf, sizeof buf, sfi)) | |
646 { | |
23 | 647 case OK: |
120 | 648 (*action)(buf, arg); |
649 break; | |
23 | 650 case DONE: |
120 | 651 return OK; |
23 | 652 case NOTOK: |
120 | 653 strcpy (Errmsg, buf); |
654 return NOTOK; | |
23 | 655 } |
656 } | |
657 } | |
658 | |
120 | 659 getline (buf, n, f) |
660 char *buf; | |
661 register int n; | |
662 FILE *f; | |
23 | 663 { |
120 | 664 register char *p; |
665 int c; | |
23 | 666 |
120 | 667 p = buf; |
668 while (--n > 0 && (c = fgetc (f)) != EOF) | |
669 if ((*p++ = c) == '\n') break; | |
23 | 670 |
120 | 671 if (ferror (f)) |
672 { | |
673 strcpy (buf, "error on connection"); | |
674 return NOTOK; | |
675 } | |
676 | |
677 if (c == EOF && p == buf) | |
678 { | |
679 strcpy (buf, "connection closed by foreign host"); | |
680 return DONE; | |
23 | 681 } |
682 | |
120 | 683 *p = NULL; |
684 if (*--p == '\n') *p = NULL; | |
685 if (*--p == '\r') *p = NULL; | |
686 return OK; | |
23 | 687 } |
688 | |
120 | 689 multiline (buf, n, f) |
690 char *buf; | |
691 register int n; | |
692 FILE *f; | |
23 | 693 { |
447
2e226dcdaf0f
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
351
diff
changeset
|
694 if (getline (buf, n, f) != OK) |
2e226dcdaf0f
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
351
diff
changeset
|
695 return NOTOK; |
120 | 696 if (*buf == '.') |
697 { | |
698 if (*(buf+1) == NULL) | |
447
2e226dcdaf0f
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
351
diff
changeset
|
699 return DONE; |
120 | 700 else |
447
2e226dcdaf0f
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
351
diff
changeset
|
701 strcpy (buf, buf+1); |
23 | 702 } |
120 | 703 return OK; |
23 | 704 } |
705 | |
706 char * | |
120 | 707 get_errmsg () |
23 | 708 { |
120 | 709 extern int errno, sys_nerr; |
710 extern char *sys_errlist[]; | |
711 char *s; | |
23 | 712 |
120 | 713 if (errno < sys_nerr) |
714 s = sys_errlist[errno]; | |
715 else | |
716 s = "unknown error"; | |
717 return (s); | |
23 | 718 } |
719 | |
120 | 720 putline (buf, err, f) |
721 char *buf; | |
722 char *err; | |
723 FILE *f; | |
23 | 724 { |
120 | 725 fprintf (f, "%s\r\n", buf); |
726 fflush (f); | |
727 if (ferror (f)) | |
728 { | |
729 strcpy (err, "lost connection"); | |
730 return NOTOK; | |
23 | 731 } |
120 | 732 return OK; |
23 | 733 } |
734 | |
120 | 735 mbx_write (line, mbf) |
736 char *line; | |
737 FILE *mbf; | |
23 | 738 { |
120 | 739 fputs (line, mbf); |
740 fputc (0x0a, mbf); | |
23 | 741 } |
742 | |
120 | 743 mbx_delimit_begin (mbf) |
744 FILE *mbf; | |
23 | 745 { |
127
762710f7381a
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
120
diff
changeset
|
746 fputs ("\f\n0, unseen,,\n", mbf); |
23 | 747 } |
748 | |
120 | 749 mbx_delimit_end (mbf) |
750 FILE *mbf; | |
23 | 751 { |
120 | 752 putc ('\037', mbf); |
23 | 753 } |
754 | |
755 #endif /* MAIL_USE_POP */ |