home *** CD-ROM | disk | FTP | other *** search
/ back2roots/padua / padua.7z / padua / lang / perl4.035.V010.lzh / perl4.035 / src / usersub.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-08-12  |  2.9 KB  |  150 lines

  1. /* $RCSfile: usersub.c,v $$Revision: 4.0.1.2 $$Date: 92/06/08 16:04:24 $
  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 4.0.1.2  92/06/08  16:04:24  lwall
  9.  * patch20: removed implicit int declarations on functions
  10.  * 
  11.  * Revision 4.0.1.1  91/11/11  16:47:17  lwall
  12.  * patch19: deleted some unused functions from usersub.c
  13.  * 
  14.  * Revision 4.0  91/03/20  01:55:56  lwall
  15.  * 4.0 baseline.
  16.  * 
  17.  */
  18.  
  19. #include "EXTERN.h"
  20. #include "perl.h"
  21.  
  22. #ifdef AMIGA
  23. #include <unistd.h>
  24. #endif
  25.  
  26. int
  27. userinit()
  28. {
  29.     return 0;
  30. }
  31.  
  32. /*
  33.  * The following is supplied by John Macdonald as a means of decrypting
  34.  * and executing (presumably proprietary) scripts that have been encrypted
  35.  * by a (presumably secret) method.  The idea is that you supply your own
  36.  * routine in place of cryptfilter (which is purposefully a very weak
  37.  * encryption).  If an encrypted script is detected, a process is forked
  38.  * off to run the cryptfilter routine as input to perl.
  39.  */
  40.  
  41. #ifdef CRYPTSCRIPT
  42.  
  43. #include <signal.h>
  44. #ifdef I_VFORK
  45. #include <vfork.h>
  46. #endif
  47.  
  48. #ifdef CRYPTLOCAL
  49.  
  50. #include "cryptlocal.h"
  51.  
  52. #else    /* ndef CRYPTLOCAL */
  53.  
  54. #define    CRYPT_MAGIC_1    0xfb
  55. #define    CRYPT_MAGIC_2    0xf1
  56.  
  57. void
  58. cryptfilter( fil )
  59. FILE *    fil;
  60. {
  61.     int    ch;
  62.  
  63.     while( (ch = getc( fil )) != EOF ) {
  64.     putchar( (ch ^ 0x80) );
  65.     }
  66. }
  67.  
  68. #endif    /* CRYPTLOCAL */
  69.  
  70. #ifndef MSDOS
  71. static FILE    *lastpipefile;
  72. static int    pipepid;
  73.  
  74. #ifdef VOIDSIG
  75. #  define    VOID    void
  76. #else
  77. #  define    VOID    int
  78. #endif
  79.  
  80. FILE *
  81. mypfiopen(fil,func)        /* open a pipe to function call for input */
  82. FILE    *fil;
  83. VOID    (*func)();
  84. {
  85.     int p[2];
  86.     STR *str;
  87.  
  88.     if (pipe(p) < 0) {
  89.     fclose( fil );
  90.     fatal("Can't get pipe for decrypt");
  91.     }
  92.  
  93.     /* make sure that the child doesn't get anything extra */
  94.     fflush(stdout);
  95.     fflush(stderr);
  96.  
  97.     while ((pipepid = fork()) < 0) {
  98.     if (errno != EAGAIN) {
  99.         close(p[0]);
  100.         close(p[1]);
  101.         fclose( fil );
  102.         fatal("Can't fork for decrypt");
  103.     }
  104.     sleep(5);
  105.     }
  106.     if (pipepid == 0) {
  107.     close(p[0]);
  108.     if (p[1] != 1) {
  109.         dup2(p[1], 1);
  110.         close(p[1]);
  111.     }
  112.     (*func)(fil);
  113.     fflush(stdout);
  114.     fflush(stderr);
  115.     _exit(0);
  116.     }
  117.     close(p[1]);
  118.     close(fileno(fil));
  119.     fclose(fil);
  120.     str = afetch(fdpid,p[0],TRUE);
  121.     str->str_u.str_useful = pipepid;
  122.     return fdopen(p[0], "r");
  123. }
  124.  
  125. void
  126. cryptswitch()
  127. {
  128.     int ch;
  129. #ifdef STDSTDIO
  130.     /* cheat on stdio if possible */
  131.     if (rsfp->_cnt > 0 && (*rsfp->_ptr & 0xff) != CRYPT_MAGIC_1)
  132.     return;
  133. #endif
  134.     ch = getc(rsfp);
  135.     if (ch == CRYPT_MAGIC_1) {
  136.     if (getc(rsfp) == CRYPT_MAGIC_2) {
  137.         if( perldb ) fatal("can't debug an encrypted script");
  138.         rsfp = mypfiopen( rsfp, cryptfilter );
  139.         preprocess = 1;    /* force call to pclose when done */
  140.     }
  141.     else
  142.         fatal( "bad encryption format" );
  143.     }
  144.     else
  145.     ungetc(ch,rsfp);
  146. }
  147. #endif /* !MSDOS */
  148.  
  149. #endif /* CRYPTSCRIPT */
  150.