Annotation of loncom/lonenc.pm, revision 1.23

1.1       www         1: # The LearningOnline Network
                      2: # URL translation for encrypted filenames
                      3: #
1.23    ! foxr        4: # $Id: lonenc.pm,v 1.22 2007/10/05 18:33:29 albertel Exp $
1.1       www         5: #
                      6: # Copyright Michigan State University Board of Trustees
                      7: #
                      8: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
                      9: #
                     10: # LON-CAPA is free software; you can redistribute it and/or modify
                     11: # it under the terms of the GNU General Public License as published by
                     12: # the Free Software Foundation; either version 2 of the License, or
                     13: # (at your option) any later version.
                     14: #
                     15: # LON-CAPA is distributed in the hope that it will be useful,
                     16: # but WITHOUT ANY WARRANTY; without even the implied warranty of
                     17: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
                     18: # GNU General Public License for more details.
                     19: #
                     20: # You should have received a copy of the GNU General Public License
                     21: # along with LON-CAPA; if not, write to the Free Software
                     22: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
                     23: #
                     24: # /home/httpd/html/adm/gpl.txt
                     25: #
                     26: # http://www.lon-capa.org/
                     27: #
                     28: 
                     29: package Apache::lonenc;
                     30: 
                     31: use strict;
1.11      albertel   32: use Apache::lonnet;
1.1       www        33: use Crypt::IDEA;
1.9       albertel   34: use Time::HiRes qw(gettimeofday);
1.15      www        35: use LONCAPA;
                     36:  
1.23    ! foxr       37: 
        !            38: #
        !            39: #  If a module makes multiple SSI calls and some of the ssi calls result in a
        !            40: #  resource for an encoded URL, and this can be done in an unprivileged role,
        !            41: #  there must be a mechanism t oreset the 'request.enc' environment variable.
        !            42: #  This sub centralizes that mechanism:
        !            43: #
        !            44: sub reset_enc {
        !            45:     $env{'request.enc'} = 0;
        !            46: }
        !            47: 
1.2       www        48: sub encryptseed {
1.19      raeburn    49:     my ($cid) = @_;
                     50:     if (!defined($cid)) {
                     51:         $cid = $env{'request.course.id'};
                     52:     }
                     53:     my $seed;
                     54:     if (defined($cid)) {
                     55:         if (defined$env{'course.'.$cid.'.internal.encseed'}) {
                     56:             $seed = $env{'course.'.$cid.'.internal.encseed'};
                     57:         } else {
                     58:             my %descargs = ( 'one_time' => 1);
                     59:             my %course = 
                     60:                &Apache::lonnet::coursedescription($cid,\%descargs);
1.20      albertel   61:             $seed = $course{'internal.encseed'};
1.19      raeburn    62:         }
                     63:     }
                     64:     if (defined($seed)) {
                     65:         $seed=~s/[^0-9a-f]/0/g;
                     66:         $seed.='0123456789abcdef';
                     67:         $seed=substr($seed.$seed,0,32);
                     68:         return pack("H32",$seed);
                     69:     } else {
1.21      albertel   70:         return pack("H32",1);
1.19      raeburn    71:     }
1.2       www        72: }
                     73: 
1.1       www        74: sub unencrypted {
1.19      raeburn    75:     my ($uri,$cid) = @_;
1.1       www        76:     $uri=~s/^\/enc\/(\d+)\///;
                     77:     my $cmdlength=$1;
1.17      albertel   78:     # strip any added extension
                     79:     $uri=~s/\.[^.]*//;
1.19      raeburn    80:     my $seed=&encryptseed($cid);
1.2       www        81:     unless ($seed) {
1.1       www        82: 	return '/'.$uri;
                     83:     }
1.15      www        84:     $uri=&unescape($uri);
1.2       www        85:     my $cipher=new IDEA $seed;
1.1       www        86:     my $decuri='';
                     87:     for (my $encidx=0;$encidx<length($uri);$encidx+=16) {
                     88: 	$decuri.=$cipher->decrypt(
                     89: 				  pack("H16",substr($uri,$encidx,16))
                     90: 				  );
                     91:     }
1.11      albertel   92:     $env{'request.enc'}=1;
1.9       albertel   93:     $decuri=&remove_noise($decuri);
1.1       www        94:     return substr($decuri,0,$cmdlength);
                     95: }
                     96: 
