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