home *** CD-ROM | disk | FTP | other *** search
/ 17 Bit Software 4: Phase Four / 17Bit_Phase_Four.iso / files / 3318.dms / 3318.adf / contrib / misc / PGPPager.c next >
Encoding:
C/C++ Source or Header  |  1993-09-17  |  7.3 KB  |  246 lines

  1. echo; /*    Execute to compile!
  2.  
  3. SC PGPPager.c LINK STARTUP=cres PARAMETERS=REGISTERS STRINGMERGE NOSTACKCHECK OPTIMIZE OPTIMIZERSIZE NOOPTIMIZERINLINE OPTIMIZERCOMPLEXITY=5 OPTIMIZERDEPTH=5 OPTIMIZERRECURDEPTH=5
  4. delete PGPPager.lnk PGPPager.o
  5. quit
  6.  
  7. */
  8.  
  9. /* pgppager, designed to be possibly integrated with elm mail reader
  10.  *
  11.  * by Alessandro Bottonelli (c)1993, A.Bottonelli@it12.bull.it
  12.  * version 1.0   - 29/Jan/93
  13.  * version 1.1   - 29/Jun/93 with contribution from Ori Degani
  14.  *                           (ori@marvin.technion.ac.il)
  15.  * version 1.1.1 - 16/Sep/93 modified for AmigaOS by Peter Simons
  16.  *
  17.  * This program is put in the public domain and permission is granted
  18.  * to use it for any purpose, provided this copyright notice is retained.
  19.  * NO WARRANTIES expressed or implied are given. USE THIS PROGRAM AT
  20.  * YOUR OWN RISK!
  21.  *
  22.  * This programs reads from a specified file or from stdin if no file is
  23.  * specified and creates three temporary files (header, encrypted, and
  24.  * trailer) as needed, in order to store the header portion in cleartext,
  25.  * the encrypted portion still in cypher text, and the trailer portion of
  26.  * the clear text. Then, if applicable, the cleartext header is outputted,
  27.  * the encrypted portion is piped through pgp as needed, then the trailer
  28.  * (if any) is outputted. THIS PROCESS IS TRANSPARENT TO NON PGP ENCRYPTED
  29.  * TEXTS
  30.  *
  31.  * For use with ELM, it is necessary to specify the display to be:
  32.  *
  33.  *    <whatever_path>/pgppager | pg
  34.  *
  35.  * in the elm option menu.
  36.  *
  37.  * What's NEW:
  38.  *
  39.  *    1.1 Now handles clear_signed messages (thanks Ori!)
  40.  *
  41.  * WARNING: this program assumes that all messages from pgp with "-f" are
  42.  *          outputted on standard output instead of standard error
  43.  *          as in the original code. Only the messages that are needed
  44.  *          to prompt the user are kept on the standard error even if
  45.  *          PGP is invoked with "-f" option.
  46.  *
  47.  *          This is done in pgp code by testing for -f and touch "pgpout"
  48.  *          variable accordingly, then making sure you identify in the
  49.  *          original PGP code all user prompts and keep them on stderr
  50.  *          no matter whether -f is used or not.
  51.  */
  52.  
  53. #include <stdio.h>
  54. #include <stdlib.h>
  55. #include <string.h>
  56.  
  57. void Exit(int);
  58.  
  59.  
  60. /*
  61.  * you may need to customize the next DEFINEs
  62.  */
  63.  
  64. #define TMPDIR "T:"
  65. #define PGP "PGP -f +BATCHMODE"
  66. #define BEGIN "-----BEGIN PGP MESSAGE----"
  67. #define END "-----END PGP MESSAGE----"
  68. #define BEGINSIG "-----BEGIN PGP SIGNED MESSAGE----"
  69. #define ENDSIG "-----END PGP SIGNATURE----"
  70.  
  71.  
  72. #define CAT "type"              /* -s option to avoid cat complaining */
  73.                                 /* about missing files                */
  74.  
  75. /*
  76.  * program should be invoked as "pgppager [file] | pg"
  77.  * actually no check is made to verify that the program
  78.  * is piped through "pg" or "more", just in case someone
  79.  * wants to redirect it to a file or a printer.
  80.  * BEWARE: redirecting it to a file or printer may be
  81.  * not good for your security if the file or printer level
  82.  * of security is unknown or poor ...
  83.  */
  84.  
  85. /*
  86.  * This variables global to be visible to the Exit() routine
  87.  */
  88.  
  89. char fh[128];           /* Header temporary file                     */
  90. char fe[128];           /* Encrypted/clear_signed temporary file     */
  91. char fr[128];           /* Trailer temporary file                    */
  92.  
  93.         static char _OSVersionString[] = "$VER: PGPPager 1.1.1 (16-Sep-93)\0";
  94.  
  95. main(ac,av)
  96. int ac;
  97. char **av;
  98. {
  99.  
  100.         FILE *fp;       /* File pointer to read file(s) */
  101.         FILE *ft;       /* File pointer to temp file(s) */
  102.         char line[256]; /* Read/Write buffer            */
  103.         char cmd[128];  /* command string               */
  104.  
  105.  
  106.  
  107. /*
  108.  * Must be invoked as "pgppager [file_name]"
  109.  */
  110.  
  111.         if (ac > 2 || !strcmp("?", av[1]))
  112.         {
  113.                 fprintf(stderr,"Usage: %s [file_name]\n", av[0]);
  114.                 Exit(1);
  115.         }
  116.  
  117.  
  118. /*
  119.  * File ( if specified ) must exist and be readable
  120.  */
  121.  
  122.         if ( ac == 2 )
  123.         {
  124.                 if ( ( fp = fopen( av[1], "r") ) == NULL )
  125.                 {
  126.                         fprintf(stderr,"%s: cannot read %s\n", av[0], av[1]);
  127.                         Exit(2);
  128.                 }
  129.         }
  130.         else
  131.                 fp=stdin;       /* if no file specified assume standard input */
  132.  
  133. /*
  134.  * Build temporary file names
  135.  *
  136.  */
  137.  
  138.         sprintf ( fh, "%spgpm.head", TMPDIR);
  139.         sprintf ( fe, "%spgpm.encr", TMPDIR);
  140.         sprintf ( fr, "%spgpm.trai", TMPDIR);
  141.  
  142.  
  143. /*
  144.  * READ file until Eof, redirect to "fe"  only those portions of the
  145.  * file that are between the BEGIN PGP and END PGP ... lines.
  146.  *
  147.  */
  148.         if ( ( ft = fopen ( fh, "w") ) == NULL )
  149.         {
  150.                 fprintf(stderr,"%s: error opening temp file\n", av[0]);
  151.                 Exit(4);
  152.         }
  153.  
  154.         fprintf( ft, "\n");     /* Just to start with a blank line */
  155.  
  156.         while ( fgets( line, 128, fp) != NULL )
  157.         {
  158.                 if ( !strncmp( BEGIN, line, strlen( BEGIN) ) )
  159.                 {
  160.                         fprintf(ft, "\n===PGP ENCRYPTED MESSAGE BEGINS===\n\n");
  161.                         fclose(ft);
  162.  
  163.                         if ( ( ft = fopen ( fe, "w") ) == NULL )
  164.                         {
  165.                                 fprintf(stderr,"%s: error opening temp file\n", av[0]);
  166.                                 Exit(4);
  167.                         }
  168.  
  169.                 }
  170.  
  171.                 if ( !strncmp( BEGINSIG, line, strlen( BEGINSIG) ) )
  172.                 {
  173.                         fprintf(ft, "\n===PGP SIGNED MESSAGE BEGINS===\n\n");
  174.                         fclose(ft);
  175.  
  176.                         if ( ( ft = fopen ( fe, "w") ) == NULL )
  177.                         {
  178.                                 fprintf(stderr,"%s: error opening temp file\n", av[0]);
  179.                                 Exit(4);
  180.                         }
  181.  
  182.                 }
  183.  
  184.                 fprintf( ft, "%s", line);
  185.  
  186.                 if ( !strncmp( END, line, strlen( END) ))
  187.                 {
  188.                         fclose(ft);
  189.  
  190.                         if ( ( ft = fopen ( fr, "w") ) == NULL )
  191.                         {
  192.                                 fprintf(stderr,"%s: error opening temp file\n", av[0]);
  193.                                 Exit(4);
  194.                         }
  195.  
  196.                         fprintf(ft, "\n\n===PGP ENCRYPTED MESSAGE ENDS===\n");
  197.                 }
  198.  
  199.                 if ( !strncmp( ENDSIG, line, strlen( ENDSIG) ))
  200.                 {
  201.                         fclose(ft);
  202.  
  203.                         if ( ( ft = fopen ( fr, "w") ) == NULL )
  204.                         {
  205.                                 fprintf(stderr,"%s: error opening temp file\n", av[0]);
  206.                                 Exit(4);
  207.                         }
  208.  
  209.                         fprintf(ft, "\n\n===PGP SIGNED MESSAGE ENDS===\n");
  210.                 }
  211.         }
  212.  
  213.         fclose(fp);
  214.         fclose(ft);
  215.  
  216.         sprintf( cmd, "%s %s", CAT, fh);
  217.         system(cmd);
  218.         fflush(stdout);
  219.         sprintf( cmd, "%s <%s", PGP, fe);
  220.         system(cmd);
  221.         fflush(stdout);
  222.         sprintf( cmd, "%s %s", CAT, fr);
  223.         system(cmd);
  224.  
  225.         Exit(0);        /* That's all folks ... */
  226. }
  227.  
  228. /*
  229.  * Exit(s) are centralized in this routine to make sure we wipe out
  230.  * temporary files in any case ...
  231.  */
  232.  
  233. void Exit(int n)        /* value to be returned to the shell (0 = OK) */
  234. {
  235. /*
  236.  * Remove temporary files (if any)
  237.  */
  238.  
  239.         unlink(fh);
  240.         unlink(fe);
  241.         unlink(fr);
  242.  
  243.         exit(n);
  244. }
  245.  
  246.