Annotation of loncom/lonenc.pm, revision 1.13

1.1       www         1: # The LearningOnline Network
                      2: # URL translation for encrypted filenames
                      3: #
1.13    ! albertel    4: # $Id: lonenc.pm,v 1.12 2006/03/22 19:37:44 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;
                     32: use Apache::Constants qw(:common :remotehost);
1.11      albertel   33: use Apache::lonnet;
1.1       www        34: use Apache::File();
                     35: use Apache::loncommon;
                     36: use Crypt::IDEA;
1.9       albertel   37: use Time::HiRes qw(gettimeofday);
1.1       www        38: 
                     39: sub handler {
                     40:     my $r = shift;
                     41:     my %cookies=CGI::Cookie->parse($r->header_in('Cookie'));
                     42:     my $lonid=$cookies{'lonID'};
                     43:     my $cookie;
                     44:     if ($lonid) {
                     45: 	my $handle=$lonid->value;
                     46:         $handle=~s/\W//g;
                     47:         my $lonidsdir=$r->dir_config('lonIDsDir');
1.11      albertel   48: 	$env{'request.enc'}=1;
1.1       www        49:         if ((-e "$lonidsdir/$handle.id") && ($handle ne '')) {
                     50: # Initialize Environment
                     51:             &Apache::lonnet::transfer_profile_to_env($lonidsdir,$handle);
                     52: # Decrypt URL and redirect
1.8       albertel   53: 	    my $redirect=&unencrypted($r->uri);
                     54: 	    if ($r->args) { $redirect.='?'.$r->args; }
                     55: 	    $r->internal_redirect($redirect);
1.1       www        56: 	    return OK;
                     57: 	} 
                     58:     }
                     59:     return FORBIDDEN;
                     60: }
                     61: 
1.2       www        62: sub encryptseed {
1.11      albertel   63:     my $seed=$env{'course.'.$env{'request.course.id'}.'.internal.encseed'};
1.2       www        64:     $seed=~s/[^0-9a-f]/0/g;
                     65:     $seed.='0123456789abcdef';
                     66:     $seed=substr($seed.$seed,0,32);
                     67:     return pack("H32",$seed);
                     68: }
                     69: 
1.1       www        70: sub unencrypted {
                     71:     my $uri=shift;
                     72:     $uri=~s/^\/enc\/(\d+)\///;
                     73:     my $cmdlength=$1;
1.2       www        74:     my $seed=&encryptseed();
                     75:     unless ($seed) {
1.1       www        76: 	return '/'.$uri;
                     77:     }
                     78:     $uri=&Apache::lonnet::unescape($uri);
1.2       www        79:     my $cipher=new IDEA $seed;
1.1       www        80:     my $decuri='';
                     81:     for (my $encidx=0;$encidx<length($uri);$encidx+=16) {
                     82: 	$decuri.=$cipher->decrypt(
                     83: 				  pack("H16",substr($uri,$encidx,16))
                     84: 				  );
                     85:     }
1.11      albertel   86:     $env{'request.enc'}=1;
1.9       albertel   87:     $decuri=&remove_noise($decuri);
1.1       www        88:     return substr($decuri,0,$cmdlength);
                     89: }
                     90: 
1.9       albertel   91: # add a randomish character after every 4th caharacter
                     92: sub add_noise {
                     93:     my ($uri)=@_;
                     94:     my @noise=split(/(.)/,(&gettimeofday())[1]);
                     95:     my $noisy;
                     96:     my $i;
                     97:     foreach my $chunk (split(/(....)/,$uri)) {
                     98: 	$noisy.=$chunk;
                     99: 	$noisy.=$noise[($i++)%(scalar@noise)];
                    100:     }
                    101:     return $noisy;
                    102: }
                    103: 
                    104: # remove every fifth character
                    105: sub remove_noise {
                    106:     my ($uri)=@_;
                    107:     my $clean;
                    108:     foreach my $chunk (split(/(....)./,$uri)) { $clean.=$chunk; }
                    109:     return $clean;
                    110: }
                    111: 
1.1       www       112: sub encrypted {
1.12      albertel  113:     my ($uri,$force_enc) = @_;
                    114:     if (!$force_enc && $env{'request.role.adv'}) { return($uri); }
1.2       www       115:     my $seed=&encryptseed();
                    116:     unless ($seed) {
                    117: 	return $uri;
                    118:     }
1.1       www       119:     my $cmdlength=length($uri);
1.9       albertel  120:     # add noise before enc so that that same url's look different
                    121:     $uri=&add_noise($uri);
                    122:     my $noiselength=length($uri);
                    123:     $uri.=time;
1.1       www       124:     my $encuri='';
1.2       www       125:     my $cipher=new IDEA $seed;
1.9       albertel  126:     for (my $encidx=0;$encidx<=$noiselength;$encidx+=8) {
1.1       www       127: 	$encuri.=unpack("H16",
                    128: 			$cipher->encrypt(substr($uri,$encidx,8)));
                    129:     }
                    130:     return '/enc/'.$cmdlength.'/'.&Apache::lonnet::escape($encuri);
                    131: }
                    132: 
1.5       albertel  133: sub check_encrypt {
                    134:     my $str=shift;
1.11      albertel  135:     if ($env{'request.enc'}) { return &Apache::lonenc::encrypted($str); }
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:/) {
                    157: 		$href = &Apache::lonenc::encrypted($href,$force_enc);
                    158: 	    }
1.10      albertel  159: 	    $token->[2]->{$name}=$href;
                    160: 	}
1.12      albertel  161: 	delete($token->[2]->{'encrypturl'});
1.10      albertel  162: 	$html = &Apache::edit::rebuild_tag($token);
                    163:     } else {
                    164: 	$html = $token->[4];
                    165:     }
                    166:     return $html;
                    167: }
1.1       www       168: 1;
                    169: __END__
                    170: 
                    171: 
                    172: 
                    173: 
                    174: 
                    175: 
                    176: 

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