* Fix security hole in mhshowsbr.c which allowed untrusted shell
[mmh] / uip / mhshowsbr.c
1
2 /*
3  * mhshowsbr.c -- routines to display the contents of MIME messages
4  *
5  * $Id$
6  */
7
8 #include <h/mh.h>
9 #include <fcntl.h>
10 #include <h/signals.h>
11 #include <h/md5.h>
12 #include <errno.h>
13 #include <setjmp.h>
14 #include <signal.h>
15 #include <zotnet/mts/mts.h>
16 #include <zotnet/tws/tws.h>
17 #include <h/mime.h>
18 #include <h/mhparse.h>
19
20 #ifdef HAVE_SYS_WAIT_H
21 # include <sys/wait.h>
22 #endif
23
24 /*
25  * Just use sigjmp/longjmp on older machines that
26  * don't have sigsetjmp/siglongjmp.
27  */
28 #ifndef HAVE_SIGSETJMP
29 # define sigjmp_buf jmp_buf
30 # define sigsetjmp(env,mask) setjmp(env)
31 # define siglongjmp(env,val) longjmp(env,val)
32 #endif
33
34 extern int errno;
35 extern int debugsw;
36
37 int pausesw  = 1;
38 int serialsw = 0;
39 int nolist   = 0;
40
41 char *progsw = NULL;
42
43 /* flags for moreproc/header display */
44 int nomore   = 0;
45 char *formsw = NULL;
46
47 pid_t xpid = 0;
48
49 static sigjmp_buf intrenv;
50
51
52 /* termsbr.c */
53 int SOprintf (char *, ...);
54
55 /* mhparse.c */
56 int pidcheck (int);
57
58 /* mhmisc.c */
59 int part_ok (CT, int);
60 int type_ok (CT, int);
61 void content_error (char *, CT, char *, ...);
62 void flush_errors (void);
63
64 /* mhlistsbr.c */
65 int list_switch (CT, int, int, int, int);
66 int list_content (CT, int, int, int, int);
67
68 /*
69  * prototypes
70  */
71 void show_all_messages (CT *);
72 int show_content_aux (CT, int, int, char *, char *);
73
74 /*
75  * static prototypes
76  */
77 static void show_single_message (CT, char *);
78 static void DisplayMsgHeader (CT, char *);
79 static int show_switch (CT, int, int);
80 static int show_content (CT, int, int);
81 static int show_content_aux2 (CT, int, int, char *, char *, int, int, int, int, int);
82 static int show_text (CT, int, int);
83 static int show_multi (CT, int, int);
84 static int show_multi_internal (CT, int, int);
85 static int show_multi_aux (CT, int, int, char *);
86 static int show_message_rfc822 (CT, int, int);
87 static int show_partial (CT, int, int);
88 static int show_external (CT, int, int);
89 static RETSIGTYPE intrser (int);
90
91
92 /*
93  * Top level entry point to show/display a group of messages
94  */
95
96 void
97 show_all_messages (CT *cts)
98 {
99     CT ct, *ctp;
100
101     /*
102      * If form is not specified, then get default form
103      * for showing headers of MIME messages.
104      */
105     if (!formsw)
106         formsw = getcpy (etcpath ("mhl.headers"));
107
108     /*
109      * If form is "mhl.null", suppress display of header.
110      */
111     if (!strcmp (formsw, "mhl.null"))
112         formsw = NULL;
113
114     for (ctp = cts; *ctp; ctp++) {
115         ct = *ctp;
116
117         /* if top-level type is ok, then display message */
118         if (type_ok (ct, 0))
119             show_single_message (ct, formsw);
120     }
121 }
122
123
124 /*
125  * Entry point to show/display a single message
126  */
127
128 static void
129 show_single_message (CT ct, char *form)
130 {
131     sigset_t set, oset;
132
133 #ifdef WAITINT
134     int status;
135 #else
136     union wait status;
137 #endif
138
139     umask (ct->c_umask);
140
141     /*
142      * If you have a format file, then display
143      * the message headers.
144      */
145     if (form)
146         DisplayMsgHeader(ct, form);
147     else
148         xpid = 0;
149
150     /* Show the body of the message */
151     show_switch (ct, 1, 0);
152
153     if (ct->c_fp) {
154         fclose (ct->c_fp);
155         ct->c_fp = NULL;
156     }
157     if (ct->c_ceclosefnx)
158         (*ct->c_ceclosefnx) (ct);
159
160     /* block a few signals */
161     sigemptyset (&set);
162     sigaddset (&set, SIGHUP);
163     sigaddset (&set, SIGINT);
164     sigaddset (&set, SIGQUIT);
165     sigaddset (&set, SIGTERM);
166     SIGPROCMASK (SIG_BLOCK, &set, &oset);
167
168     while (wait (&status) != NOTOK) {
169 #ifdef WAITINT
170         pidcheck (status);
171 #else
172         pidcheck (status.w_status);
173 #endif
174         continue;
175     }
176
177     /* reset the signal mask */
178     SIGPROCMASK (SIG_SETMASK, &oset, &set);
179
180     xpid = 0;
181     flush_errors ();
182 }
183
184
185 /*
186  * Use the mhlproc to show the header fields
187  */
188
189 static void
190 DisplayMsgHeader (CT ct, char *form)
191 {
192     pid_t child_id;
193     int i, vecp;
194     char *vec[8];
195
196     vecp = 0;
197     vec[vecp++] = r1bindex (mhlproc, '/');
198     vec[vecp++] = "-form";
199     vec[vecp++] = form;
200     vec[vecp++] = "-nobody";
201     vec[vecp++] = ct->c_file;
202
203     /*
204      * If we've specified -(no)moreproc,
205      * then just pass that along.
206      */
207     if (nomore) {
208         vec[vecp++] = "-nomoreproc";
209     } else if (progsw) {
210         vec[vecp++] = "-moreproc";
211         vec[vecp++] = progsw;
212     }
213     vec[vecp] = NULL;
214
215     fflush (stdout);
216
217     for (i = 0; (child_id = vfork()) == NOTOK && i < 5; i++)
218         sleep (5);
219
220     switch (child_id) {
221     case NOTOK:
222         adios ("fork", "unable to");
223         /* NOTREACHED */
224
225     case OK:
226         execvp (mhlproc, vec);
227         fprintf (stderr, "unable to exec ");
228         perror (mhlproc);
229         _exit (-1);
230         /* NOTREACHED */
231
232     default:
233         xpid = -child_id;
234         break;
235     }
236 }
237
238
239 /*
240  * Switching routine.  Call the correct routine
241  * based on content type.
242  */
243
244 static int
245 show_switch (CT ct, int serial, int alternate)
246 {
247     switch (ct->c_type) {
248         case CT_MULTIPART:
249             return show_multi (ct, serial, alternate);
250             break;
251
252         case CT_MESSAGE:
253             switch (ct->c_subtype) {
254                 case MESSAGE_PARTIAL:
255                     return show_partial (ct, serial, alternate);
256                     break;
257
258                 case MESSAGE_EXTERNAL:
259                     return show_external (ct, serial, alternate);
260                     break;
261
262                 case MESSAGE_RFC822:
263                 default:
264                     return show_message_rfc822 (ct, serial, alternate);
265                     break;
266             }
267             break;
268
269         case CT_TEXT:
270             return show_text (ct, serial, alternate);
271             break;
272
273         case CT_AUDIO:
274         case CT_IMAGE:
275         case CT_VIDEO:
276         case CT_APPLICATION:
277             return show_content (ct, serial, alternate);
278             break;
279
280         default:
281             adios (NULL, "unknown content type %d", ct->c_type);
282             break;
283     }
284
285     return 0;   /* NOT REACHED */
286 }
287
288
289 /*
290  * Generic method for displaying content
291  */
292
293 static int
294 show_content (CT ct, int serial, int alternate)
295 {
296     char *cp, buffer[BUFSIZ];
297     CI ci = &ct->c_ctinfo;
298
299     /* Check for mhn-show-type/subtype */
300     snprintf (buffer, sizeof(buffer), "%s-show-%s/%s",
301                 invo_name, ci->ci_type, ci->ci_subtype);
302     if ((cp = context_find (buffer)) && *cp != '\0')
303         return show_content_aux (ct, serial, alternate, cp, NULL);
304
305     /* Check for mhn-show-type */
306     snprintf (buffer, sizeof(buffer), "%s-show-%s", invo_name, ci->ci_type);
307     if ((cp = context_find (buffer)) && *cp != '\0')
308         return show_content_aux (ct, serial, alternate, cp, NULL);
309
310     if ((cp = ct->c_showproc))
311         return show_content_aux (ct, serial, alternate, cp, NULL);
312
313     /* complain if we are not a part of a multipart/alternative */
314     if (!alternate)
315         content_error (NULL, ct, "don't know how to display content");
316
317     return NOTOK;
318 }
319
320
321 /*
322  * Parse the display string for displaying generic content
323  */
324
325 int
326 show_content_aux (CT ct, int serial, int alternate, char *cp, char *cracked)
327 {
328     int fd, len, buflen, quoted;
329     int xstdin, xlist, xpause, xtty;
330     char *bp, *pp, *file, buffer[BUFSIZ];
331     CI ci = &ct->c_ctinfo;
332
333     if (!ct->c_ceopenfnx) {
334         if (!alternate)
335             content_error (NULL, ct, "don't know how to decode content");
336
337         return NOTOK;
338     }
339
340     file = NULL;
341     if ((fd = (*ct->c_ceopenfnx) (ct, &file)) == NOTOK)
342         return NOTOK;
343     if (ct->c_showproc && !strcmp (ct->c_showproc, "true"))
344         return (alternate ? DONE : OK);
345     
346     xlist  = 0;
347     xpause = 0;
348     xstdin = 0;
349     xtty   = 0;
350
351     if (cracked) {
352         strncpy (buffer, cp, sizeof(buffer));
353         goto got_command;
354     }
355
356     /* get buffer ready to go */
357     bp = buffer;
358     buflen = sizeof(buffer) - 1;
359     bp[0] = bp[buflen] = '\0';
360     quoted = 0;
361
362     /* Now parse display string */
363     for ( ; *cp && buflen > 0; cp++) {
364         if (*cp == '%') {
365             pp = bp;
366
367             switch (*++cp) {
368             case 'a':
369                 /* insert parameters from Content-Type field */
370             {
371                 char **ap, **ep;
372                 char *s = "";
373
374                 for (ap = ci->ci_attrs, ep = ci->ci_values; *ap; ap++, ep++) {
375                     snprintf (bp, buflen, "%s%s=\"%s\"", s, *ap, *ep);
376                     len = strlen (bp);
377                     bp += len;
378                     buflen -= len;
379                     s = " ";
380                 }
381             }
382             break;
383
384             case 'd':
385                 /* insert content description */
386                 if (ct->c_descr) {
387                     char *s;
388
389                     s = trimcpy (ct->c_descr);
390                     strncpy (bp, s, buflen);
391                     free (s);
392                 }
393                 break;
394
395             case 'e':
396                 /* exclusive execution */
397                 xtty = 1;
398                 break;
399
400             case 'F':
401                 /* %e, %f, and stdin is terminal not content */
402                 xstdin = 1;
403                 xtty = 1;
404                 /* and fall... */
405
406             case 'f':
407                 /* insert filename containing content */
408                 snprintf (bp, buflen, "%s", file);
409                 break;
410
411             case 'p':
412                 /* %l, and pause prior to displaying content */
413                 xpause = pausesw;
414                 /* and fall... */
415
416             case 'l':
417                 /* display listing prior to displaying content */
418                 xlist = !nolist;
419                 break;
420
421             case 's':
422                 /* insert subtype of content */
423                 strncpy (bp, ci->ci_subtype, buflen);
424                 break;
425
426             case '%':
427                 /* insert character % */
428                 goto raw;
429
430             default:
431                 *bp++ = *--cp;
432                 *bp = '\0';
433                 buflen--;
434                 continue;
435             }
436             len = strlen (bp);
437             bp += len;
438             buflen -= len;
439
440             /* Did we actually insert something? */
441             if (bp != pp) {
442                 /* Insert single quote if not inside quotes already */
443                 if (!quoted && buflen) {
444                     len = strlen (pp);
445                     memmove (pp + 1, pp, len);
446                     *pp++ = '\'';
447                     buflen--;
448                     bp++;
449                 }
450                 /* Escape existing quotes */
451                 while ((pp = strchr (pp, '\'')) && buflen > 3) {
452                     len = strlen (pp++);
453                     memmove (pp + 3, pp, len);
454                     *pp++ = '\\';
455                     *pp++ = '\'';
456                     *pp++ = '\'';
457                     buflen -= 3;
458                     bp += 3;
459                 }
460                 /* If pp is still set, that means we ran out of space. */
461                 if (pp)
462                     buflen = 0;
463                 if (!quoted && buflen) {
464                     *bp++ = '\'';
465                     *bp = '\0';
466                     buflen--;
467                 }
468             }
469         } else {
470 raw:
471             *bp++ = *cp;
472             *bp = '\0';
473             buflen--;
474
475             if (*cp == '\'')
476                 quoted = !quoted;
477         }
478     }
479
480     if (buflen <= 0 || (ct->c_termproc && buflen <= strlen(ct->c_termproc))) {
481         /* content_error would provide a more useful error message
482          * here, except that if we got overrun, it probably would
483          * too.
484          */
485         fprintf(stderr, "Buffer overflow constructing show command!\n");
486         return NOTOK;
487     }
488
489     /* use charset string to modify display method */
490     if (ct->c_termproc) {
491         char term[BUFSIZ];
492
493         strncpy (term, buffer, sizeof(term));
494         snprintf (buffer, sizeof(buffer), ct->c_termproc, term);
495     }
496
497 got_command:
498     return show_content_aux2 (ct, serial, alternate, cracked, buffer,
499                               fd, xlist, xpause, xstdin, xtty);
500 }
501
502
503 /*
504  * Routine to actually display the content
505  */
506
507 static int
508 show_content_aux2 (CT ct, int serial, int alternate, char *cracked, char *buffer,
509                    int fd, int xlist, int xpause, int xstdin, int xtty)
510 {
511     pid_t child_id;
512     int i;
513     char *vec[4], exec[BUFSIZ + sizeof "exec "];
514     
515     if (debugsw || cracked) {
516         fflush (stdout);
517
518         fprintf (stderr, "%s msg %s", cracked ? "storing" : "show",
519                  ct->c_file);
520         if (ct->c_partno)
521             fprintf (stderr, " part %s", ct->c_partno);
522         if (cracked)
523             fprintf (stderr, " using command (cd %s; %s)\n", cracked, buffer);
524         else
525             fprintf (stderr, " using command %s\n", buffer);
526     }
527
528     if (xpid < 0 || (xtty && xpid)) {
529         if (xpid < 0)
530             xpid = -xpid;
531         pidcheck(pidwait (xpid, NOTOK));
532         xpid = 0;
533     }
534
535     if (xlist) {
536         char prompt[BUFSIZ];
537
538         if (ct->c_type == CT_MULTIPART)
539             list_content (ct, -1, 1, 0, 0);
540         else
541             list_switch (ct, -1, 1, 0, 0);
542
543         if (xpause && SOprintf ("Press <return> to show content..."))
544             printf ("Press <return> to show content...");
545
546         if (xpause) {
547             int intr;
548             SIGNAL_HANDLER istat;
549
550             istat = SIGNAL (SIGINT, intrser);
551             if ((intr = sigsetjmp (intrenv, 1)) == OK) {
552                 fflush (stdout);
553                 prompt[0] = 0;
554                 read (fileno (stdout), prompt, sizeof(prompt));
555             }
556             SIGNAL (SIGINT, istat);
557             if (intr != OK || prompt[0] == 'n') {
558                 (*ct->c_ceclosefnx) (ct);
559                 return (alternate ? DONE : NOTOK);
560             }
561             if (prompt[0] == 'q') done(OK);
562         }
563     }
564
565     snprintf (exec, sizeof(exec), "exec %s", buffer);
566
567     vec[0] = "/bin/sh";
568     vec[1] = "-c";
569     vec[2] = exec;
570     vec[3] = NULL;
571
572     fflush (stdout);
573
574     for (i = 0; (child_id = vfork ()) == NOTOK && i < 5; i++)
575         sleep (5);
576     switch (child_id) {
577         case NOTOK:
578             advise ("fork", "unable to");
579             (*ct->c_ceclosefnx) (ct);
580             return NOTOK;
581
582         case OK:
583             if (cracked)
584                 chdir (cracked);
585             if (!xstdin)
586                 dup2 (fd, 0);
587             close (fd);
588             execvp ("/bin/sh", vec);
589             fprintf (stderr, "unable to exec ");
590             perror ("/bin/sh");
591             _exit (-1);
592             /* NOTREACHED */
593
594         default:
595             if (!serial) {
596                 ct->c_pid = child_id;
597                 if (xtty)
598                     xpid = child_id;
599             } else {
600                 pidcheck (pidXwait (child_id, NULL));
601             }
602
603             if (fd != NOTOK)
604                 (*ct->c_ceclosefnx) (ct);
605             return (alternate ? DONE : OK);
606     }
607 }
608
609
610 /*
611  * show content of type "text"
612  */
613
614 static int
615 show_text (CT ct, int serial, int alternate)
616 {
617     char *cp, buffer[BUFSIZ];
618     CI ci = &ct->c_ctinfo;
619
620     /* Check for mhn-show-type/subtype */
621     snprintf (buffer, sizeof(buffer), "%s-show-%s/%s",
622                 invo_name, ci->ci_type, ci->ci_subtype);
623     if ((cp = context_find (buffer)) && *cp != '\0')
624         return show_content_aux (ct, serial, alternate, cp, NULL);
625
626     /* Check for mhn-show-type */
627     snprintf (buffer, sizeof(buffer), "%s-show-%s", invo_name, ci->ci_type);
628     if ((cp = context_find (buffer)) && *cp != '\0')
629         return show_content_aux (ct, serial, alternate, cp, NULL);
630
631     /*
632      * Use default method if content is text/plain, or if
633      * if it is not a text part of a multipart/alternative
634      */
635     if (!alternate || ct->c_subtype == TEXT_PLAIN) {
636         snprintf (buffer, sizeof(buffer), "%%p%s '%%F'", progsw ? progsw :
637                 moreproc && *moreproc ? moreproc : "more");
638         cp = (ct->c_showproc = add (buffer, NULL));
639         return show_content_aux (ct, serial, alternate, cp, NULL);
640     }
641
642     return NOTOK;
643 }
644
645
646 /*
647  * show message body of type "multipart"
648  */
649
650 static int
651 show_multi (CT ct, int serial, int alternate)
652 {
653     char *cp, buffer[BUFSIZ];
654     CI ci = &ct->c_ctinfo;
655
656     /* Check for mhn-show-type/subtype */
657     snprintf (buffer, sizeof(buffer), "%s-show-%s/%s",
658                 invo_name, ci->ci_type, ci->ci_subtype);
659     if ((cp = context_find (buffer)) && *cp != '\0')
660         return show_multi_aux (ct, serial, alternate, cp);
661
662     /* Check for mhn-show-type */
663     snprintf (buffer, sizeof(buffer), "%s-show-%s", invo_name, ci->ci_type);
664     if ((cp = context_find (buffer)) && *cp != '\0')
665         return show_multi_aux (ct, serial, alternate, cp);
666
667     if ((cp = ct->c_showproc))
668         return show_multi_aux (ct, serial, alternate, cp);
669
670     /*
671      * Use default method to display this multipart content
672      * if it is not a (nested) part of a multipart/alternative,
673      * or if it is one of the known subtypes of multipart.
674      */
675     if (!alternate || ct->c_subtype != MULTI_UNKNOWN)
676         return show_multi_internal (ct, serial, alternate);
677
678     return NOTOK;
679 }
680
681
682 /*
683  * show message body of subtypes of multipart that
684  * we understand directly (mixed, alternate, etc...)
685  */
686
687 static int
688 show_multi_internal (CT ct, int serial, int alternate)
689 {
690     int alternating, nowalternate, nowserial, result;
691     struct multipart *m = (struct multipart *) ct->c_ctparams;
692     struct part *part;
693     CT p;
694     sigset_t set, oset;
695
696     alternating = 0;
697     nowalternate = alternate;
698
699     if (ct->c_subtype == MULTI_PARALLEL) {
700         nowserial = serialsw;
701     } else if (ct->c_subtype == MULTI_ALTERNATE) {
702         nowalternate = 1;
703         alternating  = 1;
704         nowserial = serial;
705     } else {
706         /*
707          * multipart/mixed
708          * mutlipart/digest
709          * unknown subtypes of multipart (treat as mixed per rfc2046)
710          */
711         nowserial = serial;
712     }
713
714     /* block a few signals */
715     if (!nowserial) {
716         sigemptyset (&set);
717         sigaddset (&set, SIGHUP);
718         sigaddset (&set, SIGINT);
719         sigaddset (&set, SIGQUIT);
720         sigaddset (&set, SIGTERM);
721         SIGPROCMASK (SIG_BLOCK, &set, &oset);
722     }
723
724 /*
725  * alternate   -> we are a part inside an multipart/alternative
726  * alternating -> we are a multipart/alternative 
727  */
728
729     result = alternate ? NOTOK : OK;
730
731     for (part = m->mp_parts; part; part = part->mp_next) {
732         p = part->mp_part;
733
734         if (part_ok (p, 0) && type_ok (p, 0)) {
735             int inneresult;
736
737             inneresult = show_switch (p, nowserial, nowalternate);
738             switch (inneresult) {
739                 case NOTOK:
740                     if (alternate && !alternating) {
741                         result = NOTOK;
742                         goto out;
743                     }
744                     continue;
745
746                 case OK:
747                 case DONE:
748                     if (alternating) {
749                         result = DONE;
750                         break;
751                     }
752                     if (alternate) {
753                         alternate = nowalternate = 0;
754                         if (result == NOTOK)
755                             result = inneresult;
756                     }
757                     continue;
758             }
759             break;
760         }
761     }
762
763     if (alternating && !part) {
764         if (!alternate)
765             content_error (NULL, ct, "don't know how to display any of the contents");
766         result = NOTOK;
767         goto out;
768     }
769
770     if (serial && !nowserial) {
771         pid_t pid;
772         int kids;
773 #ifdef WAITINT
774         int status;
775 #else
776         union wait status;
777 #endif
778
779         kids = 0;
780         for (part = m->mp_parts; part; part = part->mp_next) {
781             p = part->mp_part;
782
783             if (p->c_pid > OK) {
784                 if (kill (p->c_pid, 0) == NOTOK)
785                     p->c_pid = 0;
786                 else
787                     kids++;
788             }
789         }
790
791         while (kids > 0 && (pid = wait (&status)) != NOTOK) {
792 #ifdef WAITINT
793             pidcheck (status);
794 #else
795             pidcheck (status.w_status);
796 #endif
797
798             for (part = m->mp_parts; part; part = part->mp_next) {
799                 p = part->mp_part;
800
801                 if (xpid == pid)
802                     xpid = 0;
803                 if (p->c_pid == pid) {
804                     p->c_pid = 0;
805                     kids--;
806                     break;
807                 }
808             }
809         }
810     }
811
812 out:
813     if (!nowserial) {
814         /* reset the signal mask */
815         SIGPROCMASK (SIG_SETMASK, &oset, &set);
816     }
817
818     return result;
819 }
820
821
822 /*
823  * Parse display string for multipart content
824  * and use external program to display it.
825  */
826
827 static int
828 show_multi_aux (CT ct, int serial, int alternate, char *cp)
829 {
830     int len, buflen, quoted;
831     int xlist, xpause, xtty;
832     char *bp, *pp, *file, buffer[BUFSIZ];
833     struct multipart *m = (struct multipart *) ct->c_ctparams;
834     struct part *part;
835     CI ci = &ct->c_ctinfo;
836     CT p;
837
838     for (part = m->mp_parts; part; part = part->mp_next) {
839         p = part->mp_part;
840
841         if (!p->c_ceopenfnx) {
842             if (!alternate)
843                 content_error (NULL, p, "don't know how to decode content");
844             return NOTOK;
845         }
846
847         if (p->c_storage == NULL) {
848             file = NULL;
849             if ((*p->c_ceopenfnx) (p, &file) == NOTOK)
850                 return NOTOK;
851
852             /* I'm not sure if this is necessary? */
853             p->c_storage = add (file, NULL);
854
855             if (p->c_showproc && !strcmp (p->c_showproc, "true"))
856                 return (alternate ? DONE : OK);
857             (*p->c_ceclosefnx) (p);
858         }
859     }
860
861     xlist     = 0;
862     xpause    = 0;
863     xtty      = 0;
864
865     /* get buffer ready to go */
866     bp = buffer;
867     buflen = sizeof(buffer) - 1;
868     bp[0] = bp[buflen] = '\0';
869     quoted = 0;
870
871     /* Now parse display string */
872     for ( ; *cp; cp++) {
873         if (*cp == '%') {
874             switch (*++cp) {
875             case 'a':
876                 /* insert parameters from Content-Type field */
877             {
878                 char **ap, **ep;
879                 char *s = "";
880
881                 for (ap = ci->ci_attrs, ep = ci->ci_values; *ap; ap++, ep++) {
882                     snprintf (bp, buflen, "%s%s=\"%s\"", s, *ap, *ep);
883                     len = strlen (bp);
884                     bp += len;
885                     buflen -= len;
886                     s = " ";
887                 }
888             }
889             break;
890
891             case 'd':
892                 /* insert content description */
893                 if (ct->c_descr) {
894                     char *s;
895
896                     s = trimcpy (ct->c_descr);
897                     strncpy (bp, s, buflen);
898                     free (s);
899                 }
900                 break;
901
902             case 'e':
903                 /* exclusive execution */
904                 xtty = 1;
905                 break;
906
907             case 'F':
908                 /* %e and %f */
909                 xtty = 1;
910                 /* and fall... */
911
912             case 'f':
913                 /* insert filename(s) containing content */
914             {
915                 char *s = "";
916                         
917                 for (part = m->mp_parts; part; part = part->mp_next) {
918                     p = part->mp_part;
919
920                     snprintf (bp, buflen, "%s'%s'", s, p->c_storage);
921                     len = strlen (bp);
922                     bp += len;
923                     buflen -= len;
924                     s = " ";
925                 }
926             }
927             break;
928
929             case 'p':
930                 /* %l, and pause prior to displaying content */
931                 xpause = pausesw;
932                 /* and fall... */
933
934             case 'l':
935                 /* display listing prior to displaying content */
936                 xlist = !nolist;
937                 break;
938
939             case 's':
940                 /* insert subtype of content */
941                 strncpy (bp, ci->ci_subtype, buflen);
942                 break;
943
944             case '%':
945                 /* insert character % */
946                 goto raw;
947
948             default:
949                 *bp++ = *--cp;
950                 *bp = '\0';
951                 buflen--;
952                 continue;
953             }
954             len = strlen (bp);
955             bp += len;
956             buflen -= len;
957
958             /* Did we actually insert something? */
959             if (bp != pp) {
960                 /* Insert single quote if not inside quotes already */
961                 if (!quoted && buflen) {
962                     len = strlen (pp);
963                     memmove (pp + 1, pp, len);
964                     *pp++ = '\'';
965                     buflen--;
966                     bp++;
967                 }
968                 /* Escape existing quotes */
969                 while ((pp = strchr (pp, '\'')) && buflen > 3) {
970                     len = strlen (pp++);
971                     memmove (pp + 3, pp, len);
972                     *pp++ = '\\';
973                     *pp++ = '\'';
974                     *pp++ = '\'';
975                     buflen -= 3;
976                     bp += 3;
977                 }
978                 /* If pp is still set, that means we ran out of space. */
979                 if (pp)
980                     buflen = 0;
981                 if (!quoted && buflen) {
982                     *bp++ = '\'';
983                     *bp = '\0';
984                     buflen--;
985                 }
986             }
987         } else {
988 raw:
989             *bp++ = *cp;
990             *bp = '\0';
991             buflen--;
992
993             if (*cp == '\'')
994                 quoted = !quoted;
995         }
996     }
997
998     if (buflen <= 0 || (ct->c_termproc && buflen <= strlen(ct->c_termproc))) {
999         /* content_error would provide a more useful error message
1000          * here, except that if we got overrun, it probably would
1001          * too.
1002          */
1003         fprintf(stderr, "Buffer overflow constructing show command!\n");
1004         return NOTOK;
1005     }
1006
1007     /* use charset string to modify display method */
1008     if (ct->c_termproc) {
1009         char term[BUFSIZ];
1010
1011         strncpy (term, buffer, sizeof(term));
1012         snprintf (buffer, sizeof(buffer), ct->c_termproc, term);
1013     }
1014
1015     return show_content_aux2 (ct, serial, alternate, NULL, buffer,
1016                               NOTOK, xlist, xpause, 0, xtty);
1017 }
1018
1019
1020 /*
1021  * show content of type "message/rfc822"
1022  */
1023
1024 static int
1025 show_message_rfc822 (CT ct, int serial, int alternate)
1026 {
1027     char *cp, buffer[BUFSIZ];
1028     CI ci = &ct->c_ctinfo;
1029
1030     /* Check for mhn-show-type/subtype */
1031     snprintf (buffer, sizeof(buffer), "%s-show-%s/%s",
1032                 invo_name, ci->ci_type, ci->ci_subtype);
1033     if ((cp = context_find (buffer)) && *cp != '\0')
1034         return show_content_aux (ct, serial, alternate, cp, NULL);
1035
1036     /* Check for mhn-show-type */
1037     snprintf (buffer, sizeof(buffer), "%s-show-%s", invo_name, ci->ci_type);
1038     if ((cp = context_find (buffer)) && *cp != '\0')
1039         return show_content_aux (ct, serial, alternate, cp, NULL);
1040
1041     if ((cp = ct->c_showproc))
1042         return show_content_aux (ct, serial, alternate, cp, NULL);
1043
1044     /* default method for message/rfc822 */
1045     if (ct->c_subtype == MESSAGE_RFC822) {
1046         cp = (ct->c_showproc = add ("%pshow -file '%F'", NULL));
1047         return show_content_aux (ct, serial, alternate, cp, NULL);
1048     }
1049
1050     /* complain if we are not a part of a multipart/alternative */
1051     if (!alternate)
1052         content_error (NULL, ct, "don't know how to display content");
1053
1054     return NOTOK;
1055 }
1056
1057
1058 /*
1059  * Show content of type "message/partial".
1060  */
1061
1062 static int
1063 show_partial (CT ct, int serial, int alternate)
1064 {
1065     content_error (NULL, ct,
1066         "in order to display this message, you must reassemble it");
1067     return NOTOK;
1068 }
1069
1070
1071 /*
1072  * Show content of type "message/external".
1073  *
1074  * THE ERROR CHECKING IN THIS ONE IS NOT DONE YET.
1075  */
1076
1077 static int
1078 show_external (CT ct, int serial, int alternate)
1079 {
1080     struct exbody *e = (struct exbody *) ct->c_ctparams;
1081     CT p = e->eb_content;
1082
1083     if (!type_ok (p, 0))
1084         return OK;
1085
1086     return show_switch (p, serial, alternate);
1087
1088 #if 0
1089     content_error (NULL, p, "don't know how to display content");
1090     return NOTOK;
1091 #endif
1092 }
1093
1094
1095 static RETSIGTYPE
1096 intrser (int i)
1097 {
1098 #ifndef RELIABLE_SIGNALS
1099     SIGNAL (SIGINT, intrser);
1100 #endif
1101
1102     putchar ('\n');
1103     siglongjmp (intrenv, DONE);
1104 }