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