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