home *** CD-ROM | disk | FTP | other *** search
/ APDL Public Domain 1 / APDL_PD1A.iso / program / language / perl / Source / C / Usersub < prev    next >
Encoding:
Text File  |  1990-11-07  |  3.7 KB  |  188 lines

  1. /* $Header: usersub.c,v 3.0.1.2 90/10/16 11:22:04 lwall Locked $
  2.  *
  3.  *  This file contains stubs for routines that the user may define to
  4.  *  set up glue routines for C libraries or to decrypt encrypted scripts
  5.  *  for execution.
  6.  *
  7.  * $Log:    usersub.c,v $
  8.  * Revision 3.0.1.2  90/10/16  11:22:04  lwall
  9.  * patch29: added waitpid
  10.  * 
  11.  * Revision 3.0.1.1  90/08/09  05:40:45  lwall
  12.  * patch19: Initial revision
  13.  * 
  14.  */
  15.  
  16. #include "EXTERN.h"
  17. #include "perl.h"
  18.  
  19. void
  20. userinit()
  21. {
  22. }
  23.  
  24. /*
  25.  * The following is supplied by John MacDonald as a means of decrypting
  26.  * and executing (presumably proprietary) scripts that have been encrypted
  27.  * by a (presumably secret) method.  The idea is that you supply your own
  28.  * routine in place of cryptfilter (which is purposefully a very weak
  29.  * encryption).  If an encrypted script is detected, a process is forked
  30.  * off to run the cryptfilter routine as input to perl.
  31.  */
  32.  
  33. #ifdef CRYPTSCRIPT
  34.  
  35. #include <signal.h>
  36. #ifdef I_VFORK
  37. #include <vfork.h>
  38. #endif
  39.  
  40. #define    CRYPT_MAGIC_1    0xfb
  41. #define    CRYPT_MAGIC_2    0xf1
  42.  
  43. cryptfilter( fil )
  44. FILE *    fil;
  45. {
  46.     int    ch;
  47.  
  48.     while( (ch = getc( fil )) != EOF ) {
  49.     putchar( (ch ^ 0x80) );
  50.     }
  51. }
  52.  
  53. #ifndef MSDOS
  54. static FILE    *lastpipefile;
  55. static int    pipepid;
  56.  
  57. #ifdef VOIDSIG
  58. #  define    VOID    void
  59. #else
  60. #  define    VOID    int
  61. #endif
  62.  
  63. FILE *
  64. mypfiopen(fil,func)        /* open a pipe to function call for input */
  65. FILE    *fil;
  66. VOID    (*func)();
  67. {
  68.     int p[2];
  69.     STR *str;
  70.  
  71.     if (pipe(p) < 0) {
  72.     fclose( fil );
  73.     fatal("Can't get pipe for decrypt");
  74.     }
  75.  
  76.     /* make sure that the child doesn't get anything extra */
  77.     fflush(stdout);
  78.     fflush(stderr);
  79.  
  80.     while ((pipepid = fork()) < 0) {
  81.     if (errno != EAGAIN) {
  82.         close(p[0]);
  83.         close(p[1]);
  84.         fclose( fil );
  85.         fatal("Can't fork for decrypt");
  86.     }
  87.     sleep(5);
  88.     }
  89.     if (pipepid == 0) {
  90.     close(p[0]);
  91.     if (p[1] != 1) {
  92.         dup2(p[1], 1);
  93.         close(p[1]);
  94.     }
  95.     (*func)(fil);
  96.     fflush(stdout);
  97.     fflush(stderr);
  98.     _exit(0);
  99.     }
  100.     close(p[1]);
  101.     fclose(fil);
  102.     str = afetch(fdpid,p[0],TRUE);
  103.     str->str_u.str_useful = pipepid;
  104.     return fdopen(p[0], "r");
  105. }
  106.  
  107. cryptswitch()
  108. {
  109.     int ch;
  110. #ifdef STDSTDIO
  111.     /* cheat on stdio if possible */
  112.     if (rsfp->_cnt > 0 && (*rsfp->_ptr & 0xff) != CRYPT_MAGIC_1)
  113.     return;
  114. #endif
  115.     ch = getc(rsfp);
  116.     if (ch == CRYPT_MAGIC_1) {
  117.     if (getc(rsfp) == CRYPT_MAGIC_2) {
  118.         rsfp = mypfiopen( rsfp, cryptfilter );
  119.         preprocess = 1;    /* force call to pclose when done */
  120.     }
  121.     else
  122.         fatal( "bad encryption format" );
  123.     }
  124.     else
  125.     ungetc(ch,rsfp);
  126. }
  127.  
  128. FILE *
  129. cryptopen(cmd)        /* open a (possibly encrypted) program for input */
  130. char    *cmd;
  131. {
  132.     FILE    *fil = fopen( cmd, "r" );
  133.  
  134.     lastpipefile = Nullfp;
  135.     pipepid = 0;
  136.  
  137.     if( fil ) {
  138.     int    ch = getc( fil );
  139.     int    lines = 0;
  140.     int    chars = 0;
  141.  
  142.     /* Search for the magic cookie that starts the encrypted script,
  143.     ** while still allowing a few lines of unencrypted text to let
  144.     ** '#!' and the nih hack both continue to work.  (These lines
  145.     ** will end up being ignored.)
  146.     */
  147.     while( ch != CRYPT_MAGIC_1 && ch != EOF && lines < 5 && chars < 300 ) {
  148.         if( ch == '\n' )
  149.         ++lines;
  150.         ch = getc( fil );
  151.         ++chars;
  152.     }
  153.  
  154.     if( ch == CRYPT_MAGIC_1 ) {
  155.         if( (ch = getc( fil ) ) == CRYPT_MAGIC_2 ) {
  156.         if( perldb ) fatal("can't debug an encrypted script");
  157.         /* we found it, decrypt the rest of the file */
  158.         fil = mypfiopen( fil, cryptfilter );
  159.         return( lastpipefile = fil );
  160.         } else
  161.         /* if its got MAGIC 1 without MAGIC 2, too bad */
  162.         fatal( "bad encryption format" );
  163.     }
  164.  
  165.     /* this file is not encrypted - rewind and process it normally */
  166.     rewind( fil );
  167.     }
  168.  
  169.     return( fil );
  170. }
  171.  
  172. VOID
  173. cryptclose(fil)
  174. FILE    *fil;
  175. {
  176.     if( fil == Nullfp )
  177.     return;
  178.  
  179.     if( fil == lastpipefile )
  180.     mypclose( fil );
  181.     else
  182.     fclose( fil );
  183. }
  184. #endif /* !MSDOS */
  185.  
  186. #endif /* CRYPTSCRIPT */
  187.  
  188.