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