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