Annotation of loncom/lonenc.pm, revision 1.15
1.1 www 1: # The LearningOnline Network
2: # URL translation for encrypted filenames
3: #
1.15 ! www 4: # $Id: lonenc.pm,v 1.14 2006/04/07 22:15:34 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 lib '/home/httpd/lib/perl/';
! 36: use LONCAPA;
! 37:
1.2 www 38: sub encryptseed {
1.11 albertel 39: my $seed=$env{'course.'.$env{'request.course.id'}.'.internal.encseed'};
1.2 www 40: $seed=~s/[^0-9a-f]/0/g;
41: $seed.='0123456789abcdef';
42: $seed=substr($seed.$seed,0,32);
43: return pack("H32",$seed);
44: }
45:
1.1 www 46: sub unencrypted {
47: my $uri=shift;
48: $uri=~s/^\/enc\/(\d+)\///;
49: my $cmdlength=$1;
1.2 www 50: my $seed=&encryptseed();
51: unless ($seed) {
1.1 www 52: return '/'.$uri;
53: }
1.15 ! www 54: $uri=&unescape($uri);
1.2 www 55: my $cipher=new IDEA $seed;
1.1 www 56: my $decuri='';
57: for (my $encidx=0;$encidx<length($uri);$encidx+=16) {
58: $decuri.=$cipher->decrypt(
59: pack("H16",substr($uri,$encidx,16))
60: );
61: }
1.11 albertel 62: $env{'request.enc'}=1;
1.9 albertel 63: $decuri=&remove_noise($decuri);
1.1 www 64: return substr($decuri,0,$cmdlength);
65: }
66:
1.9 albertel 67: # add a randomish character after every 4th caharacter
68: sub add_noise {
69: my ($uri)=@_;
70: my @noise=split(/(.)/,(&gettimeofday())[1]);
71: my $noisy;
72: my $i;
73: foreach my $chunk (split(/(....)/,$uri)) {
74: $noisy.=$chunk;
75: $noisy.=$noise[($i++)%(scalar@noise)];
76: }
77: return $noisy;
78: }
79:
80: # remove every fifth character
81: sub remove_noise {
82: my ($uri)=@_;
83: my $clean;
84: foreach my $chunk (split(/(....)./,$uri)) { $clean.=$chunk; }
85: return $clean;
86: }
87:
1.1 www 88: sub encrypted {
1.12 albertel 89: my ($uri,$force_enc) = @_;
90: if (!$force_enc && $env{'request.role.adv'}) { return($uri); }
1.2 www 91: my $seed=&encryptseed();
92: unless ($seed) {
93: return $uri;
94: }
1.1 www 95: my $cmdlength=length($uri);
1.9 albertel 96: # add noise before enc so that that same url's look different
97: $uri=&add_noise($uri);
98: my $noiselength=length($uri);
99: $uri.=time;
1.1 www 100: my $encuri='';
1.2 www 101: my $cipher=new IDEA $seed;
1.9 albertel 102: for (my $encidx=0;$encidx<=$noiselength;$encidx+=8) {
1.1 www 103: $encuri.=unpack("H16",
104: $cipher->encrypt(substr($uri,$encidx,8)));
105: }
1.15 ! www 106: return '/enc/'.$cmdlength.'/'.&escape($encuri);
1.1 www 107: }
108:
1.5 albertel 109: sub check_encrypt {
110: my $str=shift;
1.11 albertel 111: if ($env{'request.enc'}) { return &Apache::lonenc::encrypted($str); }
1.5 albertel 112: return $str;
113: }
114:
1.6 albertel 115: sub check_decrypt {
116: my ($str)=@_;
1.7 albertel 117: if (ref($str)) {
118: if ($$str=~m|^/enc/|) { $$str=&Apache::lonenc::unencrypted($$str); }
119: return;
120: }
121: if ($str=~m|^/enc/|) { return &Apache::lonenc::unencrypted($str); }
122: return $str;
1.6 albertel 123: }
124:
1.10 albertel 125: sub encrypt_ref {
1.12 albertel 126: my ($token,$elements,$force_enc)=@_;
1.10 albertel 127: my $html;
1.12 albertel 128: if ($force_enc || $env{'request.enc'}) {
1.10 albertel 129: while (my ($name,$value)= each(%{ $elements })) {
130: if (!$value) { next; }
131: my $href=&Apache::lonnet::hreflocation($Apache::lonxml::pwd[-1],$value);
1.12 albertel 132: if ($href !~ /^http:/) {
133: $href = &Apache::lonenc::encrypted($href,$force_enc);
134: }
1.10 albertel 135: $token->[2]->{$name}=$href;
136: }
1.12 albertel 137: delete($token->[2]->{'encrypturl'});
1.10 albertel 138: $html = &Apache::edit::rebuild_tag($token);
139: } else {
140: $html = $token->[4];
141: }
142: return $html;
143: }
1.1 www 144: 1;
145: __END__
146:
147:
148:
149:
150:
151:
152:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>