Annotation of loncom/lonenc.pm, revision 1.21

1.1       www         1: # The LearningOnline Network
                      2: # URL translation for encrypted filenames
                      3: #
1.21    ! albertel    4: # $Id: lonenc.pm,v 1.20 2006/12/27 17:11:08 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.2       www        37: sub encryptseed {
1.19      raeburn    38:     my ($cid) = @_;
                     39:     if (!defined($cid)) {
                     40:         $cid = $env{'request.course.id'};
                     41:     }
                     42:     my $seed;
                     43:     if (defined($cid)) {
                     44:         if (defined$env{'course.'.$cid.'.internal.encseed'}) {
                     45:             $seed = $env{'course.'.$cid.'.internal.encseed'};
                     46:         } else {
                     47:             my %descargs = ( 'one_time' => 1);
                     48:             my %course = 
                     49:                &Apache::lonnet::coursedescription($cid,\%descargs);
1.20      albertel   50:             $seed = $course{'internal.encseed'};
1.19      raeburn    51:         }
                     52:     }
                     53:     if (defined($seed)) {
                     54:         $seed=~s/[^0-9a-f]/0/g;
                     55:         $seed.='0123456789abcdef';
                     56:         $seed=substr($seed.$seed,0,32);
                     57:         return pack("H32",$seed);
                     58:     } else {
1.21    ! albertel   59:         return pack("H32",1);
1.19      raeburn    60:     }
1.2       www        61: }
                     62: 
1.1       www        63: sub unencrypted {
1.19      raeburn    64:     my ($uri,$cid) = @_;
1.1       www        65:     $uri=~s/^\/enc\/(\d+)\///;
                     66:     my $cmdlength=$1;
1.17      albertel   67:     # strip any added extension
                     68:     $uri=~s/\.[^.]*//;
1.19      raeburn    69:     my $seed=&encryptseed($cid);
1.2       www        70:     unless ($seed) {
1.1       www        71: 	return '/'.$uri;
                     72:     }
1.15      www        73:     $uri=&unescape($uri);
1.2       www        74:     my $cipher=new IDEA $seed;
1.1       www        75:     my $decuri='';
                     76:     for (my $encidx=0;$encidx<length($uri);$encidx+=16) {
                     77: 	$decuri.=$cipher->decrypt(
                     78: 				  pack("H16",substr($uri,$encidx,16))
                     79: 				  );
                     80:     }
1.11      albertel   81:     $env{'request.enc'}=1;
1.9       albertel   82:     $decuri=&remove_noise($decuri);
1.1       www        83:     return substr($decuri,0,$cmdlength);
                     84: }
                     85: 
1.9       albertel   86: # add a randomish character after every 4th caharacter
                     87: sub add_noise {
                     88:     my ($uri)=@_;
                     89:     my @noise=split(/(.)/,(&gettimeofday())[1]);
                     90:     my $noisy;
                     91:     my $i;
                     92:     foreach my $chunk (split(/(....)/,$uri)) {
                     93: 	$noisy.=$chunk;
                     94: 	$noisy.=$noise[($i++)%(scalar@noise)];
                     95:     }
                     96:     return $noisy;
                     97: }
                     98: 
                     99: # remove every fifth character
                    100: sub remove_noise {
                    101:     my ($uri)=@_;
                    102:     my $clean;
                    103:     foreach my $chunk (split(/(....)./,$uri)) { $clean.=$chunk; }
                    104:     return $clean;
                    105: }
                    106: 
1.1       www       107: sub encrypted {
1.12      albertel  108:     my ($uri,$force_enc) = @_;
                    109:     if (!$force_enc && $env{'request.role.adv'}) { return($uri); }
1.2       www       110:     my $seed=&encryptseed();
                    111:     unless ($seed) {
                    112: 	return $uri;
                    113:     }
1.1       www       114:     my $cmdlength=length($uri);
1.9       albertel  115:     # add noise before enc so that that same url's look different
                    116:     $uri=&add_noise($uri);
                    117:     my $noiselength=length($uri);
                    118:     $uri.=time;
1.1       www       119:     my $encuri='';
1.2       www       120:     my $cipher=new IDEA $seed;
1.9       albertel  121:     for (my $encidx=0;$encidx<=$noiselength;$encidx+=8) {
1.1       www       122: 	$encuri.=unpack("H16",
                    123: 			$cipher->encrypt(substr($uri,$encidx,8)));
                    124:     }
1.15      www       125:     return '/enc/'.$cmdlength.'/'.&escape($encuri);
1.1       www       126: }
                    127: 
1.5       albertel  128: sub check_encrypt {
                    129:     my $str=shift;
1.18      raeburn   130:     if (ref($str)) {
                    131:         if ($env{'request.enc'}) { $$str = &Apache::lonenc::encrypted($$str); }
                    132:         return;
                    133:     } else {
                    134:         if ($env{'request.enc'}) { return &Apache::lonenc::encrypted($str); }
                    135:     }
1.5       albertel  136:     return $str;
                    137: }
                    138: 
1.6       albertel  139: sub check_decrypt {
                    140:     my ($str)=@_;
1.7       albertel  141:     if (ref($str)) {
                    142: 	if ($$str=~m|^/enc/|) { $$str=&Apache::lonenc::unencrypted($$str); }
                    143: 	return;
                    144:     }
                    145:     if ($str=~m|^/enc/|) { return &Apache::lonenc::unencrypted($str); }
                    146:     return $str;
1.6       albertel  147: }
                    148: 
1.10      albertel  149: sub encrypt_ref {
1.12      albertel  150:     my ($token,$elements,$force_enc)=@_;
1.10      albertel  151:     my $html;
1.12      albertel  152:     if ($force_enc || $env{'request.enc'}) {
1.10      albertel  153: 	while (my ($name,$value)= each(%{ $elements })) {
                    154: 	    if (!$value) { next; }
                    155: 	    my $href=&Apache::lonnet::hreflocation($Apache::lonxml::pwd[-1],$value);
1.12      albertel  156: 	    if ($href !~ /^http:/) {
1.17      albertel  157: 		# IE really wants an extension
                    158: 		my ($extension) = ($href =~ m/(\.[^.]*)$/);
1.12      albertel  159: 		$href = &Apache::lonenc::encrypted($href,$force_enc);
1.17      albertel  160: 		$href .= $extension;
1.12      albertel  161: 	    }
1.10      albertel  162: 	    $token->[2]->{$name}=$href;
                    163: 	}
1.12      albertel  164: 	delete($token->[2]->{'encrypturl'});
1.10      albertel  165: 	$html = &Apache::edit::rebuild_tag($token);
                    166:     } else {
                    167: 	$html = $token->[4];
                    168:     }
                    169:     return $html;
                    170: }
1.1       www       171: 1;
                    172: __END__
                    173: 
                    174: 
                    175: 
                    176: 
                    177: 
                    178: 
                    179: 

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