1.9       albertel   97: # add a randomish character after every 4th caharacter
                     98: sub add_noise {
                     99:     my ($uri)=@_;
                    100:     my @noise=split(/(.)/,(&gettimeofday())[1]);
                    101:     my $noisy;
                    102:     my $i;
                    103:     foreach my $chunk (split(/(....)/,$uri)) {
                    104: 	$noisy.=$chunk;
                    105: 	$noisy.=$noise[($i++)%(scalar@noise)];
                    106:     }
                    107:     return $noisy;
                    108: }
                    109: 
                    110: # remove every fifth character
                    111: sub remove_noise {
                    112:     my ($uri)=@_;
                    113:     my $clean;
                    114:     foreach my $chunk (split(/(....)./,$uri)) { $clean.=$chunk; }
                    115:     return $clean;
                    116: }
                    117: 
1.1       www       118: sub encrypted {
1.12      albertel  119:     my ($uri,$force_enc) = @_;
                    120:     if (!$force_enc && $env{'request.role.adv'}) { return($uri); }
1.2       www       121:     my $seed=&encryptseed();
                    122:     unless ($seed) {
                    123: 	return $uri;
                    124:     }
1.1       www       125:     my $cmdlength=length($uri);
1.9       albertel  126:     # add noise before enc so that that same url's look different
                    127:     $uri=&add_noise($uri);
                    128:     my $noiselength=length($uri);
                    129:     $uri.=time;
1.1       www       130:     my $encuri='';
1.2       www       131:     my $cipher=new IDEA $seed;
1.9       albertel  132:     for (my $encidx=0;$encidx<=$noiselength;$encidx+=8) {
1.1       www       133: 	$encuri.=unpack("H16",
                    134: 			$cipher->encrypt(substr($uri,$encidx,8)));
                    135:     }
1.15      www       136:     return '/enc/'.$cmdlength.'/'.&escape($encuri);
1.1       www       137: }
                    138: 
1.5       albertel  139: sub check_encrypt {
                    140:     my $str=shift;
1.18      raeburn   141:     if (ref($str)) {
                    142:         if ($env{'request.enc'}) { $$str = &Apache::lonenc::encrypted($$str); }
                    143:         return;
                    144:     } else {
                    145:         if ($env{'request.enc'}) { return &Apache::lonenc::encrypted($str); }
                    146:     }
1.5       albertel  147:     return $str;
                    148: }
                    149: 
1.6       albertel  150: sub check_decrypt {
                    151:     my ($str)=@_;
1.7       albertel  152:     if (ref($str)) {
                    153: 	if ($$str=~m|^/enc/|) { $$str=&Apache::lonenc::unencrypted($$str); }
                    154: 	return;
                    155:     }
                    156:     if ($str=~m|^/enc/|) { return &Apache::lonenc::unencrypted($str); }
                    157:     return $str;
1.6       albertel  158: }
                    159: 
1.10      albertel  160: sub encrypt_ref {
1.12      albertel  161:     my ($token,$elements,$force_enc)=@_;
1.10      albertel  162:     my $html;
1.12      albertel  163:     if ($force_enc || $env{'request.enc'}) {
1.10      albertel  164: 	while (my ($name,$value)= each(%{ $elements })) {
1.22      albertel  165: 	    next if (!$value); 
                    166: 	    next if ($value =~ /^\w+:/); # explict javascript: or http: link
1.10      albertel  167: 	    my $href=&Apache::lonnet::hreflocation($Apache::lonxml::pwd[-1],$value);
1.12      albertel  168: 	    if ($href !~ /^http:/) {
1.17      albertel  169: 		# IE really wants an extension
                    170: 		my ($extension) = ($href =~ m/(\.[^.]*)$/);
1.12      albertel  171: 		$href = &Apache::lonenc::encrypted($href,$force_enc);
1.17      albertel  172: 		$href .= $extension;
1.12      albertel  173: 	    }
1.10      albertel  174: 	    $token->[2]->{$name}=$href;
                    175: 	}
1.12      albertel  176: 	delete($token->[2]->{'encrypturl'});
1.10      albertel  177: 	$html = &Apache::edit::rebuild_tag($token);
                    178:     } else {
                    179: 	$html = $token->[4];
                    180:     }
                    181:     return $html;
                    182: }
1.1       www       183: 1;
                    184: __END__
                    185: 
                    186: 
                    187: 
                    188: 
                    189: 
                    190: 
                    191: 

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>