]> diplodocus.org Git - nmh/blob - uip/dropsbr.c
sbr/dtime.c: Remove struct-assigning twscopy().
[nmh] / uip / dropsbr.c
1 /* dropsbr.c -- create/read/manipulate mail drops
2 *
3 * This code is Copyright (c) 2002, by the authors of nmh. See the
4 * COPYRIGHT file in the root directory of the nmh distribution for
5 * complete copyright information.
6 */
7
8 #include <h/nmh.h>
9 #include <h/utils.h>
10
11 #include <h/mh.h>
12 #include <h/dropsbr.h>
13 #include <h/mts.h>
14 #include <h/tws.h>
15
16 #ifdef NTOHLSWAP
17 # include <netinet/in.h>
18 #else
19 # undef ntohl
20 # define ntohl(n) (n)
21 #endif
22
23 #include <fcntl.h>
24
25 /*
26 * static prototypes
27 */
28 static int mbx_chk_mbox (int);
29 static int mbx_chk_mmdf (int);
30 static int map_open (char *, int);
31
32
33 /*
34 * Main entry point to open/create and lock
35 * a file or maildrop.
36 */
37
38 int
39 mbx_open (char *file, int mbx_style, uid_t uid, gid_t gid, mode_t mode)
40 {
41 int j, count, fd = NOTOK;
42 struct stat st;
43
44 j = 0;
45
46 /* attempt to open and lock file */
47 for (count = 4; count > 0; count--) {
48 int failed_to_lock = 0;
49 if ((fd = lkopenspool (file, O_RDWR | O_CREAT |
50 O_NONBLOCK, mode, &failed_to_lock)) == NOTOK) {
51 if (failed_to_lock) {
52 j = errno;
53 sleep (5);
54 continue;
55 } else {
56 return NOTOK;
57 }
58 }
59
60 /* good file descriptor */
61 break;
62 }
63
64 errno = j;
65
66 /*
67 * Return if we still failed after 4 attempts,
68 * or we just want to skip the sanity checks.
69 */
70 if (fd == NOTOK || mbx_style == OTHER_FORMAT)
71 return fd;
72
73 /*
74 * Do sanity checks on maildrop.
75 */
76 if (fstat (fd, &st) == NOTOK) {
77 /*
78 * The stat failed. So we make sure file
79 * has right ownership/modes
80 */
81 if (chown (file, uid, gid) < 0) {
82 advise (file, "chown");
83 }
84 if (chmod (file, mode) < 0) {
85 advise (file, "chmod");
86 }
87 } else if (st.st_size > (off_t) 0) {
88 int status;
89
90 /* check the maildrop */
91 switch (mbx_style) {
92 case MMDF_FORMAT:
93 default:
94 status = mbx_chk_mmdf (fd);
95 break;
96
97 case MBOX_FORMAT:
98 status = mbx_chk_mbox (fd);
99 break;
100 }
101
102 /* if error, attempt to close it */
103 if (status == NOTOK) {
104 close (fd);
105 return NOTOK;
106 }
107 }
108
109 return fd;
110 }
111
112
113 /*
114 * Check/prepare MBOX style maildrop for appending.
115 */
116
117 static int
118 mbx_chk_mbox (int fd)
119 {
120 /* just seek to the end */
121 if (lseek (fd, (off_t) 0, SEEK_END) == (off_t) NOTOK)
122 return NOTOK;
123
124 return OK;
125 }
126
127
128 /*
129 * Check/prepare MMDF style maildrop for appending.
130 */
131
132 static int
133 mbx_chk_mmdf (int fd)
134 {
135 ssize_t count;
136 char ldelim[BUFSIZ];
137
138 count = strlen (mmdlm2);
139
140 if (lseek (fd, -count, SEEK_END) == (off_t) NOTOK)
141 return NOTOK;
142 if (read (fd, ldelim, count) != count)
143 return NOTOK;
144
145 ldelim[count] = 0;
146
147 if (strcmp (ldelim, mmdlm2)
148 && write (fd, "\n", 1) != 1
149 && write (fd, mmdlm2, count) != count)
150 return NOTOK;
151
152 return OK;
153 }
154
155
156 int
157 mbx_read (FILE *fp, long pos, struct drop **drops)
158 {
159 int len, size;
160 long ld1, ld2;
161 char *bp;
162 char buffer[BUFSIZ];
163 struct drop *cp, *dp, *ep, *pp;
164
165 len = MAXFOLDER;
166 pp = mh_xcalloc(len, sizeof *pp);
167
168 ld1 = (long) strlen (mmdlm1);
169 ld2 = (long) strlen (mmdlm2);
170
171 fseek (fp, pos, SEEK_SET);
172 for (ep = (dp = pp) + len - 1; fgets (buffer, sizeof(buffer), fp);) {
173 size = 0;
174 if (strcmp (buffer, mmdlm1) == 0)
175 pos += ld1, dp->d_start = (long) pos;
176 else {
177 dp->d_start = (long)pos , pos += (long) strlen (buffer);
178 for (bp = buffer; *bp; bp++, size++)
179 if (*bp == '\n')
180 size++;
181 }
182
183 while (fgets (buffer, sizeof(buffer), fp) != NULL)
184 if (strcmp (buffer, mmdlm2) == 0)
185 break;
186 else {
187 pos += (long) strlen (buffer);
188 for (bp = buffer; *bp; bp++, size++)
189 if (*bp == '\n')
190 size++;
191 }
192
193 if (dp->d_start != (long) pos) {
194 dp->d_id = 0;
195 dp->d_size = (long) size;
196 dp->d_stop = pos;
197 dp++;
198 }
199 pos += ld2;
200
201 if (dp >= ep) {
202 int curlen = dp - pp;
203
204 cp = (struct drop *) mh_xrealloc ((char *) pp,
205 (size_t) (len += MAXFOLDER) * sizeof(*pp));
206 dp = cp + curlen, ep = (pp = cp) + len - 1;
207 }
208 }
209
210 if (dp == pp)
211 free(pp);
212 else
213 *drops = pp;
214 return (dp - pp);
215 }
216
217
218 int
219 mbx_write(char *mailbox, int md, FILE *fp, int id, long last,
220 long pos, off_t stop, int mapping, int noisy)
221 {
222 int i, j, size;
223 off_t start;
224 long off;
225 char *cp;
226 char buffer[BUFSIZ];
227
228 off = (long) lseek (md, (off_t) 0, SEEK_CUR);
229 j = strlen (mmdlm1);
230 if (write (md, mmdlm1, j) != j)
231 return NOTOK;
232 start = lseek (md, (off_t) 0, SEEK_CUR);
233 size = 0;
234
235 fseek (fp, pos, SEEK_SET);
236 while (fgets (buffer, sizeof(buffer), fp) && (pos < stop)) {
237 i = strlen (buffer);
238 for ( ; (j = stringdex (mmdlm1, buffer)) >= 0; buffer[j]++)
239 continue;
240 for ( ; (j = stringdex (mmdlm2, buffer)) >= 0; buffer[j]++)
241 continue;
242 if (write (md, buffer, i) != i)
243 return NOTOK;
244 pos += (long) i;
245 if (mapping)
246 for (cp = buffer; i-- > 0; size++)
247 if (*cp++ == '\n')
248 size++;
249 }
250
251 stop = lseek (md, (off_t) 0, SEEK_CUR);
252 j = strlen (mmdlm2);
253 if (write (md, mmdlm2, j) != j)
254 return NOTOK;
255 if (mapping)
256 map_write (mailbox, md, id, last, start, stop, off, size, noisy);
257
258 return OK;
259 }
260
261
262 /*
263 * Append message to end of file or maildrop.
264 */
265
266 int
267 mbx_copy (char *mailbox, int mbx_style, int md, int fd,
268 int mapping, char *text, int noisy)
269 {
270 int i, j, size;
271 off_t start, stop;
272 long pos;
273 char *cp, buffer[BUFSIZ + 1]; /* Space for NUL. */
274 FILE *fp;
275
276 pos = (long) lseek (md, (off_t) 0, SEEK_CUR);
277 size = 0;
278
279 switch (mbx_style) {
280 case MMDF_FORMAT:
281 default:
282 j = strlen (mmdlm1);
283 if (write (md, mmdlm1, j) != j)
284 return NOTOK;
285 start = lseek (md, (off_t) 0, SEEK_CUR);
286
287 if (text) {
288 i = strlen (text);
289 if (write (md, text, i) != i)
290 return NOTOK;
291 for (cp = text; *cp++; size++)
292 if (*cp == '\n')
293 size++;
294 }
295
296 while ((i = read (fd, buffer, sizeof buffer - 1)) > 0) {
297 buffer[i] = '\0'; /* Terminate for stringdex(). */
298
299 for ( ; (j = stringdex (mmdlm1, buffer)) >= 0; buffer[j]++)
300 continue;
301 for ( ; (j = stringdex (mmdlm2, buffer)) >= 0; buffer[j]++)
302 continue;
303 if (write (md, buffer, i) != i)
304 return NOTOK;
305 if (mapping)
306 for (cp = buffer; i-- > 0; size++)
307 if (*cp++ == '\n')
308 size++;
309 }
310
311 stop = lseek (md, (off_t) 0, SEEK_CUR);
312 j = strlen (mmdlm2);
313 if (write (md, mmdlm2, j) != j)
314 return NOTOK;
315 if (mapping)
316 map_write (mailbox, md, 0, (long) 0, start, stop, pos, size, noisy);
317
318 return (i != NOTOK ? OK : NOTOK);
319
320 case MBOX_FORMAT:
321 if ((j = dup (fd)) == NOTOK)
322 return NOTOK;
323 if ((fp = fdopen (j, "r")) == NULL) {
324 close (j);
325 return NOTOK;
326 }
327 start = lseek (md, (off_t) 0, SEEK_CUR);
328
329 /* If text is given, we add it to top of message */
330 if (text) {
331 i = strlen (text);
332 if (write (md, text, i) != i)
333 return NOTOK;
334 for (cp = text; *cp++; size++)
335 if (*cp == '\n')
336 size++;
337 }
338
339 for (j = 0; fgets (buffer, sizeof(buffer), fp) != NULL; j++) {
340
341 /*
342 * Check the first line, and make some changes.
343 */
344 if (j == 0 && !text) {
345 /*
346 * Change the "Return-Path:" field (if in first line)
347 * back to "From ".
348 */
349 if (has_prefix(buffer, "Return-Path:")) {
350 char tmpbuffer[sizeof buffer];
351 char *tp, *ep, *fp;
352
353 strncpy(tmpbuffer, buffer, sizeof(tmpbuffer));
354 ep = tmpbuffer + 13;
355 if (!(fp = strchr(ep + 1, ' ')))
356 fp = strchr(ep + 1, '\n');
357 tp = dctime(dlocaltimenow());
358 snprintf (buffer, sizeof(buffer), "From %.*s %s",
359 (int)(fp - ep), ep, tp);
360 } else if (has_prefix(buffer, "X-Envelope-From:")) {
361 /*
362 * Change the "X-Envelope-From:" field
363 * (if first line) back to "From ".
364 */
365 char tmpbuffer[sizeof buffer];
366 char *ep;
367
368 strncpy(tmpbuffer, buffer, sizeof(tmpbuffer));
369 ep = tmpbuffer + 17;
370 snprintf (buffer, sizeof(buffer), "From %s", ep);
371 } else if (!has_prefix(buffer, "From ")) {
372 /*
373 * If there is already a "From " line,
374 * then leave it alone. Else we add one.
375 */
376 char tmpbuffer[sizeof buffer];
377 char *tp, *ep;
378
379 strncpy(tmpbuffer, buffer, sizeof(tmpbuffer));
380 ep = "nobody@nowhere";
381 tp = dctime(dlocaltimenow());
382 snprintf (buffer, sizeof(buffer), "From %s %s%s", ep, tp, tmpbuffer);
383 }
384 }
385
386 /*
387 * If this is not first line, and begins with
388 * "From ", then prepend line with ">".
389 */
390 if (j != 0 && has_prefix(buffer, "From ")) {
391 if (write (md, ">", 1) < 0) {
392 advise (mailbox, "write");
393 }
394 size++;
395 }
396 i = strlen (buffer);
397 if (write (md, buffer, i) != i) {
398 fclose (fp);
399 return NOTOK;
400 }
401 if (mapping)
402 for (cp = buffer; i-- > 0; size++)
403 if (*cp++ == '\n')
404 size++;
405 }
406 if (write (md, "\n", 1) != 1) {
407 fclose (fp);
408 return NOTOK;
409 }
410 if (mapping)
411 size += 2;
412
413 fclose (fp);
414 lseek (fd, (off_t) 0, SEEK_END);
415 stop = lseek (md, (off_t) 0, SEEK_CUR);
416 if (mapping)
417 map_write (mailbox, md, 0, (long) 0, start, stop, pos, size, noisy);
418
419 return OK;
420 }
421 }
422
423
424 int
425 mbx_size (int md, off_t start, off_t stop)
426 {
427 int i, fd;
428 long pos;
429 FILE *fp;
430
431 if ((fd = dup (md)) == NOTOK || (fp = fdopen (fd, "r")) == NULL) {
432 if (fd != NOTOK)
433 close (fd);
434 return NOTOK;
435 }
436
437 fseek (fp, start, SEEK_SET);
438 for (i = 0, pos = stop - start; pos-- > 0; i++)
439 if (fgetc (fp) == '\n')
440 i++;
441
442 fclose (fp);
443 return i;
444 }
445
446
447 /*
448 * Close and unlock file/maildrop.
449 */
450
451 int
452 mbx_close (char *mailbox, int md)
453 {
454 if (lkclosespool (md, mailbox) == 0)
455 return OK;
456 return NOTOK;
457 }
458
459
460 /*
461 * This function is performed implicitly by getbbent.c:
462 * bb->bb_map = map_name (bb->bb_file);
463 */
464
465 char *
466 map_name (char *file)
467 {
468 char *cp, *dp;
469 static char buffer[BUFSIZ];
470
471 if ((dp = strchr(cp = r1bindex (file, '/'), '.')) == NULL)
472 dp = cp + strlen (cp);
473 if (cp == file)
474 snprintf (buffer, sizeof(buffer), ".%.*s%s", (int)(dp - cp), cp, ".map");
475 else
476 snprintf (buffer, sizeof(buffer), "%.*s.%.*s%s",
477 (int)(cp - file), file, (int)(dp - cp), cp, ".map");
478
479 return buffer;
480 }
481
482
483 int
484 map_read (char *file, long pos, struct drop **drops, int noisy)
485 {
486 int i, md, msgp;
487 char *cp;
488 struct drop d;
489 struct drop *mp, *dp;
490
491 if ((md = open (cp = map_name (file), O_RDONLY)) == NOTOK
492 || map_chk (cp, md, mp = &d, pos, noisy)) {
493 if (md != NOTOK)
494 close (md);
495 return 0;
496 }
497
498 msgp = mp->d_id;
499 dp = mh_xcalloc(msgp + 1, sizeof *dp);
500 memcpy((char *) dp, (char *) mp, sizeof(*dp));
501
502 lseek (md, (off_t) sizeof(*mp), SEEK_SET);
503 if ((i = read (md, (char *) (dp + 1), msgp * sizeof(*dp))) <
504 (int) sizeof(*dp)) {
505 i = 0;
506 free(dp);
507 } else {
508 #ifdef NTOHLSWAP
509 struct drop *tdp;
510 int j;
511
512 for (j = 0, tdp = dp; j < i / sizeof(*dp); j++, tdp++) {
513 tdp->d_id = ntohl(tdp->d_id);
514 tdp->d_size = ntohl(tdp->d_size);
515 tdp->d_start = ntohl(tdp->d_start);
516 tdp->d_stop = ntohl(tdp->d_stop);
517 }
518 #endif
519 *drops = dp;
520 }
521
522 close (md);
523
524 return (i / sizeof(*dp));
525 }
526
527
528 int
529 map_write (char *mailbox, int md, int id, long last, off_t start,
530 off_t stop, long pos, int size, int noisy)
531 {
532 int i;
533 int clear, fd, td;
534 char *file;
535 struct drop *dp;
536 struct drop d1, d2, *rp;
537 FILE *fp;
538 struct stat st;
539
540 if ((fd = map_open (file = map_name (mailbox), md)) == NOTOK)
541 return NOTOK;
542
543 if ((fstat (fd, &st) == OK) && (st.st_size > 0))
544 clear = 0;
545 else
546 clear = 1;
547
548 if (!clear && map_chk (file, fd, &d1, pos, noisy)) {
549 (void) m_unlink (file);
550 mbx_close (file, fd);
551 if ((fd = map_open (file, md)) == NOTOK)
552 return NOTOK;
553 clear++;
554 }
555
556 if (clear) {
557 if ((td = dup (md)) == NOTOK || (fp = fdopen (td, "r")) == NULL) {
558 if (noisy)
559 admonish (file, "unable to %s", td != NOTOK ? "fdopen" : "dup");
560 if (td != NOTOK)
561 close (td);
562 mbx_close (file, fd);
563 return NOTOK;
564 }
565
566 switch (i = mbx_read (fp, 0, &rp)) {
567 case NOTOK:
568 fclose (fp);
569 mbx_close (file, fd);
570 return NOTOK;
571
572 case OK:
573 fclose (fp);
574 break;
575
576 default:
577 d1.d_id = 0;
578 for (dp = rp; i-- >0; dp++) {
579 if (dp->d_start == start)
580 dp->d_id = id;
581 lseek (fd, (off_t) (++d1.d_id * sizeof(*dp)), SEEK_SET);
582 if (write (fd, (char *) dp, sizeof(*dp)) != sizeof(*dp)) {
583 if (noisy)
584 admonish (file, "write error");
585 mbx_close (file, fd);
586 fclose (fp);
587 return NOTOK;
588 }
589 }
590 free(rp);
591 fclose (fp);
592 break;
593 }
594 }
595 else {
596 if (last == 0)
597 last = d1.d_start;
598 dp = &d2;
599 dp->d_id = id;
600 dp->d_size = (long) (size ? size : mbx_size (fd, start, stop));
601 dp->d_start = start;
602 dp->d_stop = stop;
603 lseek (fd, (off_t) (++d1.d_id * sizeof(*dp)), SEEK_SET);
604 if (write (fd, (char *) dp, sizeof(*dp)) != sizeof(*dp)) {
605 if (noisy)
606 admonish (file, "write error");
607 mbx_close (file, fd);
608 return NOTOK;
609 }
610 }
611
612 dp = &d1;
613 dp->d_size = DRVRSN;
614 dp->d_start = (long) last;
615 dp->d_stop = lseek (md, (off_t) 0, SEEK_CUR);
616
617 lseek (fd, (off_t) 0, SEEK_SET);
618 if (write (fd, (char *) dp, sizeof(*dp)) != sizeof(*dp)) {
619 if (noisy)
620 admonish (file, "write error");
621 mbx_close (file, fd);
622 return NOTOK;
623 }
624
625 mbx_close (file, fd);
626
627 return OK;
628 }
629
630
631 static int
632 map_open (char *file, int md)
633 {
634 mode_t mode;
635 struct stat st;
636
637 mode = fstat (md, &st) != NOTOK ? (int) (st.st_mode & 0777) : m_gmprot ();
638 return mbx_open (file, OTHER_FORMAT, st.st_uid, st.st_gid, mode);
639 }
640
641
642 int
643 map_chk (char *file, int fd, struct drop *dp, long pos, int noisy)
644 {
645 ssize_t count;
646 struct drop d, tmpd;
647 struct drop *dl;
648
649 if (read (fd, (char *) &tmpd, sizeof(*dp)) != sizeof(*dp)) {
650 #ifdef notdef
651 inform("%s: missing or partial index, continuing...", file);
652 #endif /* notdef */
653 return NOTOK;
654 }
655 #ifndef NTOHLSWAP
656 *dp = tmpd; /* if ntohl(n)=(n), can use struct assign */
657 #else
658 dp->d_id = ntohl(tmpd.d_id);
659 dp->d_size = ntohl(tmpd.d_size);
660 dp->d_start = ntohl(tmpd.d_start);
661 dp->d_stop = ntohl(tmpd.d_stop);
662 #endif
663
664 if (dp->d_size != DRVRSN) {
665 if (noisy)
666 inform("%s: version mismatch (%d != %d), continuing...", file,
667 dp->d_size, DRVRSN);
668 return NOTOK;
669 }
670
671 if (dp->d_stop != pos) {
672 if (noisy && pos != (long) 0)
673 inform("%s: pointer mismatch or incomplete index (%ld!=%ld), "
674 "continuing...", file, dp->d_stop, (long) pos);
675 return NOTOK;
676 }
677
678 if ((long) ((dp->d_id + 1) * sizeof(*dp)) != (long) lseek (fd, (off_t) 0, SEEK_END)) {
679 if (noisy)
680 inform("%s: corrupt index(1), continuing...", file);
681 return NOTOK;
682 }
683
684 dl = &d;
685 count = strlen (mmdlm2);
686 lseek (fd, (off_t) (dp->d_id * sizeof(*dp)), SEEK_SET);
687 if (read (fd, (char *) dl, sizeof(*dl)) != sizeof(*dl)
688 || (ntohl(dl->d_stop) != dp->d_stop
689 && ntohl(dl->d_stop) + count != dp->d_stop)) {
690 if (noisy)
691 inform("%s: corrupt index(2), continuing...", file);
692 return NOTOK;
693 }
694
695 return OK;
696 }