1 /* $Header: usersub.c,v 3.0.1.1 90/08/09 05:40:45 lwall Locked $
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
8 * Revision 3.0.1.1 90/08/09 05:40:45 lwall
9 * patch19: Initial revision
22 * The following is supplied by John MacDonald as a means of decrypting
23 * and executing (presumably proprietary) scripts that have been encrypted
24 * by a (presumably secret) method. The idea is that you supply your own
25 * routine in place of cryptfilter (which is purposefully a very weak
26 * encryption). If an encrypted script is detected, a process is forked
27 * off to run the cryptfilter routine as input to perl.
37 #define CRYPT_MAGIC_1 0xfb
38 #define CRYPT_MAGIC_2 0xf1
45 while( (ch = getc( fil )) != EOF ) {
46 putchar( (ch ^ 0x80) );
51 static FILE *lastpipefile;
61 mypfiopen(fil,func) /* open a pipe to function call for input */
70 fatal("Can't get pipe for decrypt");
73 /* make sure that the child doesn't get anything extra */
77 while ((pipepid = fork()) < 0) {
78 if (errno != EAGAIN) {
82 fatal("Can't fork for decrypt");
99 str = afetch(pidstatary,p[0],TRUE);
100 str_numset(str,(double)pipepid);
102 return fdopen(p[0], "r");
109 /* cheat on stdio if possible */
110 if (rsfp->_cnt > 0 && (*rsfp->_ptr & 0xff) != CRYPT_MAGIC_1)
114 if (ch == CRYPT_MAGIC_1) {
115 if (getc(rsfp) == CRYPT_MAGIC_2) {
116 rsfp = mypfiopen( rsfp, cryptfilter );
117 preprocess = 1; /* force call to pclose when done */
120 fatal( "bad encryption format" );
127 cryptopen(cmd) /* open a (possibly encrypted) program for input */
130 FILE *fil = fopen( cmd, "r" );
132 lastpipefile = Nullfp;
136 int ch = getc( fil );
140 /* Search for the magic cookie that starts the encrypted script,
141 ** while still allowing a few lines of unencrypted text to let
142 ** '#!' and the nih hack both continue to work. (These lines
143 ** will end up being ignored.)
145 while( ch != CRYPT_MAGIC_1 && ch != EOF && lines < 5 && chars < 300 ) {
152 if( ch == CRYPT_MAGIC_1 ) {
153 if( (ch = getc( fil ) ) == CRYPT_MAGIC_2 ) {
154 if( perldb ) fatal("can't debug an encrypted script");
155 /* we found it, decrypt the rest of the file */
156 fil = mypfiopen( fil, cryptfilter );
157 return( lastpipefile = fil );
159 /* if its got MAGIC 1 without MAGIC 2, too bad */
160 fatal( "bad encryption format" );
163 /* this file is not encrypted - rewind and process it normally */
177 if( fil == lastpipefile )
184 #endif /* CRYPTSCRIPT */