File:  [LON-CAPA] / loncom / lonenc.pm
Revision 1.11: download - view: text, annotated - select for diffs
Thu Apr 7 06:56:20 2005 UTC (19 years ago) by albertel
Branches: MAIN
CVS tags: version_2_1_X, version_2_1_3, version_2_1_2, version_2_1_1, version_2_1_0, version_2_0_X, version_2_0_99_1, version_2_0_2, version_2_0_1, version_2_0_0, version_1_99_3, version_1_99_2, version_1_99_1_tmcc, version_1_99_1, version_1_99_0_tmcc, version_1_99_0, HEAD
- ENV -> env

    1: # The LearningOnline Network
    2: # URL translation for encrypted filenames
    3: #
    4: # $Id: lonenc.pm,v 1.11 2005/04/07 06:56:20 albertel Exp $
    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);
   33: use Apache::lonnet;
   34: use Apache::File();
   35: use Apache::loncommon;
   36: use Crypt::IDEA;
   37: use Time::HiRes qw(gettimeofday);
   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');
   48: 	$env{'request.enc'}=1;
   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
   53: 	    my $redirect=&unencrypted($r->uri);
   54: 	    if ($r->args) { $redirect.='?'.$r->args; }
   55: 	    $r->internal_redirect($redirect);
   56: 	    return OK;
   57: 	} 
   58:     }
   59:     return FORBIDDEN;
   60: }
   61: 
   62: sub encryptseed {
   63:     my $seed=$env{'course.'.$env{'request.course.id'}.'.internal.encseed'};
   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: 
   70: sub unencrypted {
   71:     my $uri=shift;
   72:     $uri=~s/^\/enc\/(\d+)\///;
   73:     my $cmdlength=$1;
   74:     my $seed=&encryptseed();
   75:     unless ($seed) {
   76: 	return '/'.$uri;
   77:     }
   78:     $uri=&Apache::lonnet::unescape($uri);
   79:     my $cipher=new IDEA $seed;
   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:     }
   86:     $env{'request.enc'}=1;
   87:     $decuri=&remove_noise($decuri);
   88:     return substr($decuri,0,$cmdlength);
   89: }
   90: 
   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: 
  112: sub encrypted {
  113:     my $uri=shift;
  114:     if ($env{'request.role.adv'}) { return($uri); }
  115:     my $seed=&encryptseed();
  116:     unless ($seed) {
  117: 	return $uri;
  118:     }
  119:     my $cmdlength=length($uri);
  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;
  124:     my $encuri='';
  125:     my $cipher=new IDEA $seed;
  126:     for (my $encidx=0;$encidx<=$noiselength;$encidx+=8) {
  127: 	$encuri.=unpack("H16",
  128: 			$cipher->encrypt(substr($uri,$encidx,8)));
  129:     }
  130:     return '/enc/'.$cmdlength.'/'.&Apache::lonnet::escape($encuri);
  131: }
  132: 
  133: sub check_encrypt {
  134:     my $str=shift;
  135:     if ($env{'request.enc'}) { return &Apache::lonenc::encrypted($str); }
  136:     return $str;
  137: }
  138: 
  139: sub check_decrypt {
  140:     my ($str)=@_;
  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;
  147: }
  148: 
  149: sub encrypt_ref {
  150:     my ($token,$elements)=@_;
  151:     my $html;
  152:     if ($env{'request.enc'}) {
  153: 	while (my ($name,$value)= each(%{ $elements })) {
  154: 	    if (!$value) { next; }
  155: 	    my $href=&Apache::lonnet::hreflocation($Apache::lonxml::pwd[-1],$value);
  156: 	    if ($href !~ /^http:/) { $href=&Apache::lonenc::encrypted($href); }
  157: 	    $token->[2]->{$name}=$href;
  158: 	}
  159: 	$html = &Apache::edit::rebuild_tag($token);
  160:     } else {
  161: 	$html = $token->[4];
  162:     }
  163:     return $html;
  164: }
  165: 1;
  166: __END__
  167: 
  168: 
  169: 
  170: 
  171: 
  172: 
  173: 

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