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