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