1 /* $Header: usersub.c,v 4.0 91/03/20 01:55:56 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 4.0 91/03/20 01:55:56 lwall
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(fdpid,p[0],TRUE);
100 str->str_u.str_useful = pipepid;
101 return fdopen(p[0], "r");
108 /* cheat on stdio if possible */
109 if (rsfp->_cnt > 0 && (*rsfp->_ptr & 0xff) != CRYPT_MAGIC_1)
113 if (ch == CRYPT_MAGIC_1) {
114 if (getc(rsfp) == CRYPT_MAGIC_2) {
115 rsfp = mypfiopen( rsfp, cryptfilter );
116 preprocess = 1; /* force call to pclose when done */
119 fatal( "bad encryption format" );
126 cryptopen(cmd) /* open a (possibly encrypted) program for input */
129 FILE *fil = fopen( cmd, "r" );
131 lastpipefile = Nullfp;
135 int ch = getc( fil );
139 /* Search for the magic cookie that starts the encrypted script,
140 ** while still allowing a few lines of unencrypted text to let
141 ** '#!' and the nih hack both continue to work. (These lines
142 ** will end up being ignored.)
144 while( ch != CRYPT_MAGIC_1 && ch != EOF && lines < 5 && chars < 300 ) {
151 if( ch == CRYPT_MAGIC_1 ) {
152 if( (ch = getc( fil ) ) == CRYPT_MAGIC_2 ) {
153 if( perldb ) fatal("can't debug an encrypted script");
154 /* we found it, decrypt the rest of the file */
155 fil = mypfiopen( fil, cryptfilter );
156 return( lastpipefile = fil );
158 /* if its got MAGIC 1 without MAGIC 2, too bad */
159 fatal( "bad encryption format" );
162 /* this file is not encrypted - rewind and process it normally */
176 if( fil == lastpipefile )
183 #endif /* CRYPTSCRIPT */