Annotation of loncom/lonnet/perl/lonnet.pm, revision 1.180
1.1 albertel 1: # The LearningOnline Network
2: # TCP networking package
1.12 www 3: #
1.180 ! albertel 4: # $Id: lonnet.pm,v 1.179 2001/11/29 21:54:56 www Exp $
1.178 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: #
1.169 harris41 28: # 6/1/99,6/2,6/10,6/11,6/12,6/14,6/26,6/28,6/29,6/30,
29: # 7/1,7/2,7/9,7/10,7/12,7/14,7/15,7/19,
30: # 11/8,11/16,11/18,11/22,11/23,12/22,
31: # 01/06,01/13,02/24,02/28,02/29,
32: # 03/01,03/02,03/06,03/07,03/13,
33: # 04/05,05/29,05/31,06/01,
34: # 06/05,06/26 Gerd Kortemeyer
35: # 06/26 Ben Tyszka
36: # 06/30,07/15,07/17,07/18,07/20,07/21,07/22,07/25 Gerd Kortemeyer
37: # 08/14 Ben Tyszka
38: # 08/22,08/28,08/31,09/01,09/02,09/04,09/05,09/25,09/28,09/30 Gerd Kortemeyer
39: # 10/04 Gerd Kortemeyer
40: # 10/04 Guy Albertelli
41: # 10/06,10/09,10/10,10/11,10/14,10/20,10/23,10/25,10/26,10/27,10/28,10/29,
42: # 10/30,10/31,
43: # 11/2,11/14,11/15,11/16,11/20,11/21,11/22,11/25,11/27,
44: # 12/02,12/12,12/13,12/14,12/28,12/29 Gerd Kortemeyer
45: # 05/01/01 Guy Albertelli
46: # 05/01,06/01,09/01 Gerd Kortemeyer
47: # 09/01 Guy Albertelli
48: # 09/01,10/01,11/01 Gerd Kortemeyer
49: # YEAR=2001
50: # 02/27/01 Scott Harrison
51: # 3/2 Gerd Kortemeyer
52: # 3/15,3/19 Scott Harrison
53: # 3/19,3/20 Gerd Kortemeyer
54: # 3/22,3/27,4/2,4/16,4/17 Scott Harrison
55: # 5/26,5/28 Gerd Kortemeyer
56: # 5/30 H. K. Ng
57: # 6/1 Gerd Kortemeyer
58: # July Guy Albertelli
59: # 8/4,8/7,8/8,8/9,8/11,8/16,8/17,8/18,8/20,8/23,9/20,9/21,9/26,
60: # 10/2 Gerd Kortemeyer
61: # 10/5,10/10,11/13,11/15 Scott Harrison
1.179 www 62: # 11/17,11/20,11/22,11/29 Gerd Kortemeyer
1.169 harris41 63: #
1.180 ! albertel 64: # $Id: lonnet.pm,v 1.179 2001/11/29 21:54:56 www Exp $
1.171 www 65: #
1.169 harris41 66: ###
67:
1.12 www 68: # Functions for use by content handlers:
69: #
1.112 harris41 70: # metadata_query(sql-query-string,custom-metadata-regex) :
71: # returns file handle of where sql and
72: # regex results will be stored for query
1.12 www 73: # plaintext(short) : plain text explanation of short term
1.25 www 74: # fileembstyle(ext) : embed style in page for file extension
75: # filedescription(ext) : descriptor text for file extension
1.29 www 76: # allowed(short,url) : returns codes for allowed actions
77: # F: full access
78: # U,I,K: authentication modes (cxx only)
79: # '': forbidden
80: # 1: user needs to choose course
81: # 2: browse allowed
1.21 www 82: # definerole(rolename,sys,dom,cou) : define a custom role rolename
1.103 harris41 83: # set privileges in format of lonTabs/roles.tab for
1.21 www 84: # system, domain and course level,
85: # assignrole(udom,uname,url,role,end,start) : give a role to a user for the
86: # level given by url. Optional start and end dates
87: # (leave empty string or zero for "no date")
88: # assigncustomrole (udom,uname,url,rdom,rnam,rolename,end,start) : give a
89: # custom role to a user for the level given by url.
90: # Specify name and domain of role author, and role name
91: # revokerole (udom,uname,url,role) : Revoke a role for url
92: # revokecustomrole (udom,uname,url,rdom,rnam,rolename) : Revoke a custom role
1.24 www 93: # appenv(hash) : adds hash to session environment
1.56 www 94: # delenv(varname) : deletes all environment entries starting with varname
1.124 www 95: # store(hashref,symb,courseid,udom,uname)
1.122 albertel 96: # : stores hash permanently for this url
97: # hashref needs to be given, and should be a \%hashname
98: # the remaining args aren't required and if they aren't
99: # passed or are '' they will be derived from the ENV
1.124 www 100: # cstore(hashref,symb,courseid,udom,uname)
1.122 albertel 101: # : same as store but uses the critical interface to
102: # guarentee a store
1.124 www 103: # restore(symb,courseid,udom,uname)
1.122 albertel 104: # : returns hash for this symb, all args are optional
1.124 www 105: # if they aren't given they will be derived from the
106: # current enviroment
1.133 albertel 107: #
108: #
1.134 albertel 109: # for the next 6 functions udom and uname are optional
1.133 albertel 110: # if supplied they use udom as the domain and uname
111: # as the username for the function (supply a courseid
112: # for the uname if you want a course database)
113: # if not supplied it uses %ENV and looks at
114: # user. attribute for the values
115: #
116: # eget(namesp,arrayref,udom,uname)
117: # : returns hash with keys from array reference filled
118: # in from namesp (encrypts the return communication)
1.131 albertel 119: # get(namesp,arrayref,udom,uname)
120: # : returns hash with keys from array reference filled
121: # in from namesp
1.133 albertel 122: # dump(namesp,udom,uname) : dumps the complete namespace into a hash
123: # del(namesp,array,udom,uname) : deletes keys out of array from namesp
1.134 albertel 124: # put(namesp,hash,udom,uname) : stores hash in namesp
125: # cput(namesp,hash,udom,uname) : critical put
1.133 albertel 126: #
127: #
1.23 www 128: # ssi(url,hash) : does a complete request cycle on url to localhost, posts
129: # hash
1.34 www 130: # coursedescription(id) : returns and caches course description for id
1.17 www 131: # repcopy(filename) : replicate file
132: # dirlist(url) : gets a directory listing
1.40 www 133: # directcondval(index) : reading condition value of single condition from
134: # state string
1.28 www 135: # condval(index) : value of condition index based on state
1.58 www 136: # EXT(name) : value of a variable
1.31 www 137: # symblist(map,hash) : Updates symbolic storage links
1.44 www 138: # symbread([filename]) : returns the data handle (filename optional)
1.155 albertel 139: # rndseed([symb,courseid,domain,uname])
140: # : returns a random seed, all arguments are optional,
141: # if they aren't sent it use the environment to derive
142: # them
143: # Note: if symb isn't sent and it can't get one from
144: # &symbread it will use the current time as it's return
1.76 www 145: # receipt() : returns a receipt to be given out to users
1.36 albertel 146: # getfile(filename) : returns the contents of filename, or a -1 if it can't
147: # be found, replicates and subscribes to the file
1.121 harris41 148: # filelocation(dir,file) : returns a fairly clean absolute reference to file
1.36 albertel 149: # from the directory dir
1.46 www 150: # hreflocation(dir,file) : same as filelocation, but for hrefs
1.47 www 151: # log(domain,user,home,msg) : write to permanent log for user
1.70 www 152: # usection(domain,user,courseid) : output of section name/number or '' for
153: # "not in course" and '-1' for "no section"
154: # userenvironment(domain,user,what) : puts out any environment parameter
155: # for a user
156: # idput(domain,hash) : writes IDs for users from hash (name=>id,name=>id)
157: # idget(domain,array): returns hash with usernames (id=>name,id=>name) for
158: # an array of IDs
159: # idrget(domain,array): returns hash with IDs for usernames (name=>id,...) for
160: # an array of names
1.73 www 161: # metadata(file,entry): returns the metadata entry for a file. entry='keys'
162: # returns a comma separated list of keys
1.12 www 163: #
1.128 www 164:
1.1 albertel 165: package Apache::lonnet;
166:
167: use strict;
168: use Apache::File;
1.8 www 169: use LWP::UserAgent();
1.15 www 170: use HTTP::Headers;
1.11 www 171: use vars
1.157 www 172: qw(%perlvar %hostname %homecache %hostip %spareid %hostdom %libserv %pr %prp %fe %fd $readit %metacache %packagetab %courselogs);
1.1 albertel 173: use IO::Socket;
1.31 www 174: use GDBM_File;
1.8 www 175: use Apache::Constants qw(:common :http);
1.71 www 176: use HTML::TokeParser;
1.88 www 177: use Fcntl qw(:flock);
1.1 albertel 178:
179: # --------------------------------------------------------------------- Logging
180:
1.163 harris41 181: sub logtouch {
182: my $execdir=$perlvar{'lonDaemons'};
183: unless (-e "$execdir/logs/lonnet.log") {
184: my $fh=Apache::File->new(">>$execdir/logs/lonnet.log");
185: close $fh;
186: }
187: my ($wwwuid,$wwwgid)=(getpwnam('www'))[2,3];
188: chown($wwwuid,$wwwgid,$execdir.'/logs/lonnet.log');
189: }
190:
1.1 albertel 191: sub logthis {
192: my $message=shift;
193: my $execdir=$perlvar{'lonDaemons'};
194: my $now=time;
195: my $local=localtime($now);
1.162 harris41 196: my $fh=Apache::File->new(">>$execdir/logs/lonnet.log");
197: print $fh "$local ($$): $message\n";
1.1 albertel 198: return 1;
199: }
200:
201: sub logperm {
202: my $message=shift;
203: my $execdir=$perlvar{'lonDaemons'};
204: my $now=time;
205: my $local=localtime($now);
1.162 harris41 206: my $fh=Apache::File->new(">>$execdir/logs/lonnet.perm.log");
207: print $fh "$now:$message:$local\n";
1.1 albertel 208: return 1;
209: }
210:
211: # -------------------------------------------------- Non-critical communication
212: sub subreply {
213: my ($cmd,$server)=@_;
214: my $peerfile="$perlvar{'lonSockDir'}/$server";
215: my $client=IO::Socket::UNIX->new(Peer =>"$peerfile",
216: Type => SOCK_STREAM,
217: Timeout => 10)
218: or return "con_lost";
219: print $client "$cmd\n";
220: my $answer=<$client>;
1.9 www 221: if (!$answer) { $answer="con_lost"; }
1.1 albertel 222: chomp($answer);
223: return $answer;
224: }
225:
226: sub reply {
227: my ($cmd,$server)=@_;
228: my $answer=subreply($cmd,$server);
229: if ($answer eq 'con_lost') { $answer=subreply($cmd,$server); }
1.65 www 230: if (($answer=~/^refused/) || ($answer=~/^rejected/)) {
1.12 www 231: &logthis("<font color=blue>WARNING:".
232: " $cmd to $server returned $answer</font>");
233: }
1.1 albertel 234: return $answer;
235: }
236:
237: # ----------------------------------------------------------- Send USR1 to lonc
238:
239: sub reconlonc {
240: my $peerfile=shift;
241: &logthis("Trying to reconnect for $peerfile");
242: my $loncfile="$perlvar{'lonDaemons'}/logs/lonc.pid";
243: if (my $fh=Apache::File->new("$loncfile")) {
244: my $loncpid=<$fh>;
245: chomp($loncpid);
246: if (kill 0 => $loncpid) {
247: &logthis("lonc at pid $loncpid responding, sending USR1");
248: kill USR1 => $loncpid;
249: sleep 1;
250: if (-e "$peerfile") { return; }
251: &logthis("$peerfile still not there, give it another try");
252: sleep 5;
253: if (-e "$peerfile") { return; }
1.12 www 254: &logthis(
255: "<font color=blue>WARNING: $peerfile still not there, giving up</font>");
1.1 albertel 256: } else {
1.12 www 257: &logthis(
258: "<font color=blue>WARNING:".
259: " lonc at pid $loncpid not responding, giving up</font>");
1.1 albertel 260: }
261: } else {
1.12 www 262: &logthis('<font color=blue>WARNING: lonc not running, giving up</font>');
1.1 albertel 263: }
264: }
265:
266: # ------------------------------------------------------ Critical communication
1.12 www 267:
1.1 albertel 268: sub critical {
269: my ($cmd,$server)=@_;
1.89 www 270: unless ($hostname{$server}) {
271: &logthis("<font color=blue>WARNING:".
272: " Critical message to unknown server ($server)</font>");
273: return 'no_such_host';
274: }
1.1 albertel 275: my $answer=reply($cmd,$server);
276: if ($answer eq 'con_lost') {
277: my $pingreply=reply('ping',$server);
278: &reconlonc("$perlvar{'lonSockDir'}/$server");
279: my $pongreply=reply('pong',$server);
280: &logthis("Ping/Pong for $server: $pingreply/$pongreply");
281: $answer=reply($cmd,$server);
282: if ($answer eq 'con_lost') {
283: my $now=time;
284: my $middlename=$cmd;
1.5 www 285: $middlename=substr($middlename,0,16);
1.1 albertel 286: $middlename=~s/\W//g;
287: my $dfilename=
288: "$perlvar{'lonSockDir'}/delayed/$now.$middlename.$server";
289: {
290: my $dfh;
291: if ($dfh=Apache::File->new(">$dfilename")) {
1.7 www 292: print $dfh "$cmd\n";
1.1 albertel 293: }
294: }
295: sleep 2;
296: my $wcmd='';
297: {
298: my $dfh;
299: if ($dfh=Apache::File->new("$dfilename")) {
300: $wcmd=<$dfh>;
301: }
302: }
303: chomp($wcmd);
1.7 www 304: if ($wcmd eq $cmd) {
1.12 www 305: &logthis("<font color=blue>WARNING: ".
306: "Connection buffer $dfilename: $cmd</font>");
1.1 albertel 307: &logperm("D:$server:$cmd");
308: return 'con_delayed';
309: } else {
1.12 www 310: &logthis("<font color=red>CRITICAL:"
311: ." Critical connection failed: $server $cmd</font>");
1.1 albertel 312: &logperm("F:$server:$cmd");
313: return 'con_failed';
314: }
315: }
316: }
317: return $answer;
318: }
319:
1.5 www 320: # ---------------------------------------------------------- Append Environment
321:
322: sub appenv {
1.6 www 323: my %newenv=@_;
1.35 www 324: map {
325: if (($newenv{$_}=~/^user\.role/) || ($newenv{$_}=~/^user\.priv/)) {
326: &logthis("<font color=blue>WARNING: ".
1.151 www 327: "Attempt to modify environment ".$_." to ".$newenv{$_}
328: .'</font>');
1.35 www 329: delete($newenv{$_});
330: } else {
331: $ENV{$_}=$newenv{$_};
332: }
333: } keys %newenv;
1.95 www 334:
335: my $lockfh;
336: unless ($lockfh=Apache::File->new("$ENV{'user.environment'}")) {
1.97 www 337: return 'error: '.$!;
1.95 www 338: }
339: unless (flock($lockfh,LOCK_EX)) {
340: &logthis("<font color=blue>WARNING: ".
341: 'Could not obtain exclusive lock in appenv: '.$!);
342: $lockfh->close();
343: return 'error: '.$!;
344: }
345:
1.6 www 346: my @oldenv;
347: {
348: my $fh;
349: unless ($fh=Apache::File->new("$ENV{'user.environment'}")) {
1.97 www 350: return 'error: '.$!;
1.6 www 351: }
352: @oldenv=<$fh>;
1.89 www 353: $fh->close();
1.6 www 354: }
355: for (my $i=0; $i<=$#oldenv; $i++) {
356: chomp($oldenv[$i]);
1.9 www 357: if ($oldenv[$i] ne '') {
358: my ($name,$value)=split(/=/,$oldenv[$i]);
1.24 www 359: unless (defined($newenv{$name})) {
360: $newenv{$name}=$value;
361: }
1.9 www 362: }
1.6 www 363: }
364: {
365: my $fh;
366: unless ($fh=Apache::File->new(">$ENV{'user.environment'}")) {
367: return 'error';
368: }
369: my $newname;
1.93 www 370: foreach $newname (keys %newenv) {
1.6 www 371: print $fh "$newname=$newenv{$newname}\n";
372: }
1.86 albertel 373: $fh->close();
1.56 www 374: }
1.95 www 375:
376: $lockfh->close();
1.56 www 377: return 'ok';
378: }
379: # ----------------------------------------------------- Delete from Environment
380:
381: sub delenv {
382: my $delthis=shift;
383: my %newenv=();
384: if (($delthis=~/user\.role/) || ($delthis=~/user\.priv/)) {
385: &logthis("<font color=blue>WARNING: ".
386: "Attempt to delete from environment ".$delthis);
387: return 'error';
388: }
389: my @oldenv;
390: {
391: my $fh;
392: unless ($fh=Apache::File->new("$ENV{'user.environment'}")) {
393: return 'error';
394: }
1.89 www 395: unless (flock($fh,LOCK_SH)) {
396: &logthis("<font color=blue>WARNING: ".
397: 'Could not obtain shared lock in delenv: '.$!);
398: $fh->close();
399: return 'error: '.$!;
400: }
1.56 www 401: @oldenv=<$fh>;
1.89 www 402: $fh->close();
1.56 www 403: }
404: {
405: my $fh;
406: unless ($fh=Apache::File->new(">$ENV{'user.environment'}")) {
407: return 'error';
408: }
1.89 www 409: unless (flock($fh,LOCK_EX)) {
410: &logthis("<font color=blue>WARNING: ".
411: 'Could not obtain exclusive lock in delenv: '.$!);
412: $fh->close();
413: return 'error: '.$!;
414: }
1.56 www 415: map {
416: unless ($_=~/^$delthis/) { print $fh $_; }
417: } @oldenv;
1.87 www 418: $fh->close();
1.5 www 419: }
420: return 'ok';
421: }
1.1 albertel 422:
423: # ------------------------------ Find server with least workload from spare.tab
1.11 www 424:
1.1 albertel 425: sub spareserver {
426: my $tryserver;
427: my $spareserver='';
428: my $lowestserver=100;
429: foreach $tryserver (keys %spareid) {
430: my $answer=reply('load',$tryserver);
431: if (($answer =~ /\d/) && ($answer<$lowestserver)) {
432: $spareserver="http://$hostname{$tryserver}";
433: $lowestserver=$answer;
434: }
435: }
436: return $spareserver;
437: }
438:
1.169 harris41 439: # ----------------------- Try to determine user's current authentication scheme
440:
441: sub queryauthenticate {
442: my ($uname,$udom)=@_;
443: if (($perlvar{'lonRole'} eq 'library') &&
444: ($udom eq $perlvar{'lonDefDomain'})) {
445: my $answer=reply("encrypt:currentauth:$udom:$uname",
446: $perlvar{'lonHostID'});
447: unless ($answer eq 'unknown_user' or $answer eq 'refused') {
448: if (length($answer)) {
449: return $answer;
450: }
451: else {
452: &logthis("User $uname at $udom lacks an authentication mechanism");
453: return 'no_host';
454: }
455: }
456: }
457:
458: my $tryserver;
459: foreach $tryserver (keys %libserv) {
460: if ($hostdom{$tryserver} eq $udom) {
461: my $answer=reply("encrypt:currentauth:$udom:$uname",$tryserver);
462: unless ($answer eq 'unknown_user' or $answer eq 'refused') {
463: if (length($answer)) {
464: return $answer;
465: }
466: else {
467: &logthis("User $uname at $udom lacks an authentication mechanism");
468: return 'no_host';
469: }
470: }
471: }
472: }
473: &logthis("User $uname at $udom lacks an authentication mechanism");
474: return 'no_host';
475: }
476:
1.1 albertel 477: # --------- Try to authenticate user from domain's lib servers (first this one)
1.11 www 478:
1.1 albertel 479: sub authenticate {
480: my ($uname,$upass,$udom)=@_;
1.12 www 481: $upass=escape($upass);
1.1 albertel 482: if (($perlvar{'lonRole'} eq 'library') &&
483: ($udom eq $perlvar{'lonDefDomain'})) {
1.3 www 484: my $answer=reply("encrypt:auth:$udom:$uname:$upass",$perlvar{'lonHostID'});
1.2 www 485: if ($answer =~ /authorized/) {
1.9 www 486: if ($answer eq 'authorized') {
487: &logthis("User $uname at $udom authorized by local server");
488: return $perlvar{'lonHostID'};
489: }
490: if ($answer eq 'non_authorized') {
491: &logthis("User $uname at $udom rejected by local server");
492: return 'no_host';
493: }
1.2 www 494: }
1.1 albertel 495: }
496:
497: my $tryserver;
498: foreach $tryserver (keys %libserv) {
499: if ($hostdom{$tryserver} eq $udom) {
1.10 www 500: my $answer=reply("encrypt:auth:$udom:$uname:$upass",$tryserver);
1.1 albertel 501: if ($answer =~ /authorized/) {
1.9 www 502: if ($answer eq 'authorized') {
503: &logthis("User $uname at $udom authorized by $tryserver");
504: return $tryserver;
505: }
506: if ($answer eq 'non_authorized') {
507: &logthis("User $uname at $udom rejected by $tryserver");
508: return 'no_host';
509: }
1.1 albertel 510: }
511: }
1.9 www 512: }
513: &logthis("User $uname at $udom could not be authenticated");
1.1 albertel 514: return 'no_host';
515: }
516:
517: # ---------------------- Find the homebase for a user from domain's lib servers
1.11 www 518:
1.1 albertel 519: sub homeserver {
520: my ($uname,$udom)=@_;
521:
522: my $index="$uname:$udom";
523: if ($homecache{$index}) { return "$homecache{$index}"; }
524:
525: my $tryserver;
526: foreach $tryserver (keys %libserv) {
527: if ($hostdom{$tryserver} eq $udom) {
528: my $answer=reply("home:$udom:$uname",$tryserver);
529: if ($answer eq 'found') {
530: $homecache{$index}=$tryserver;
531: return $tryserver;
532: }
533: }
534: }
535: return 'no_host';
1.70 www 536: }
537:
538: # ------------------------------------- Find the usernames behind a list of IDs
539:
540: sub idget {
541: my ($udom,@ids)=@_;
542: my %returnhash=();
543:
544: my $tryserver;
545: foreach $tryserver (keys %libserv) {
546: if ($hostdom{$tryserver} eq $udom) {
547: my $idlist=join('&',@ids);
548: $idlist=~tr/A-Z/a-z/;
549: my $reply=&reply("idget:$udom:".$idlist,$tryserver);
550: my @answer=();
1.76 www 551: if (($reply ne 'con_lost') && ($reply!~/^error\:/)) {
1.70 www 552: @answer=split(/\&/,$reply);
553: } ;
554: my $i;
555: for ($i=0;$i<=$#ids;$i++) {
556: if ($answer[$i]) {
557: $returnhash{$ids[$i]}=$answer[$i];
558: }
559: }
560: }
561: }
562: return %returnhash;
563: }
564:
565: # ------------------------------------- Find the IDs behind a list of usernames
566:
567: sub idrget {
568: my ($udom,@unames)=@_;
569: my %returnhash=();
570: map {
571: $returnhash{$_}=(&userenvironment($udom,$_,'id'))[1];
572: } @unames;
573: return %returnhash;
574: }
575:
576: # ------------------------------- Store away a list of names and associated IDs
577:
578: sub idput {
579: my ($udom,%ids)=@_;
580: my %servers=();
581: map {
582: my $uhom=&homeserver($_,$udom);
583: if ($uhom ne 'no_host') {
584: my $id=&escape($ids{$_});
585: $id=~tr/A-Z/a-z/;
586: my $unam=&escape($_);
587: if ($servers{$uhom}) {
588: $servers{$uhom}.='&'.$id.'='.$unam;
589: } else {
590: $servers{$uhom}=$id.'='.$unam;
591: }
592: &critical('put:'.$udom.':'.$unam.':environment:id='.$id,$uhom);
593: }
594: } keys %ids;
595: map {
596: &critical('idput:'.$udom.':'.$servers{$_},$_);
597: } keys %servers;
598: }
599:
600: # ------------------------------------- Find the section of student in a course
601:
602: sub usection {
603: my ($udom,$unam,$courseid)=@_;
604: $courseid=~s/\_/\//g;
605: $courseid=~s/^(\w)/\/$1/;
606: map {
607: my ($key,$value)=split(/\=/,$_);
608: $key=&unescape($key);
609: if ($key=~/^$courseid(?:\/)*(\w+)*\_st$/) {
610: my $section=$1;
611: if ($key eq $courseid.'_st') { $section=''; }
612: my ($dummy,$end,$start)=split(/\_/,&unescape($value));
613: my $now=time;
614: my $notactive=0;
615: if ($start) {
616: if ($now<$start) { $notactive=1; }
617: }
618: if ($end) {
619: if ($now>$end) { $notactive=1; }
620: }
621: unless ($notactive) { return $section; }
622: }
623: } split(/\&/,&reply('dump:'.$udom.':'.$unam.':roles',
624: &homeserver($unam,$udom)));
625: return '-1';
626: }
627:
628: # ------------------------------------- Read an entry from a user's environment
629:
630: sub userenvironment {
631: my ($udom,$unam,@what)=@_;
632: my %returnhash=();
633: my @answer=split(/\&/,
634: &reply('get:'.$udom.':'.$unam.':environment:'.join('&',@what),
635: &homeserver($unam,$udom)));
636: my $i;
637: for ($i=0;$i<=$#what;$i++) {
638: $returnhash{$what[$i]}=&unescape($answer[$i]);
639: }
640: return %returnhash;
1.1 albertel 641: }
642:
643: # ----------------------------- Subscribe to a resource, return URL if possible
1.11 www 644:
1.1 albertel 645: sub subscribe {
646: my $fname=shift;
647: my $author=$fname;
648: $author=~s/\/home\/httpd\/html\/res\/([^\/]*)\/([^\/]*).*/$1\/$2/;
649: my ($udom,$uname)=split(/\//,$author);
650: my $home=homeserver($uname,$udom);
651: if (($home eq 'no_host') || ($home eq $perlvar{'lonHostID'})) {
652: return 'not_found';
653: }
654: my $answer=reply("sub:$fname",$home);
1.64 www 655: if (($answer eq 'con_lost') || ($answer eq 'rejected')) {
656: $answer.=' by '.$home;
657: }
1.1 albertel 658: return $answer;
659: }
660:
1.8 www 661: # -------------------------------------------------------------- Replicate file
662:
663: sub repcopy {
664: my $filename=shift;
1.23 www 665: $filename=~s/\/+/\//g;
1.8 www 666: my $transname="$filename.in.transfer";
1.17 www 667: if ((-e $filename) || (-e $transname)) { return OK; }
1.8 www 668: my $remoteurl=subscribe($filename);
1.64 www 669: if ($remoteurl =~ /^con_lost by/) {
670: &logthis("Subscribe returned $remoteurl: $filename");
1.8 www 671: return HTTP_SERVICE_UNAVAILABLE;
672: } elsif ($remoteurl eq 'not_found') {
673: &logthis("Subscribe returned not_found: $filename");
674: return HTTP_NOT_FOUND;
1.64 www 675: } elsif ($remoteurl =~ /^rejected by/) {
676: &logthis("Subscribe returned $remoteurl: $filename");
1.8 www 677: return FORBIDDEN;
1.20 www 678: } elsif ($remoteurl eq 'directory') {
679: return OK;
1.8 www 680: } else {
681: my @parts=split(/\//,$filename);
682: my $path="/$parts[1]/$parts[2]/$parts[3]/$parts[4]";
683: if ($path ne "$perlvar{'lonDocRoot'}/res") {
684: &logthis("Malconfiguration for replication: $filename");
685: return HTTP_BAD_REQUEST;
686: }
687: my $count;
688: for ($count=5;$count<$#parts;$count++) {
689: $path.="/$parts[$count]";
690: if ((-e $path)!=1) {
691: mkdir($path,0777);
692: }
693: }
694: my $ua=new LWP::UserAgent;
695: my $request=new HTTP::Request('GET',"$remoteurl");
696: my $response=$ua->request($request,$transname);
697: if ($response->is_error()) {
698: unlink($transname);
699: my $message=$response->status_line;
1.12 www 700: &logthis("<font color=blue>WARNING:"
701: ." LWP get: $message: $filename</font>");
1.8 www 702: return HTTP_SERVICE_UNAVAILABLE;
703: } else {
1.16 www 704: if ($remoteurl!~/\.meta$/) {
705: my $mrequest=new HTTP::Request('GET',$remoteurl.'.meta');
706: my $mresponse=$ua->request($mrequest,$filename.'.meta');
707: if ($mresponse->is_error()) {
708: unlink($filename.'.meta');
709: &logthis(
710: "<font color=yellow>INFO: No metadata: $filename</font>");
711: }
712: }
1.8 www 713: rename($transname,$filename);
714: return OK;
715: }
716: }
717: }
718:
1.15 www 719: # --------------------------------------------------------- Server Side Include
720:
721: sub ssi {
722:
1.23 www 723: my ($fn,%form)=@_;
1.15 www 724:
725: my $ua=new LWP::UserAgent;
1.23 www 726:
727: my $request;
728:
729: if (%form) {
730: $request=new HTTP::Request('POST',"http://".$ENV{'HTTP_HOST'}.$fn);
731: $request->content(join '&', map { "$_=$form{$_}" } keys %form);
732: } else {
733: $request=new HTTP::Request('GET',"http://".$ENV{'HTTP_HOST'}.$fn);
734: }
735:
1.15 www 736: $request->header(Cookie => $ENV{'HTTP_COOKIE'});
737: my $response=$ua->request($request);
738:
739: return $response->content;
740: }
741:
1.14 www 742: # ------------------------------------------------------------------------- Log
743:
744: sub log {
745: my ($dom,$nam,$hom,$what)=@_;
1.47 www 746: return critical("log:$dom:$nam:$what",$hom);
1.157 www 747: }
748:
749: # ------------------------------------------------------------------ Course Log
750:
751: sub flushcourselogs {
752: &logthis('Flushing course log buffers');
753: map {
754: my $crsid=$_;
755: if (&reply('log:'.$ENV{'course.'.$crsid.'.domain'}.':'.
756: $ENV{'course.'.$crsid.'.num'}.':'.
757: &escape($courselogs{$crsid}),
758: $ENV{'course.'.$crsid.'.home'}) eq 'ok') {
759: delete $courselogs{$crsid};
760: } else {
761: &logthis('Failed to flush log buffer for '.$crsid);
762: if (length($courselogs{$crsid})>40000) {
763: &logthis("<font color=blue>WARNING: Buffer for ".$crsid.
764: " exceeded maximum size, deleting.</font>");
765: delete $courselogs{$crsid};
766: }
767: }
768: } keys %courselogs;
769: }
770:
771: sub courselog {
772: my $what=shift;
1.158 www 773: $what=time.':'.$what;
1.157 www 774: unless ($ENV{'request.course.id'}) { return ''; }
775: if (defined $courselogs{$ENV{'request.course.id'}}) {
776: $courselogs{$ENV{'request.course.id'}}.='&'.$what;
777: } else {
778: $courselogs{$ENV{'request.course.id'}}.=$what;
779: }
780: if (length($courselogs{$ENV{'request.course.id'}})>4048) {
781: &flushcourselogs();
782: }
1.158 www 783: }
784:
785: sub courseacclog {
786: my $fnsymb=shift;
787: unless ($ENV{'request.course.id'}) { return ''; }
788: my $what=$fnsymb.':'.$ENV{'user.name'}.':'.$ENV{'user.domain'};
789: if ($what=~/(problem|exam|quiz|assess|survey|form)$/) {
790: map {
791: if ($_=~/^form\.(.*)/) {
792: $what.=':'.$1.'='.$ENV{$_};
793: }
794: } keys %ENV;
795: }
796: &courselog($what);
1.149 www 797: }
798:
799: # ----------------------------------------------------------- Check out an item
800:
801: sub checkout {
802: my ($symb,$tuname,$tudom,$tcrsid)=@_;
803: my $now=time;
804: my $lonhost=$perlvar{'lonHostID'};
805: my $infostr=&escape(
806: $tuname.'&'.
807: $tudom.'&'.
808: $tcrsid.'&'.
809: $symb.'&'.
810: $now.'&'.$ENV{'REMOTE_ADDR'});
811: my $token=&reply('tmpput:'.$infostr,$lonhost);
1.151 www 812: if ($token=~/^error\:/) {
813: &logthis("<font color=blue>WARNING: ".
814: "Checkout tmpput failed ".$tudom.' - '.$tuname.' - '.$symb.
815: "</font>");
816: return '';
817: }
818:
1.149 www 819: $token=~s/^(\d+)\_.*\_(\d+)$/$1\*$2\*$lonhost/;
820: $token=~tr/a-z/A-Z/;
821:
1.153 www 822: my %infohash=('resource.0.outtoken' => $token,
823: 'resource.0.checkouttime' => $now,
824: 'resource.0.outremote' => $ENV{'REMOTE_ADDR'});
1.149 www 825:
826: unless (&cstore(\%infohash,$symb,$tcrsid,$tudom,$tuname) eq 'ok') {
827: return '';
1.151 www 828: } else {
829: &logthis("<font color=blue>WARNING: ".
830: "Checkout cstore failed ".$tudom.' - '.$tuname.' - '.$symb.
831: "</font>");
1.149 www 832: }
833:
834: if (&log($tudom,$tuname,&homeserver($tuname,$tudom),
835: &escape('Checkout '.$infostr.' - '.
836: $token)) ne 'ok') {
837: return '';
1.151 www 838: } else {
839: &logthis("<font color=blue>WARNING: ".
840: "Checkout log failed ".$tudom.' - '.$tuname.' - '.$symb.
841: "</font>");
1.149 www 842: }
1.151 www 843: return $token;
1.149 www 844: }
845:
846: # ------------------------------------------------------------ Check in an item
847:
848: sub checkin {
849: my $token=shift;
1.150 www 850: my $now=time;
851: my ($ta,$tb,$lonhost)=split(/\*/,$token);
852: $lonhost=~tr/A-Z/a-z/;
853: my $dtoken=$ta.'_'.$hostip{$lonhost}.'_'.$tb;
854: $dtoken=~s/\W/\_/g;
855: my ($tuname,$tudom,$tcrsid,$symb,$chtim,$rmaddr)=
856: split(/\&/,&unescape(&reply('tmpget:'.$dtoken,$lonhost)));
857:
1.154 www 858: unless (($tuname) && ($tudom)) {
859: &logthis('Check in '.$token.' ('.$dtoken.') failed');
860: return '';
861: }
862:
863: unless (&allowed('mgr',$tcrsid)) {
864: &logthis('Check in '.$token.' ('.$dtoken.') unauthorized: '.
865: $ENV{'user.name'}.' - '.$ENV{'user.domain'});
866: return '';
867: }
868:
1.153 www 869: my %infohash=('resource.0.intoken' => $token,
870: 'resource.0.checkintime' => $now,
871: 'resource.0.inremote' => $ENV{'REMOTE_ADDR'});
1.150 www 872:
873: unless (&cstore(\%infohash,$symb,$tcrsid,$tudom,$tuname) eq 'ok') {
874: return '';
875: }
876:
877: if (&log($tudom,$tuname,&homeserver($tuname,$tudom),
878: &escape('Checkin - '.$token)) ne 'ok') {
879: return '';
880: }
881:
882: return ($symb,$tuname,$tudom,$tcrsid);
1.110 www 883: }
884:
885: # --------------------------------------------- Set Expire Date for Spreadsheet
886:
887: sub expirespread {
888: my ($uname,$udom,$stype,$usymb)=@_;
889: my $cid=$ENV{'request.course.id'};
890: if ($cid) {
891: my $now=time;
892: my $key=$uname.':'.$udom.':'.$stype.':'.$usymb;
893: return &reply('put:'.$ENV{'course.'.$cid.'.domain'}.':'.
894: $ENV{'course.'.$cid.'.num'}.
895: ':nohist_expirationdates:'.
896: &escape($key).'='.$now,
897: $ENV{'course.'.$cid.'.home'})
898: }
899: return 'ok';
1.14 www 900: }
901:
1.109 www 902: # ----------------------------------------------------- Devalidate Spreadsheets
903:
904: sub devalidate {
905: my $symb=shift;
906: my $cid=$ENV{'request.course.id'};
907: if ($cid) {
908: my $key=$ENV{'user.name'}.':'.$ENV{'user.domain'}.':';
909: my $status=
1.133 albertel 910: &del('nohist_calculatedsheet',
911: [$key.'studentcalc'],
912: $ENV{'course.'.$cid.'.domain'},
913: $ENV{'course.'.$cid.'.num'})
914: .' '.
915: &del('nohist_calculatedsheets_'.$cid,
916: [$key.'assesscalc:'.$symb]);
1.109 www 917: unless ($status eq 'ok ok') {
918: &logthis('Could not devalidate spreadsheet '.
919: $ENV{'user.name'}.' at '.$ENV{'user.domain'}.' for '.
920: $symb.': '.$status);
1.133 albertel 921: }
1.109 www 922: }
923: }
924:
1.168 albertel 925: sub hash2str {
926: my (%hash)=@_;
927: my $result='';
928: map { $result.=escape($_).'='.escape($hash{$_}).'&'; } keys %hash;
929: $result=~s/\&$//;
930: return $result;
931: }
932:
933: sub str2hash {
934: my ($string) = @_;
935: my %returnhash;
936: map {
937: my ($name,$value)=split(/\=/,$_);
938: $returnhash{&unescape($name)}=&unescape($value);
939: } split(/\&/,$string);
940: return %returnhash;
941: }
942:
1.167 albertel 943: # -------------------------------------------------------------------Temp Store
944:
1.168 albertel 945: sub tmpreset {
946: my ($symb,$namespace,$domain,$stuname) = @_;
947: if (!$symb) {
948: $symb=&symbread();
949: if (!$symb) { $symb= $ENV{'REQUEST_URI'}; }
950: }
951: $symb=escape($symb);
952:
953: if (!$namespace) { $namespace=$ENV{'request.state'}; }
954: $namespace=~s/\//\_/g;
955: $namespace=~s/\W//g;
956:
957: #FIXME needs to do something for /pub resources
958: if (!$domain) { $domain=$ENV{'user.domain'}; }
959: if (!$stuname) { $stuname=$ENV{'user.name'}; }
960: my $path=$perlvar{'lonDaemons'}.'/tmp';
961: my %hash;
962: if (tie(%hash,'GDBM_File',
963: $path.'/tmpstore_'.$stuname.'_'.$domain.'_'.$namespace.'.db',
964: &GDBM_WRCREAT,0640)) {
965: foreach my $key (keys %hash) {
1.180 ! albertel 966: if ($key=~ /:$symb/) {
1.168 albertel 967: delete($hash{$key});
968: }
969: }
970: }
971: }
972:
1.167 albertel 973: sub tmpstore {
1.168 albertel 974: my ($storehash,$symb,$namespace,$domain,$stuname) = @_;
975:
976: if (!$symb) {
977: $symb=&symbread();
978: if (!$symb) { $symb= $ENV{'request.url'}; }
979: }
980: $symb=escape($symb);
981:
982: if (!$namespace) {
983: # I don't think we would ever want to store this for a course.
984: # it seems this will only be used if we don't have a course.
985: #$namespace=$ENV{'request.course.id'};
986: #if (!$namespace) {
987: $namespace=$ENV{'request.state'};
988: #}
989: }
990: $namespace=~s/\//\_/g;
991: $namespace=~s/\W//g;
992: #FIXME needs to do something for /pub resources
993: if (!$domain) { $domain=$ENV{'user.domain'}; }
994: if (!$stuname) { $stuname=$ENV{'user.name'}; }
995: my $now=time;
996: my %hash;
997: my $path=$perlvar{'lonDaemons'}.'/tmp';
998: if (tie(%hash,'GDBM_File',
999: $path.'/tmpstore_'.$stuname.'_'.$domain.'_'.$namespace.'.db',
1000: &GDBM_WRCREAT,0640)) {
1001: $hash{"version:$symb"}++;
1002: my $version=$hash{"version:$symb"};
1003: my $allkeys='';
1004: foreach my $key (keys(%$storehash)) {
1005: $allkeys.=$key.':';
1006: $hash{"$version:$symb:$key"}=$$storehash{$key};
1007: }
1008: $hash{"$version:$symb:timestamp"}=$now;
1009: $allkeys.='timestamp';
1010: $hash{"$version:keys:$symb"}=$allkeys;
1011: if (untie(%hash)) {
1012: return 'ok';
1013: } else {
1014: return "error:$!";
1015: }
1016: } else {
1017: return "error:$!";
1018: }
1019: }
1.167 albertel 1020:
1.168 albertel 1021: # -----------------------------------------------------------------Temp Restore
1.167 albertel 1022:
1.168 albertel 1023: sub tmprestore {
1024: my ($symb,$namespace,$domain,$stuname) = @_;
1.167 albertel 1025:
1.168 albertel 1026: if (!$symb) {
1027: $symb=&symbread();
1028: if (!$symb) { $symb= $ENV{'request.url'}; }
1029: }
1030: $symb=escape($symb);
1031:
1032: if (!$namespace) { $namespace=$ENV{'request.state'}; }
1033: #FIXME needs to do something for /pub resources
1034: if (!$domain) { $domain=$ENV{'user.domain'}; }
1035: if (!$stuname) { $stuname=$ENV{'user.name'}; }
1036:
1037: my %returnhash;
1038: $namespace=~s/\//\_/g;
1039: $namespace=~s/\W//g;
1040: my %hash;
1041: my $path=$perlvar{'lonDaemons'}.'/tmp';
1042: if (tie(%hash,'GDBM_File',
1043: $path.'/tmpstore_'.$stuname.'_'.$domain.'_'.$namespace.'.db',
1044: &GDBM_READER,0640)) {
1045: my $version=$hash{"version:$symb"};
1046: $returnhash{'version'}=$version;
1047: my $scope;
1048: for ($scope=1;$scope<=$version;$scope++) {
1049: my $vkeys=$hash{"$scope:keys:$symb"};
1050: my @keys=split(/:/,$vkeys);
1051: my $key;
1052: $returnhash{"$scope:keys"}=$vkeys;
1053: foreach $key (@keys) {
1054: $returnhash{"$scope:$key"}=$hash{"$scope:$symb:$key"};
1055: $returnhash{"$key"}=$hash{"$scope:$symb:$key"};
1.167 albertel 1056: }
1057: }
1.168 albertel 1058: if (!(untie(%hash))) {
1059: return "error:$!";
1060: }
1061: } else {
1062: return "error:$!";
1063: }
1064: return %returnhash;
1.167 albertel 1065: }
1066:
1.9 www 1067: # ----------------------------------------------------------------------- Store
1068:
1069: sub store {
1.124 www 1070: my ($storehash,$symb,$namespace,$domain,$stuname) = @_;
1071: my $home='';
1072:
1.168 albertel 1073: if ($stuname) { $home=&homeserver($stuname,$domain); }
1.124 www 1074:
1.122 albertel 1075: if (!$symb) { unless ($symb=&symbread()) { return ''; } }
1.109 www 1076:
1077: &devalidate($symb);
1078:
1079: $symb=escape($symb);
1.122 albertel 1080: if (!$namespace) { unless ($namespace=$ENV{'request.course.id'}) { return ''; } }
1081: if (!$domain) { $domain=$ENV{'user.domain'}; }
1082: if (!$stuname) { $stuname=$ENV{'user.name'}; }
1083: if (!$home) { $home=$ENV{'user.home'}; }
1.12 www 1084: my $namevalue='';
1085: map {
1.122 albertel 1086: $namevalue.=escape($_).'='.escape($$storehash{$_}).'&';
1087: } keys %$storehash;
1.12 www 1088: $namevalue=~s/\&$//;
1.124 www 1089: return reply("store:$domain:$stuname:$namespace:$symb:$namevalue","$home");
1.9 www 1090: }
1091:
1.47 www 1092: # -------------------------------------------------------------- Critical Store
1093:
1094: sub cstore {
1.124 www 1095: my ($storehash,$symb,$namespace,$domain,$stuname) = @_;
1096: my $home='';
1097:
1.168 albertel 1098: if ($stuname) { $home=&homeserver($stuname,$domain); }
1.124 www 1099:
1.122 albertel 1100: if (!$symb) { unless ($symb=&symbread()) { return ''; } }
1.109 www 1101:
1102: &devalidate($symb);
1103:
1104: $symb=escape($symb);
1.122 albertel 1105: if (!$namespace) { unless ($namespace=$ENV{'request.course.id'}) { return ''; } }
1106: if (!$domain) { $domain=$ENV{'user.domain'}; }
1107: if (!$stuname) { $stuname=$ENV{'user.name'}; }
1108: if (!$home) { $home=$ENV{'user.home'}; }
1109:
1.47 www 1110: my $namevalue='';
1111: map {
1.122 albertel 1112: $namevalue.=escape($_).'='.escape($$storehash{$_}).'&';
1113: } keys %$storehash;
1.47 www 1114: $namevalue=~s/\&$//;
1.122 albertel 1115: return critical("store:$domain:$stuname:$namespace:$symb:$namevalue","$home");
1.47 www 1116: }
1117:
1.9 www 1118: # --------------------------------------------------------------------- Restore
1119:
1120: sub restore {
1.124 www 1121: my ($symb,$namespace,$domain,$stuname) = @_;
1122: my $home='';
1123:
1.168 albertel 1124: if ($stuname) { $home=&homeserver($stuname,$domain); }
1.124 www 1125:
1.122 albertel 1126: if (!$symb) {
1127: unless ($symb=escape(&symbread())) { return ''; }
1128: } else {
1129: $symb=&escape($symb);
1130: }
1131: if (!$namespace) { unless ($namespace=$ENV{'request.course.id'}) { return ''; } }
1132: if (!$domain) { $domain=$ENV{'user.domain'}; }
1133: if (!$stuname) { $stuname=$ENV{'user.name'}; }
1134: if (!$home) { $home=$ENV{'user.home'}; }
1135: my $answer=&reply("restore:$domain:$stuname:$namespace:$symb","$home");
1136:
1.12 www 1137: my %returnhash=();
1138: map {
1139: my ($name,$value)=split(/\=/,$_);
1140: $returnhash{&unescape($name)}=&unescape($value);
1141: } split(/\&/,$answer);
1.75 www 1142: my $version;
1143: for ($version=1;$version<=$returnhash{'version'};$version++) {
1144: map {
1145: $returnhash{$_}=$returnhash{$version.':'.$_};
1146: } split(/\:/,$returnhash{$version.':keys'});
1147: }
1.13 www 1148: return %returnhash;
1.34 www 1149: }
1150:
1151: # ---------------------------------------------------------- Course Description
1152:
1153: sub coursedescription {
1154: my $courseid=shift;
1155: $courseid=~s/^\///;
1.49 www 1156: $courseid=~s/\_/\//g;
1.34 www 1157: my ($cdomain,$cnum)=split(/\//,$courseid);
1.129 albertel 1158: my $chome=&homeserver($cnum,$cdomain);
1.34 www 1159: if ($chome ne 'no_host') {
1.129 albertel 1160: my %returnhash=&dump('environment',$cdomain,$cnum);
1161: if (!exists($returnhash{'con_lost'})) {
1.96 www 1162: my $normalid=$cdomain.'_'.$cnum;
1.53 www 1163: my %envhash=();
1.129 albertel 1164: $returnhash{'home'}= $chome;
1165: $returnhash{'domain'} = $cdomain;
1166: $returnhash{'num'} = $cnum;
1.130 albertel 1167: while (my ($name,$value) = each %returnhash) {
1.53 www 1168: $envhash{'course.'.$normalid.'.'.$name}=$value;
1.129 albertel 1169: }
1.34 www 1170: $returnhash{'url'}='/res/'.declutter($returnhash{'url'});
1171: $returnhash{'fn'}=$perlvar{'lonDaemons'}.'/tmp/'.
1.38 www 1172: $ENV{'user.name'}.'_'.$cdomain.'_'.$cnum;
1.54 www 1173: $envhash{'course.'.$normalid.'.last_cache'}=time;
1.60 www 1174: $envhash{'course.'.$normalid.'.home'}=$chome;
1175: $envhash{'course.'.$normalid.'.domain'}=$cdomain;
1176: $envhash{'course.'.$normalid.'.num'}=$cnum;
1.53 www 1177: &appenv(%envhash);
1.34 www 1178: return %returnhash;
1179: }
1180: }
1181: return ();
1.9 www 1182: }
1.1 albertel 1183:
1.103 harris41 1184: # -------------------------------------------------------- Get user privileges
1.11 www 1185:
1186: sub rolesinit {
1187: my ($domain,$username,$authhost)=@_;
1188: my $rolesdump=reply("dump:$domain:$username:roles",$authhost);
1.12 www 1189: if (($rolesdump eq 'con_lost') || ($rolesdump eq '')) { return ''; }
1.11 www 1190: my %allroles=();
1191: my %thesepriv=();
1192: my $now=time;
1.21 www 1193: my $userroles="user.login.time=$now\n";
1.11 www 1194: my $thesestr;
1195:
1196: if ($rolesdump ne '') {
1197: map {
1.21 www 1198: if ($_!~/^rolesdef\&/) {
1.11 www 1199: my ($area,$role)=split(/=/,$_);
1.21 www 1200: $area=~s/\_\w\w$//;
1.11 www 1201: my ($trole,$tend,$tstart)=split(/_/,$role);
1.21 www 1202: $userroles.='user.role.'.$trole.'.'.$area.'='.
1203: $tstart.'.'.$tend."\n";
1.11 www 1204: if ($tend!=0) {
1205: if ($tend<$now) {
1206: $trole='';
1207: }
1208: }
1209: if ($tstart!=0) {
1210: if ($tstart>$now) {
1211: $trole='';
1212: }
1213: }
1214: if (($area ne '') && ($trole ne '')) {
1.50 www 1215: my $spec=$trole.'.'.$area;
1.12 www 1216: my ($tdummy,$tdomain,$trest)=split(/\//,$area);
1217: if ($trole =~ /^cr\//) {
1218: my ($rdummy,$rdomain,$rauthor,$rrole)=split(/\//,$trole);
1219: my $homsvr=homeserver($rauthor,$rdomain);
1220: if ($hostname{$homsvr} ne '') {
1221: my $roledef=
1.21 www 1222: reply("get:$rdomain:$rauthor:roles:rolesdef_$rrole",
1.12 www 1223: $homsvr);
1224: if (($roledef ne 'con_lost') && ($roledef ne '')) {
1225: my ($syspriv,$dompriv,$coursepriv)=
1.21 www 1226: split(/\_/,unescape($roledef));
1.50 www 1227: $allroles{'cm./'}.=':'.$syspriv;
1228: $allroles{$spec.'./'}.=':'.$syspriv;
1.12 www 1229: if ($tdomain ne '') {
1.50 www 1230: $allroles{'cm./'.$tdomain.'/'}.=':'.$dompriv;
1231: $allroles{$spec.'./'.$tdomain.'/'}.=':'.$dompriv;
1.12 www 1232: if ($trest ne '') {
1.50 www 1233: $allroles{'cm.'.$area}.=':'.$coursepriv;
1234: $allroles{$spec.'.'.$area}.=':'.$coursepriv;
1.12 www 1235: }
1236: }
1237: }
1.11 www 1238: }
1.12 www 1239: } else {
1.50 www 1240: $allroles{'cm./'}.=':'.$pr{$trole.':s'};
1241: $allroles{$spec.'./'}.=':'.$pr{$trole.':s'};
1.12 www 1242: if ($tdomain ne '') {
1.50 www 1243: $allroles{'cm./'.$tdomain.'/'}.=':'.$pr{$trole.':d'};
1244: $allroles{$spec.'./'.$tdomain.'/'}.=':'.$pr{$trole.':d'};
1.12 www 1245: if ($trest ne '') {
1.50 www 1246: $allroles{'cm.'.$area}.=':'.$pr{$trole.':c'};
1247: $allroles{$spec.'.'.$area}.=':'.$pr{$trole.':c'};
1.12 www 1248: }
1249: }
1.11 www 1250: }
1.12 www 1251: }
1252: }
1.11 www 1253: } split(/&/,$rolesdump);
1.125 www 1254: my $adv=0;
1.128 www 1255: my $author=0;
1.11 www 1256: map {
1257: %thesepriv=();
1.146 www 1258: if (($_!~/^st/) && ($_!~/^ta/) && ($_!~/^cm/)) { $adv=1; }
1.128 www 1259: if (($_=~/^au/) || ($_=~/^ca/)) { $author=1; }
1.11 www 1260: map {
1261: if ($_ ne '') {
1.103 harris41 1262: my ($privilege,$restrictions)=split(/&/,$_);
1.11 www 1263: if ($restrictions eq '') {
1.103 harris41 1264: $thesepriv{$privilege}='F';
1.11 www 1265: } else {
1.103 harris41 1266: if ($thesepriv{$privilege} ne 'F') {
1267: $thesepriv{$privilege}.=$restrictions;
1.11 www 1268: }
1269: }
1270: }
1271: } split(/:/,$allroles{$_});
1272: $thesestr='';
1273: map { $thesestr.=':'.$_.'&'.$thesepriv{$_}; } keys %thesepriv;
1274: $userroles.='user.priv.'.$_.'='.$thesestr."\n";
1275: } keys %allroles;
1.128 www 1276: $userroles.='user.adv='.$adv."\n".
1277: 'user.author='.$author."\n";
1.126 www 1278: $ENV{'user.adv'}=$adv;
1.11 www 1279: }
1280: return $userroles;
1281: }
1282:
1.12 www 1283: # --------------------------------------------------------------- get interface
1284:
1285: sub get {
1.131 albertel 1286: my ($namespace,$storearr,$udomain,$uname)=@_;
1.12 www 1287: my $items='';
1288: map {
1289: $items.=escape($_).'&';
1.131 albertel 1290: } @$storearr;
1.12 www 1291: $items=~s/\&$//;
1.131 albertel 1292: if (!$udomain) { $udomain=$ENV{'user.domain'}; }
1293: if (!$uname) { $uname=$ENV{'user.name'}; }
1294: my $uhome=&homeserver($uname,$udomain);
1295:
1.133 albertel 1296: my $rep=&reply("get:$udomain:$uname:$namespace:$items",$uhome);
1.15 www 1297: my @pairs=split(/\&/,$rep);
1298: my %returnhash=();
1.42 www 1299: my $i=0;
1.15 www 1300: map {
1.42 www 1301: $returnhash{$_}=unescape($pairs[$i]);
1302: $i++;
1.131 albertel 1303: } @$storearr;
1.15 www 1304: return %returnhash;
1.27 www 1305: }
1306:
1307: # --------------------------------------------------------------- del interface
1308:
1309: sub del {
1.133 albertel 1310: my ($namespace,$storearr,$udomain,$uname)=@_;
1.27 www 1311: my $items='';
1312: map {
1313: $items.=escape($_).'&';
1.133 albertel 1314: } @$storearr;
1.27 www 1315: $items=~s/\&$//;
1.133 albertel 1316: if (!$udomain) { $udomain=$ENV{'user.domain'}; }
1317: if (!$uname) { $uname=$ENV{'user.name'}; }
1318: my $uhome=&homeserver($uname,$udomain);
1319:
1320: return &reply("del:$udomain:$uname:$namespace:$items",$uhome);
1.15 www 1321: }
1322:
1323: # -------------------------------------------------------------- dump interface
1324:
1325: sub dump {
1.129 albertel 1326: my ($namespace,$udomain,$uname)=@_;
1327: if (!$udomain) { $udomain=$ENV{'user.domain'}; }
1328: if (!$uname) { $uname=$ENV{'user.name'}; }
1329: my $uhome=&homeserver($uname,$udomain);
1330: my $rep=reply("dump:$udomain:$uname:$namespace",$uhome);
1.12 www 1331: my @pairs=split(/\&/,$rep);
1332: my %returnhash=();
1333: map {
1334: my ($key,$value)=split(/=/,$_);
1.29 www 1335: $returnhash{unescape($key)}=unescape($value);
1.12 www 1336: } @pairs;
1337: return %returnhash;
1338: }
1339:
1340: # --------------------------------------------------------------- put interface
1341:
1342: sub put {
1.134 albertel 1343: my ($namespace,$storehash,$udomain,$uname)=@_;
1344: if (!$udomain) { $udomain=$ENV{'user.domain'}; }
1345: if (!$uname) { $uname=$ENV{'user.name'}; }
1346: my $uhome=&homeserver($uname,$udomain);
1.12 www 1347: my $items='';
1348: map {
1.134 albertel 1349: $items.=&escape($_).'='.&escape($$storehash{$_}).'&';
1350: } keys %$storehash;
1.12 www 1351: $items=~s/\&$//;
1.134 albertel 1352: return &reply("put:$udomain:$uname:$namespace:$items",$uhome);
1.47 www 1353: }
1354:
1355: # ------------------------------------------------------ critical put interface
1356:
1357: sub cput {
1.134 albertel 1358: my ($namespace,$storehash,$udomain,$uname)=@_;
1359: if (!$udomain) { $udomain=$ENV{'user.domain'}; }
1360: if (!$uname) { $uname=$ENV{'user.name'}; }
1361: my $uhome=&homeserver($uname,$udomain);
1.47 www 1362: my $items='';
1363: map {
1.134 albertel 1364: $items.=escape($_).'='.escape($$storehash{$_}).'&';
1365: } keys %$storehash;
1.47 www 1366: $items=~s/\&$//;
1.134 albertel 1367: return &critical("put:$udomain:$uname:$namespace:$items",$uhome);
1.12 www 1368: }
1369:
1370: # -------------------------------------------------------------- eget interface
1371:
1372: sub eget {
1.133 albertel 1373: my ($namespace,$storearr,$udomain,$uname)=@_;
1.12 www 1374: my $items='';
1375: map {
1376: $items.=escape($_).'&';
1.133 albertel 1377: } @$storearr;
1.12 www 1378: $items=~s/\&$//;
1.133 albertel 1379: if (!$udomain) { $udomain=$ENV{'user.domain'}; }
1380: if (!$uname) { $uname=$ENV{'user.name'}; }
1381: my $uhome=&homeserver($uname,$udomain);
1382: my $rep=&reply("eget:$udomain:$uname:$namespace:$items",$uhome);
1.12 www 1383: my @pairs=split(/\&/,$rep);
1384: my %returnhash=();
1.42 www 1385: my $i=0;
1.12 www 1386: map {
1.42 www 1387: $returnhash{$_}=unescape($pairs[$i]);
1388: $i++;
1.133 albertel 1389: } @$storearr;
1.12 www 1390: return %returnhash;
1391: }
1392:
1.103 harris41 1393: # ------------------------------------------------- Check for a user privilege
1.12 www 1394:
1395: sub allowed {
1396: my ($priv,$uri)=@_;
1.152 www 1397:
1398: my $orguri=$uri;
1.52 www 1399: $uri=&declutter($uri);
1.29 www 1400:
1.54 www 1401: # Free bre access to adm and meta resources
1.29 www 1402:
1.54 www 1403: if ((($uri=~/^adm\//) || ($uri=~/\.meta$/)) && ($priv eq 'bre')) {
1.14 www 1404: return 'F';
1.159 www 1405: }
1406:
1407: # Free bre to public access
1408:
1409: if ($priv eq 'bre') {
1410: if (&metadata($uri,'copyright') eq 'public') { return 'F'; }
1.14 www 1411: }
1.29 www 1412:
1.52 www 1413: my $thisallowed='';
1414: my $statecond=0;
1415: my $courseprivid='';
1416:
1417: # Course
1418:
1419: if ($ENV{'user.priv.'.$ENV{'request.role'}.'./'}=~/$priv\&([^\:]*)/) {
1420: $thisallowed.=$1;
1421: }
1.29 www 1422:
1.52 www 1423: # Domain
1424:
1425: if ($ENV{'user.priv.'.$ENV{'request.role'}.'./'.(split(/\//,$uri))[0].'/'}
1426: =~/$priv\&([^\:]*)/) {
1.12 www 1427: $thisallowed.=$1;
1428: }
1.52 www 1429:
1430: # Course: uri itself is a course
1.66 www 1431: my $courseuri=$uri;
1432: $courseuri=~s/\_(\d)/\/$1/;
1.83 www 1433: $courseuri=~s/^([^\/])/\/$1/;
1.81 www 1434:
1.83 www 1435: if ($ENV{'user.priv.'.$ENV{'request.role'}.'.'.$courseuri}
1.52 www 1436: =~/$priv\&([^\:]*)/) {
1.12 www 1437: $thisallowed.=$1;
1438: }
1.29 www 1439:
1.52 www 1440: # Full access at system, domain or course-wide level? Exit.
1.29 www 1441:
1442: if ($thisallowed=~/F/) {
1443: return 'F';
1444: }
1445:
1.52 www 1446: # If this is generating or modifying users, exit with special codes
1.29 www 1447:
1.166 www 1448: if (':csu:cdc:ccc:cin:cta:cep:ccr:cst:cad:cli:cau:cdg:cca:'=~/\:$priv\:/) {
1.52 www 1449: return $thisallowed;
1450: }
1451: #
1.103 harris41 1452: # Gathered so far: system, domain and course wide privileges
1.52 www 1453: #
1454: # Course: See if uri or referer is an individual resource that is part of
1455: # the course
1456:
1457: if ($ENV{'request.course.id'}) {
1458: $courseprivid=$ENV{'request.course.id'};
1459: if ($ENV{'request.course.sec'}) {
1460: $courseprivid.='/'.$ENV{'request.course.sec'};
1461: }
1462: $courseprivid=~s/\_/\//;
1463: my $checkreferer=1;
1464: my @uriparts=split(/\//,$uri);
1465: my $filename=$uriparts[$#uriparts];
1466: my $pathname=$uri;
1467: $pathname=~s/\/$filename$//;
1468: if ($ENV{'acc.res.'.$ENV{'request.course.id'}.'.'.$pathname}=~
1.54 www 1469: /\&$filename\:([\d\|]+)\&/) {
1.52 www 1470: $statecond=$1;
1471: if ($ENV{'user.priv.'.$ENV{'request.role'}.'./'.$courseprivid}
1472: =~/$priv\&([^\:]*)/) {
1473: $thisallowed.=$1;
1474: $checkreferer=0;
1475: }
1.29 www 1476: }
1.83 www 1477:
1.148 www 1478: if ($checkreferer) {
1.152 www 1479: my $refuri=$ENV{'httpref.'.$orguri};
1.148 www 1480:
1481: unless ($refuri) {
1482: map {
1483: if ($_=~/^httpref\..*\*/) {
1484: my $pattern=$_;
1.156 www 1485: $pattern=~s/^httpref\.\/res\///;
1.148 www 1486: $pattern=~s/\*/\[\^\/\]\+/g;
1487: $pattern=~s/\//\\\//g;
1.152 www 1488: if ($orguri=~/$pattern/) {
1.148 www 1489: $refuri=$ENV{$_};
1490: }
1491: }
1492: } keys %ENV;
1493: }
1494: if ($refuri) {
1.152 www 1495: $refuri=&declutter($refuri);
1.53 www 1496: my @uriparts=split(/\//,$refuri);
1.52 www 1497: my $filename=$uriparts[$#uriparts];
1.53 www 1498: my $pathname=$refuri;
1.52 www 1499: $pathname=~s/\/$filename$//;
1.53 www 1500: if ($ENV{'acc.res.'.$ENV{'request.course.id'}.'.'.$pathname}=~
1.54 www 1501: /\&$filename\:([\d\|]+)\&/) {
1.53 www 1502: my $refstatecond=$1;
1.52 www 1503: if ($ENV{'user.priv.'.$ENV{'request.role'}.'./'.$courseprivid}
1504: =~/$priv\&([^\:]*)/) {
1505: $thisallowed.=$1;
1.53 www 1506: $uri=$refuri;
1507: $statecond=$refstatecond;
1.52 www 1508: }
1509: }
1.148 www 1510: }
1.29 www 1511: }
1.52 www 1512: }
1.29 www 1513:
1.52 www 1514: #
1.103 harris41 1515: # Gathered now: all privileges that could apply, and condition number
1.52 www 1516: #
1517: #
1518: # Full or no access?
1519: #
1.29 www 1520:
1.52 www 1521: if ($thisallowed=~/F/) {
1522: return 'F';
1523: }
1.29 www 1524:
1.52 www 1525: unless ($thisallowed) {
1526: return '';
1527: }
1.29 www 1528:
1.52 www 1529: # Restrictions exist, deal with them
1530: #
1531: # C:according to course preferences
1532: # R:according to resource settings
1533: # L:unless locked
1534: # X:according to user session state
1535: #
1536:
1537: # Possibly locked functionality, check all courses
1.54 www 1538: # Locks might take effect only after 10 minutes cache expiration for other
1539: # courses, and 2 minutes for current course
1.52 www 1540:
1541: my $envkey;
1542: if ($thisallowed=~/L/) {
1543: foreach $envkey (keys %ENV) {
1.54 www 1544: if ($envkey=~/^user\.role\.(st|ta)\.([^\.]*)/) {
1545: my $courseid=$2;
1546: my $roleid=$1.'.'.$2;
1.92 www 1547: $courseid=~s/^\///;
1.54 www 1548: my $expiretime=600;
1549: if ($ENV{'request.role'} eq $roleid) {
1550: $expiretime=120;
1551: }
1552: my ($cdom,$cnum,$csec)=split(/\//,$courseid);
1553: my $prefix='course.'.$cdom.'_'.$cnum.'.';
1554: if ((time-$ENV{$prefix.'last_cache'})>$expiretime) {
1555: &coursedescription($courseid);
1556: }
1557: if (($ENV{$prefix.'res.'.$uri.'.lock.sections'}=~/\,$csec\,/)
1558: || ($ENV{$prefix.'res.'.$uri.'.lock.sections'} eq 'all')) {
1559: if ($ENV{$prefix.'res.'.$uri.'.lock.expire'}>time) {
1.57 www 1560: &log($ENV{'user.domain'},$ENV{'user.name'},
1561: $ENV{'user.host'},
1562: 'Locked by res: '.$priv.' for '.$uri.' due to '.
1.52 www 1563: $cdom.'/'.$cnum.'/'.$csec.' expire '.
1.54 www 1564: $ENV{$prefix.'priv.'.$priv.'.lock.expire'});
1.52 www 1565: return '';
1566: }
1567: }
1.54 www 1568: if (($ENV{$prefix.'priv.'.$priv.'.lock.sections'}=~/\,$csec\,/)
1569: || ($ENV{$prefix.'priv.'.$priv.'.lock.sections'} eq 'all')) {
1570: if ($ENV{'priv.'.$priv.'.lock.expire'}>time) {
1.57 www 1571: &log($ENV{'user.domain'},$ENV{'user.name'},
1572: $ENV{'user.host'},
1573: 'Locked by priv: '.$priv.' for '.$uri.' due to '.
1.52 www 1574: $cdom.'/'.$cnum.'/'.$csec.' expire '.
1.54 www 1575: $ENV{$prefix.'priv.'.$priv.'.lock.expire'});
1.52 www 1576: return '';
1577: }
1578: }
1579: }
1.29 www 1580: }
1.52 www 1581: }
1582:
1583: #
1584: # Rest of the restrictions depend on selected course
1585: #
1586:
1587: unless ($ENV{'request.course.id'}) {
1588: return '1';
1589: }
1.29 www 1590:
1.52 www 1591: #
1592: # Now user is definitely in a course
1593: #
1.53 www 1594:
1595:
1596: # Course preferences
1597:
1598: if ($thisallowed=~/C/) {
1.54 www 1599: my $rolecode=(split(/\./,$ENV{'request.role'}))[0];
1600: if ($ENV{'course.'.$ENV{'request.course.id'}.'.'.$priv.'.roles.denied'}
1601: =~/\,$rolecode\,/) {
1.57 www 1602: &log($ENV{'user.domain'},$ENV{'user.name'},$ENV{'user.host'},
1603: 'Denied by role: '.$priv.' for '.$uri.' as '.$rolecode.' in '.
1.54 www 1604: $ENV{'request.course.id'});
1605: return '';
1606: }
1.53 www 1607: }
1608:
1609: # Resource preferences
1610:
1611: if ($thisallowed=~/R/) {
1.54 www 1612: my $rolecode=(split(/\./,$ENV{'request.role'}))[0];
1613: my $filename=$perlvar{'lonDocRoot'}.'/res/'.$uri.'.meta';
1614: if (-e $filename) {
1615: my @content;
1616: {
1617: my $fh=Apache::File->new($filename);
1618: @content=<$fh>;
1619: }
1620: if (join('',@content)=~
1621: /\<roledeny[^\>]*\>[^\<]*$rolecode[^\<]*\<\/roledeny\>/) {
1.57 www 1622: &log($ENV{'user.domain'},$ENV{'user.name'},$ENV{'user.host'},
1623: 'Denied by role: '.$priv.' for '.$uri.' as '.$rolecode);
1.54 www 1624: return '';
1625:
1626: }
1627: }
1.53 www 1628: }
1.30 www 1629:
1.52 www 1630: # Restricted by state?
1.30 www 1631:
1.52 www 1632: if ($thisallowed=~/X/) {
1633: if (&condval($statecond)) {
1634: return '2';
1635: } else {
1636: return '';
1637: }
1638: }
1.30 www 1639:
1.52 www 1640: return 'F';
1.12 www 1641: }
1642:
1643: # ----------------------------------------------------------------- Define Role
1644:
1645: sub definerole {
1646: if (allowed('mcr','/')) {
1647: my ($rolename,$sysrole,$domrole,$courole)=@_;
1.21 www 1648: map {
1649: my ($crole,$cqual)=split(/\&/,$_);
1650: if ($pr{'cr:s'}!~/$crole/) { return "refused:s:$crole"; }
1651: if ($pr{'cr:s'}=~/$crole\&/) {
1652: if ($pr{'cr:s'}!~/$crole\&\w*$cqual/) {
1653: return "refused:s:$crole&$cqual";
1654: }
1655: }
1656: } split('/',$sysrole);
1657: map {
1658: my ($crole,$cqual)=split(/\&/,$_);
1659: if ($pr{'cr:d'}!~/$crole/) { return "refused:d:$crole"; }
1660: if ($pr{'cr:d'}=~/$crole\&/) {
1661: if ($pr{'cr:d'}!~/$crole\&\w*$cqual/) {
1662: return "refused:d:$crole&$cqual";
1663: }
1664: }
1665: } split('/',$domrole);
1666: map {
1667: my ($crole,$cqual)=split(/\&/,$_);
1668: if ($pr{'cr:c'}!~/$crole/) { return "refused:c:$crole"; }
1669: if ($pr{'cr:c'}=~/$crole\&/) {
1670: if ($pr{'cr:c'}!~/$crole\&\w*$cqual/) {
1671: return "refused:c:$crole&$cqual";
1672: }
1673: }
1674: } split('/',$courole);
1.12 www 1675: my $command="encrypt:rolesput:$ENV{'user.domain'}:$ENV{'user.name'}:".
1676: "$ENV{'user.domain'}:$ENV{'user.name'}:".
1.21 www 1677: "rolesdef_$rolename=".
1678: escape($sysrole.'_'.$domrole.'_'.$courole);
1.12 www 1679: return reply($command,$ENV{'user.home'});
1680: } else {
1681: return 'refused';
1682: }
1.105 harris41 1683: }
1684:
1685: # ---------------- Make a metadata query against the network of library servers
1686:
1687: sub metadata_query {
1.116 harris41 1688: my ($query,$custom,$customshow)=@_;
1.120 harris41 1689: my %rhash;
1.123 harris41 1690: for my $server (keys %libserv) {
1.118 harris41 1691: unless ($custom or $customshow) {
1692: my $reply=&reply("querysend:".&escape($query),$server);
1693: $rhash{$server}=$reply;
1694: }
1695: else {
1696: my $reply=&reply("querysend:".&escape($query).':'.
1697: &escape($custom).':'.&escape($customshow),
1698: $server);
1699: $rhash{$server}=$reply;
1700: }
1.112 harris41 1701: }
1.118 harris41 1702: return \%rhash;
1.12 www 1703: }
1704:
1705: # ------------------------------------------------------------------ Plain Text
1706:
1707: sub plaintext {
1.22 www 1708: my $short=shift;
1709: return $prp{$short};
1.12 www 1710: }
1711:
1.25 www 1712: # ------------------------------------------------------------------ Plain Text
1713:
1714: sub fileembstyle {
1715: my $ending=shift;
1716: return $fe{$ending};
1717: }
1718:
1719: # ------------------------------------------------------------ Description Text
1720:
1.74 www 1721: sub filedescription {
1.25 www 1722: my $ending=shift;
1723: return $fd{$ending};
1724: }
1725:
1.12 www 1726: # ----------------------------------------------------------------- Assign Role
1727:
1728: sub assignrole {
1.21 www 1729: my ($udom,$uname,$url,$role,$end,$start)=@_;
1730: my $mrole;
1731: if ($role =~ /^cr\//) {
1.104 www 1732: unless (&allowed('ccr',$url)) {
1733: &logthis('Refused custom assignrole: '.
1734: $udom.' '.$uname.' '.$url.' '.$role.' '.$end.' '.$start.' by '.
1735: $ENV{'user.name'}.' at '.$ENV{'user.domain'});
1736: return 'refused';
1737: }
1.21 www 1738: $mrole='cr';
1739: } else {
1.82 www 1740: my $cwosec=$url;
1.83 www 1741: $cwosec=~s/^\/(\w+)\/(\w+)\/.*/$1\/$2/;
1.104 www 1742: unless (&allowed('c'.$role,$cwosec)) {
1743: &logthis('Refused assignrole: '.
1744: $udom.' '.$uname.' '.$url.' '.$role.' '.$end.' '.$start.' by '.
1745: $ENV{'user.name'}.' at '.$ENV{'user.domain'});
1746: return 'refused';
1747: }
1.21 www 1748: $mrole=$role;
1749: }
1750: my $command="encrypt:rolesput:$ENV{'user.domain'}:$ENV{'user.name'}:".
1751: "$udom:$uname:$url".'_'."$mrole=$role";
1.81 www 1752: if ($end) { $command.='_'.$end; }
1.21 www 1753: if ($start) {
1754: if ($end) {
1.81 www 1755: $command.='_'.$start;
1.21 www 1756: } else {
1.81 www 1757: $command.='_0_'.$start;
1.21 www 1758: }
1759: }
1760: return &reply($command,&homeserver($uname,$udom));
1.169 harris41 1761: }
1762:
1763: # -------------------------------------------------- Modify user authentication
1764: sub modifyuserauth {
1765: my ($udom,$uname,$umode,$upass)=@_;
1766: my $uhome=&homeserver($uname,$udom);
1767: &logthis('Call to modify user authentication'.$udom.', '.$uname.', '.
1768: $umode.' by '.$ENV{'user.name'}.' at '.$ENV{'user.domain'});
1769: my $reply=&reply('encrypt:changeuserauth:'.$udom.':'.$uname.':'.$umode.':'.
1770: &escape($upass),$uhome);
1771: unless ($reply eq 'ok') {
1772: return 'error: '.$reply;
1773: }
1.170 harris41 1774: return 'ok';
1.80 www 1775: }
1776:
1.81 www 1777: # --------------------------------------------------------------- Modify a user
1.80 www 1778:
1779:
1.81 www 1780: sub modifyuser {
1.80 www 1781: my ($udom,$uname,$uid,$umode,$upass,$first,$middle,$last,$gene)=@_;
1.81 www 1782: &logthis('Call to modify user '.$udom.', '.$uname.', '.$uid.', '.
1.80 www 1783: $umode.', '.$first.', '.$middle.', '.
1784: $last.', '.$gene.' by '.
1785: $ENV{'user.name'}.' at '.$ENV{'user.domain'});
1786: my $uhome=&homeserver($uname,$udom);
1787: # ----------------------------------------------------------------- Create User
1.81 www 1788: if (($uhome eq 'no_host') && ($umode) && ($upass)) {
1.80 www 1789: my $unhome='';
1790: if ($ENV{'course.'.$ENV{'request.course.id'}.'.domain'} eq $udom) {
1791: $unhome=$ENV{'course.'.$ENV{'request.course.id'}.'.home'};
1792: } else {
1793: my $tryserver;
1.81 www 1794: my $loadm=10000000;
1.80 www 1795: foreach $tryserver (keys %libserv) {
1796: if ($hostdom{$tryserver} eq $udom) {
1797: my $answer=reply('load',$tryserver);
1798: if (($answer=~/\d+/) && ($answer<$loadm)) {
1799: $loadm=$answer;
1800: $unhome=$tryserver;
1801: }
1802: }
1803: }
1804: }
1805: if (($unhome eq '') || ($unhome eq 'no_host')) {
1806: return 'error: find home';
1807: }
1808: my $reply=&reply('encrypt:makeuser:'.$udom.':'.$uname.':'.$umode.':'.
1809: &escape($upass),$unhome);
1810: unless ($reply eq 'ok') {
1811: return 'error: '.$reply;
1812: }
1813: $uhome=&homeserver($uname,$udom);
1814: if (($uhome eq '') || ($uhome eq 'no_host') || ($uhome ne $unhome)) {
1815: return 'error: verify home';
1816: }
1817: }
1818: # ---------------------------------------------------------------------- Add ID
1819: if ($uid) {
1820: $uid=~tr/A-Z/a-z/;
1821: my %uidhash=&idrget($udom,$uname);
1822: if (($uidhash{$uname}) && ($uidhash{$uname}!~/error\:/)) {
1823: unless ($uid eq $uidhash{$uname}) {
1824: return 'error: mismatch '.$uidhash{$uname}.' versus '.$uid;
1825: }
1826: } else {
1827: &idput($udom,($uname => $uid));
1828: }
1829: }
1830: # -------------------------------------------------------------- Add names, etc
1.134 albertel 1831: my %names=&get('environment',
1832: ['firstname','middlename','lastname','generation'],
1833: $udom,$uname);
1834: if ($first) { $names{'firstname'} = $first; }
1835: if ($middle) { $names{'middlename'} = $middle; }
1836: if ($last) { $names{'lastname'} = $last; }
1837: if ($gene) { $names{'generation'} = $gene; }
1838: my $reply = &put('environment', \%names, $udom,$uname);
1839: if ($reply ne 'ok') { return 'error: '.$reply; }
1.81 www 1840: &logthis('Success modifying user '.$udom.', '.$uname.', '.$uid.', '.
1.80 www 1841: $umode.', '.$first.', '.$middle.', '.
1842: $last.', '.$gene.' by '.
1843: $ENV{'user.name'}.' at '.$ENV{'user.domain'});
1.134 albertel 1844: return 'ok';
1.80 www 1845: }
1846:
1.81 www 1847: # -------------------------------------------------------------- Modify student
1.80 www 1848:
1.81 www 1849: sub modifystudent {
1850: my ($udom,$uname,$uid,$umode,$upass,$first,$middle,$last,$gene,$usec,
1851: $end,$start)=@_;
1852: my $cid='';
1853: unless ($cid=$ENV{'request.course.id'}) {
1.80 www 1854: return 'not_in_class';
1855: }
1856: # --------------------------------------------------------------- Make the user
1.81 www 1857: my $reply=&modifyuser
1.80 www 1858: ($udom,$uname,$uid,$umode,$upass,$first,$middle,$last,$gene);
1859: unless ($reply eq 'ok') { return $reply; }
1.81 www 1860: my $uhome=&homeserver($uname,$udom);
1861: if (($uhome eq '') || ($uhome eq 'no_host')) {
1862: return 'error: no such user';
1863: }
1.80 www 1864: # -------------------------------------------------- Add student to course list
1.160 www 1865: $reply=critical('put:'.$ENV{'course.'.$cid.'.domain'}.':'.
1.81 www 1866: $ENV{'course.'.$cid.'.num'}.':classlist:'.
1867: &escape($uname.':'.$udom).'='.
1868: &escape($end.':'.$start),
1869: $ENV{'course.'.$cid.'.home'});
1870: unless (($reply eq 'ok') || ($reply eq 'delayed')) {
1871: return 'error: '.$reply;
1872: }
1.80 www 1873: # ---------------------------------------------------- Add student role to user
1.83 www 1874: my $uurl='/'.$cid;
1.81 www 1875: $uurl=~s/\_/\//g;
1876: if ($usec) {
1877: $uurl.='/'.$usec;
1878: }
1879: return &assignrole($udom,$uname,$uurl,'st',$end,$start);
1.21 www 1880: }
1881:
1.84 www 1882: # ------------------------------------------------- Write to course preferences
1883:
1884: sub writecoursepref {
1885: my ($courseid,%prefs)=@_;
1886: $courseid=~s/^\///;
1887: $courseid=~s/\_/\//g;
1888: my ($cdomain,$cnum)=split(/\//,$courseid);
1889: my $chome=homeserver($cnum,$cdomain);
1890: if (($chome eq '') || ($chome eq 'no_host')) {
1891: return 'error: no such course';
1892: }
1893: my $cstring='';
1894: map {
1895: $cstring.=escape($_).'='.escape($prefs{$_}).'&';
1896: } keys %prefs;
1897: $cstring=~s/\&$//;
1898: return reply('put:'.$cdomain.':'.$cnum.':environment:'.$cstring,$chome);
1899: }
1900:
1901: # ---------------------------------------------------------- Make/modify course
1902:
1903: sub createcourse {
1904: my ($udom,$description,$url)=@_;
1905: $url=&declutter($url);
1906: my $cid='';
1907: unless (&allowed('ccc',$ENV{'user.domain'})) {
1908: return 'refused';
1909: }
1910: unless ($udom eq $ENV{'user.domain'}) {
1911: return 'refused';
1912: }
1913: # ------------------------------------------------------------------- Create ID
1914: my $uname=substr($$.time,0,5).unpack("H8",pack("I32",time)).
1915: unpack("H2",pack("I32",int(rand(255)))).$perlvar{'lonHostID'};
1916: # ----------------------------------------------- Make sure that does not exist
1917: my $uhome=&homeserver($uname,$udom);
1918: unless (($uhome eq '') || ($uhome eq 'no_host')) {
1919: $uname=substr($$.time,0,5).unpack("H8",pack("I32",time)).
1920: unpack("H2",pack("I32",int(rand(255)))).$perlvar{'lonHostID'};
1921: $uhome=&homeserver($uname,$udom);
1922: unless (($uhome eq '') || ($uhome eq 'no_host')) {
1923: return 'error: unable to generate unique course-ID';
1924: }
1925: }
1926: # ------------------------------------------------------------- Make the course
1927: my $reply=&reply('encrypt:makeuser:'.$udom.':'.$uname.':none::',
1928: $ENV{'user.home'});
1929: unless ($reply eq 'ok') { return 'error: '.$reply; }
1.160 www 1930: $uhome=&homeserver($uname,$udom);
1.84 www 1931: if (($uhome eq '') || ($uhome eq 'no_host')) {
1932: return 'error: no such course';
1933: }
1934: &writecoursepref($udom.'_'.$uname,
1935: ('description' => $description,
1936: 'url' => $url));
1937: return '/'.$udom.'/'.$uname;
1938: }
1939:
1.21 www 1940: # ---------------------------------------------------------- Assign Custom Role
1941:
1942: sub assigncustomrole {
1943: my ($udom,$uname,$url,$rdom,$rnam,$rolename,$end,$start)=@_;
1944: return &assignrole($udom,$uname,$url,'cr/'.$rdom.'/'.$rnam.'/'.$rolename,
1945: $end,$start);
1946: }
1947:
1948: # ----------------------------------------------------------------- Revoke Role
1949:
1950: sub revokerole {
1951: my ($udom,$uname,$url,$role)=@_;
1952: my $now=time;
1953: return &assignrole($udom,$uname,$url,$role,$now);
1954: }
1955:
1956: # ---------------------------------------------------------- Revoke Custom Role
1957:
1958: sub revokecustomrole {
1959: my ($udom,$uname,$url,$rdom,$rnam,$rolename)=@_;
1960: my $now=time;
1961: return &assigncustomrole($udom,$uname,$url,$rdom,$rnam,$rolename,$now);
1.17 www 1962: }
1963:
1964: # ------------------------------------------------------------ Directory lister
1965:
1966: sub dirlist {
1967: my $uri=shift;
1.18 www 1968: $uri=~s/^\///;
1969: $uri=~s/\/$//;
1.19 www 1970: my ($res,$udom,$uname,@rest)=split(/\//,$uri);
1971: if ($udom) {
1972: if ($uname) {
1973: my $listing=reply('ls:'.$perlvar{'lonDocRoot'}.'/'.$uri,
1974: homeserver($uname,$udom));
1975: return split(/:/,$listing);
1976: } else {
1977: my $tryserver;
1978: my %allusers=();
1979: foreach $tryserver (keys %libserv) {
1980: if ($hostdom{$tryserver} eq $udom) {
1981: my $listing=reply('ls:'.$perlvar{'lonDocRoot'}.'/res/'.$udom,
1982: $tryserver);
1983: if (($listing ne 'no_such_dir') && ($listing ne 'empty')
1984: && ($listing ne 'con_lost')) {
1985: map {
1986: my ($entry,@stat)=split(/&/,$_);
1987: $allusers{$entry}=1;
1988: } split(/:/,$listing);
1989: }
1990: }
1991: }
1992: my $alluserstr='';
1993: map {
1994: $alluserstr.=$_.'&user:';
1995: } sort keys %allusers;
1996: $alluserstr=~s/:$//;
1997: return split(/:/,$alluserstr);
1998: }
1999: } else {
2000: my $tryserver;
2001: my %alldom=();
2002: foreach $tryserver (keys %libserv) {
2003: $alldom{$hostdom{$tryserver}}=1;
2004: }
2005: my $alldomstr='';
2006: map {
2007: $alldomstr.=$perlvar{'lonDocRoot'}.'/res/'.$_.'&domain:';
2008: } sort keys %alldom;
2009: $alldomstr=~s/:$//;
2010: return split(/:/,$alldomstr);
2011: }
1.26 www 2012: }
2013:
2014: # -------------------------------------------------------- Value of a Condition
2015:
1.40 www 2016: sub directcondval {
2017: my $number=shift;
2018: if ($ENV{'user.state.'.$ENV{'request.course.id'}}) {
2019: return substr($ENV{'user.state.'.$ENV{'request.course.id'}},$number,1);
2020: } else {
2021: return 2;
2022: }
2023: }
2024:
1.26 www 2025: sub condval {
2026: my $condidx=shift;
2027: my $result=0;
1.54 www 2028: my $allpathcond='';
2029: map {
2030: if (defined($ENV{'acc.cond.'.$ENV{'request.course.id'}.'.'.$_})) {
2031: $allpathcond.=
2032: '('.$ENV{'acc.cond.'.$ENV{'request.course.id'}.'.'.$_}.')|';
2033: }
2034: } split(/\|/,$condidx);
2035: $allpathcond=~s/\|$//;
1.33 www 2036: if ($ENV{'request.course.id'}) {
1.54 www 2037: if ($allpathcond) {
1.26 www 2038: my $operand='|';
2039: my @stack;
2040: map {
2041: if ($_ eq '(') {
2042: push @stack,($operand,$result)
2043: } elsif ($_ eq ')') {
2044: my $before=pop @stack;
2045: if (pop @stack eq '&') {
2046: $result=$result>$before?$before:$result;
2047: } else {
2048: $result=$result>$before?$result:$before;
2049: }
2050: } elsif (($_ eq '&') || ($_ eq '|')) {
2051: $operand=$_;
2052: } else {
1.40 www 2053: my $new=directcondval($_);
1.26 www 2054: if ($operand eq '&') {
2055: $result=$result>$new?$new:$result;
2056: } else {
2057: $result=$result>$new?$result:$new;
2058: }
2059: }
1.54 www 2060: } ($allpathcond=~/(\d+|\(|\)|\&|\|)/g);
1.26 www 2061: }
2062: }
2063: return $result;
1.28 www 2064: }
2065:
2066: # --------------------------------------------------------- Value of a Variable
2067:
1.58 www 2068: sub EXT {
1.147 www 2069: my ($varname,$symbparm)=@_;
1.68 www 2070: unless ($varname) { return ''; }
1.48 www 2071: my ($realm,$space,$qualifier,@therest)=split(/\./,$varname);
2072: my $rest;
2073: if ($therest[0]) {
2074: $rest=join('.',@therest);
2075: } else {
2076: $rest='';
2077: }
1.57 www 2078: my $qualifierrest=$qualifier;
2079: if ($rest) { $qualifierrest.='.'.$rest; }
2080: my $spacequalifierrest=$space;
2081: if ($qualifierrest) { $spacequalifierrest.='.'.$qualifierrest; }
1.28 www 2082: if ($realm eq 'user') {
1.48 www 2083: # --------------------------------------------------------------- user.resource
2084: if ($space eq 'resource') {
1.122 albertel 2085: my %restored=&restore();
1.57 www 2086: return $restored{$qualifierrest};
1.48 www 2087: # ----------------------------------------------------------------- user.access
2088: } elsif ($space eq 'access') {
2089: return &allowed($qualifier,$rest);
2090: # ------------------------------------------ user.preferences, user.environment
2091: } elsif (($space eq 'preferences') || ($space eq 'environment')) {
1.57 www 2092: return $ENV{join('.',('environment',$qualifierrest))};
1.48 www 2093: # ----------------------------------------------------------------- user.course
2094: } elsif ($space eq 'course') {
2095: return $ENV{join('.',('request.course',$qualifier))};
2096: # ------------------------------------------------------------------- user.role
2097: } elsif ($space eq 'role') {
2098: my ($role,$where)=split(/\./,$ENV{'request.role'});
2099: if ($qualifier eq 'value') {
2100: return $role;
2101: } elsif ($qualifier eq 'extent') {
2102: return $where;
2103: }
2104: # ----------------------------------------------------------------- user.domain
2105: } elsif ($space eq 'domain') {
2106: return $ENV{'user.domain'};
2107: # ------------------------------------------------------------------- user.name
2108: } elsif ($space eq 'name') {
2109: return $ENV{'user.name'};
2110: # ---------------------------------------------------- Any other user namespace
1.29 www 2111: } else {
1.48 www 2112: my $item=($rest)?$qualifier.'.'.$rest:$qualifier;
1.131 albertel 2113: my %reply=&get($space,[$item]);
1.48 www 2114: return $reply{$item};
2115: }
2116: } elsif ($realm eq 'request') {
2117: # ------------------------------------------------------------- request.browser
2118: if ($space eq 'browser') {
2119: return $ENV{'browser.'.$qualifier};
1.57 www 2120: # ------------------------------------------------------------ request.filename
2121: } else {
2122: return $ENV{'request.'.$spacequalifierrest};
1.29 www 2123: }
1.28 www 2124: } elsif ($realm eq 'course') {
1.48 www 2125: # ---------------------------------------------------------- course.description
1.127 ng 2126: return $ENV{'course.'.$ENV{'request.course.id'}.'.'.
1.57 www 2127: $spacequalifierrest};
2128: } elsif ($realm eq 'resource') {
1.127 ng 2129: if ($ENV{'request.course.id'}) {
1.165 www 2130:
2131: # print '<br>'.$space.' - '.$qualifier.' - '.$spacequalifierrest;
2132:
2133:
1.60 www 2134: # ----------------------------------------------------- Cascading lookup scheme
1.147 www 2135: my $symbp;
2136: if ($symbparm) {
2137: $symbp=$symbparm;
2138: } else {
2139: $symbp=&symbread();
2140: }
1.127 ng 2141: my $mapp=(split(/\_\_\_/,$symbp))[0];
1.69 www 2142:
1.127 ng 2143: my $symbparm=$symbp.'.'.$spacequalifierrest;
2144: my $mapparm=$mapp.'___(all).'.$spacequalifierrest;
1.69 www 2145:
1.127 ng 2146: my $seclevel=
1.69 www 2147: $ENV{'request.course.id'}.'.['.
2148: $ENV{'request.course.sec'}.'].'.$spacequalifierrest;
1.127 ng 2149: my $seclevelr=
1.69 www 2150: $ENV{'request.course.id'}.'.['.
2151: $ENV{'request.course.sec'}.'].'.$symbparm;
1.127 ng 2152: my $seclevelm=
1.69 www 2153: $ENV{'request.course.id'}.'.['.
2154: $ENV{'request.course.sec'}.'].'.$mapparm;
2155:
1.127 ng 2156: my $courselevel=
1.60 www 2157: $ENV{'request.course.id'}.'.'.$spacequalifierrest;
1.127 ng 2158: my $courselevelr=
1.69 www 2159: $ENV{'request.course.id'}.'.'.$symbparm;
1.127 ng 2160: my $courselevelm=
1.69 www 2161: $ENV{'request.course.id'}.'.'.$mapparm;
2162:
1.60 www 2163: # ----------------------------------------------------------- first, check user
1.127 ng 2164: my %resourcedata=get('resourcedata',
1.131 albertel 2165: [$courselevelr,$courselevelm,$courselevel]);
1.127 ng 2166: if (($resourcedata{$courselevelr}!~/^error\:/) &&
2167: ($resourcedata{$courselevelr}!~/^con_lost/)) {
1.69 www 2168:
1.127 ng 2169: if ($resourcedata{$courselevelr}) {
2170: return $resourcedata{$courselevelr}; }
2171: if ($resourcedata{$courselevelm}) {
2172: return $resourcedata{$courselevelm}; }
2173: if ($resourcedata{$courselevel}) { return $resourcedata{$courselevel}; }
1.69 www 2174:
1.94 www 2175: } else {
2176: if ($resourcedata{$courselevelr}!~/No such file/) {
2177: &logthis("<font color=blue>WARNING:".
2178: " Trying to get resource data for ".$ENV{'user.name'}." at "
2179: .$ENV{'user.domain'}.": ".$resourcedata{$courselevelr}.
2180: "</font>");
2181: }
1.63 www 2182: }
1.95 www 2183:
1.60 www 2184: # -------------------------------------------------------- second, check course
1.96 www 2185:
1.60 www 2186: my $reply=&reply('get:'.
1.96 www 2187: $ENV{'course.'.$ENV{'request.course.id'}.'.domain'}.':'.
2188: $ENV{'course.'.$ENV{'request.course.id'}.'.num'}.
1.79 www 2189: ':resourcedata:'.
2190: &escape($seclevelr).'&'.&escape($seclevelm).'&'.&escape($seclevel).'&'.
2191: &escape($courselevelr).'&'.&escape($courselevelm).'&'.&escape($courselevel),
1.96 www 2192: $ENV{'course.'.$ENV{'request.course.id'}.'.home'});
1.63 www 2193: if ($reply!~/^error\:/) {
1.79 www 2194: map {
2195: if ($_) { return &unescape($_); }
2196: } split(/\&/,$reply);
1.63 www 2197: }
1.94 www 2198: if (($reply=~/^con_lost/) || ($reply=~/^error\:/)) {
2199: &logthis("<font color=blue>WARNING:".
1.95 www 2200: " Getting ".$reply." asking for ".$varname." for ".
1.96 www 2201: $ENV{'course.'.$ENV{'request.course.id'}.'.num'}.
1.95 www 2202: ' at '.
1.96 www 2203: $ENV{'course.'.$ENV{'request.course.id'}.'.domain'}.
1.95 www 2204: ' from '.
1.96 www 2205: $ENV{'course.'.$ENV{'request.course.id'}.'.home'}.
1.94 www 2206: "</font>");
2207: }
1.60 www 2208: # ------------------------------------------------------ third, check map parms
1.65 www 2209: my %parmhash=();
2210: my $thisparm='';
2211: if (tie(%parmhash,'GDBM_File',
2212: $ENV{'request.course.fn'}.'_parms.db',&GDBM_READER,0640)) {
2213: $thisparm=$parmhash{$symbparm};
2214: untie(%parmhash);
1.60 www 2215: }
1.65 www 2216: if ($thisparm) { return $thisparm; }
1.60 www 2217: }
2218:
2219: # --------------------------------------------- last, look in resource metadata
1.71 www 2220:
1.78 www 2221: $spacequalifierrest=~s/\./\_/;
1.71 www 2222: my $metadata=&metadata($ENV{'request.filename'},$spacequalifierrest);
2223: if ($metadata) { return $metadata; }
1.78 www 2224: $metadata=&metadata($ENV{'request.filename'},
2225: 'parameter_'.$spacequalifierrest);
2226: if ($metadata) { return $metadata; }
1.142 www 2227:
1.145 www 2228: # ------------------------------------------------------------------ Cascade up
2229:
2230: unless ($space eq '0') {
1.165 www 2231: my ($part,$id)=split(/\_/,$space);
1.145 www 2232: if ($id) {
1.147 www 2233: my $partgeneral=&EXT('resource.'.$part.'.'.$qualifierrest,
2234: $symbparm);
1.145 www 2235: if ($partgeneral) { return $partgeneral; }
2236: } else {
1.147 www 2237: my $resourcegeneral=&EXT('resource.0.'.$qualifierrest,
2238: $symbparm);
1.145 www 2239: if ($resourcegeneral) { return $resourcegeneral; }
2240: }
2241: }
1.71 www 2242:
1.48 www 2243: # ---------------------------------------------------- Any other user namespace
2244: } elsif ($realm eq 'environment') {
2245: # ----------------------------------------------------------------- environment
1.127 ng 2246: return $ENV{'environment.'.$spacequalifierrest};
1.28 www 2247: } elsif ($realm eq 'system') {
1.48 www 2248: # ----------------------------------------------------------------- system.time
2249: if ($space eq 'time') {
2250: return time;
2251: }
1.28 www 2252: }
1.48 www 2253: return '';
1.61 www 2254: }
2255:
1.71 www 2256: # ---------------------------------------------------------------- Get metadata
2257:
2258: sub metadata {
1.176 www 2259: my ($uri,$what,$liburi,$prefix,$depthcount)=@_;
1.78 www 2260:
1.71 www 2261: $uri=&declutter($uri);
1.73 www 2262: my $filename=$uri;
2263: $uri=~s/\.meta$//;
1.172 www 2264: #
2265: # Is the metadata already cached?
1.177 www 2266: # Look at timestamp of caching
1.172 www 2267: # Everything is cached by the main uri, libraries are never directly cached
2268: #
1.174 www 2269: unless (abs($metacache{$uri.':cachedtimestamp'}-time)<600) {
1.172 www 2270: #
2271: # Is this a recursive call for a library?
2272: #
1.171 www 2273: if ($liburi) {
2274: $liburi=&declutter($liburi);
2275: $filename=$liburi;
2276: }
1.140 www 2277: my %metathesekeys=();
1.73 www 2278: unless ($filename=~/\.meta$/) { $filename.='.meta'; }
2279: my $metastring=&getfile($perlvar{'lonDocRoot'}.'/res/'.$filename);
1.71 www 2280: my $parser=HTML::TokeParser->new(\$metastring);
2281: my $token;
1.140 www 2282: undef %metathesekeys;
1.71 www 2283: while ($token=$parser->get_token) {
2284: if ($token->[0] eq 'S') {
1.135 www 2285: if (defined($token->[2]->{'package'})) {
1.172 www 2286: #
2287: # This is a package - get package info
2288: #
1.136 www 2289: my $package=$token->[2]->{'package'};
2290: my $keyroot='';
1.172 www 2291: if ($prefix) {
2292: $keyroot.='_'.$prefix;
2293: } else {
2294: if (defined($token->[2]->{'part'})) {
2295: $keyroot.='_'.$token->[2]->{'part'};
2296: }
1.136 www 2297: }
2298: if (defined($token->[2]->{'id'})) {
1.165 www 2299: $keyroot.='_'.$token->[2]->{'id'};
1.136 www 2300: }
2301: if ($metacache{$uri.':packages'}) {
2302: $metacache{$uri.':packages'}.=','.$package.$keyroot;
2303: } else {
2304: $metacache{$uri.':packages'}=$package.$keyroot;
2305: }
1.137 www 2306: map {
2307: if ($_=~/^$package\&/) {
2308: my ($pack,$name,$subp)=split(/\&/,$_);
1.139 www 2309: my $value=$packagetab{$_};
1.144 www 2310: my $part=$keyroot;
2311: $part=~s/^\_//;
1.139 www 2312: if ($subp eq 'display') {
2313: $value.=' [Part: '.$part.']';
2314: }
2315: my $unikey='parameter'.$keyroot.'_'.$name;
1.140 www 2316: $metathesekeys{$unikey}=1;
1.144 www 2317: $metacache{$uri.':'.$unikey.'.part'}=$part;
1.141 www 2318: unless
2319: (defined($metacache{$uri.':'.$unikey.'.'.$subp})) {
2320: $metacache{$uri.':'.$unikey.'.'.$subp}=$value;
2321: }
1.137 www 2322: }
2323: } keys %packagetab;
1.135 www 2324: } else {
1.172 www 2325: #
2326: # This is not a package - some other kind of start tag
2327: #
1.175 www 2328: my $entry=$token->[1];
1.176 www 2329: my $unikey;
2330: if ($entry eq 'import') {
2331: $unikey='';
2332: } else {
2333: $unikey=$entry;
2334: }
1.172 www 2335: if ($prefix) {
1.176 www 2336: $unikey.=$prefix;
1.172 www 2337: } else {
2338: if (defined($token->[2]->{'part'})) {
2339: $unikey.='_'.$token->[2]->{'part'};
2340: }
1.136 www 2341: }
2342: if (defined($token->[2]->{'id'})) {
1.165 www 2343: $unikey.='_'.$token->[2]->{'id'};
1.71 www 2344: }
1.175 www 2345:
2346: if ($entry eq 'import') {
2347: #
2348: # Importing a library here
1.176 www 2349: #
2350: if (defined($depthcount)) { $depthcount++; } else
2351: { $depthcount=0; }
2352: if ($depthcount<20) {
1.177 www 2353: map {
2354: $metathesekeys{$_}=1;
2355: } split(/\,/,&metadata($uri,'keys',
2356: $parser->get_text('/import'),$unikey,
2357: $depthcount));
1.176 www 2358: }
1.175 www 2359: } else {
2360:
1.72 www 2361: if (defined($token->[2]->{'name'})) {
1.71 www 2362: $unikey.='_'.$token->[2]->{'name'};
2363: }
1.140 www 2364: $metathesekeys{$unikey}=1;
1.71 www 2365: map {
2366: $metacache{$uri.':'.$unikey.'.'.$_}=$token->[2]->{$_};
1.72 www 2367: } @{$token->[3]};
1.78 www 2368: unless (
2369: $metacache{$uri.':'.$unikey}=$parser->get_text('/'.$entry)
2370: ) { $metacache{$uri.':'.$unikey}=
2371: $metacache{$uri.':'.$unikey.'.default'};
2372: }
1.172 www 2373: # end of not-a-package not-a-library import
1.175 www 2374: }
1.172 www 2375: # end of not-a-package start tag
2376: }
2377: # the next is the end of "start tag"
1.140 www 2378: }
1.71 www 2379: }
1.140 www 2380: $metacache{$uri.':keys'}=join(',',keys %metathesekeys);
1.174 www 2381: $metacache{$uri.':cachedtimestamp'}=time;
1.177 www 2382: # this is the end of "was not already recently cached
1.71 www 2383: }
2384: return $metacache{$uri.':'.$what};
2385: }
2386:
1.31 www 2387: # ------------------------------------------------- Update symbolic store links
2388:
2389: sub symblist {
2390: my ($mapname,%newhash)=@_;
2391: $mapname=declutter($mapname);
2392: my %hash;
2393: if (($ENV{'request.course.fn'}) && (%newhash)) {
2394: if (tie(%hash,'GDBM_File',$ENV{'request.course.fn'}.'_symb.db',
2395: &GDBM_WRCREAT,0640)) {
2396: map {
2397: $hash{declutter($_)}=$mapname.'___'.$newhash{$_};
2398: } keys %newhash;
2399: if (untie(%hash)) {
2400: return 'ok';
2401: }
2402: }
2403: }
2404: return 'error';
2405: }
2406:
2407: # ------------------------------------------------------ Return symb list entry
2408:
2409: sub symbread {
1.44 www 2410: my $thisfn=shift;
2411: unless ($thisfn) {
1.179 www 2412: if ($ENV{'request.symb'}) { return $ENV{'request.symb'}; }
1.44 www 2413: $thisfn=$ENV{'request.filename'};
2414: }
2415: $thisfn=declutter($thisfn);
1.31 www 2416: my %hash;
1.37 www 2417: my %bighash;
2418: my $syval='';
1.45 www 2419: if (($ENV{'request.course.fn'}) && ($thisfn)) {
1.31 www 2420: if (tie(%hash,'GDBM_File',$ENV{'request.course.fn'}.'_symb.db',
2421: &GDBM_READER,0640)) {
2422: $syval=$hash{$thisfn};
1.37 www 2423: untie(%hash);
2424: }
2425: # ---------------------------------------------------------- There was an entry
2426: if ($syval) {
2427: unless ($syval=~/\_\d+$/) {
2428: unless ($ENV{'form.request.prefix'}=~/\.(\d+)\_$/) {
1.44 www 2429: &appenv('request.ambiguous' => $thisfn);
1.37 www 2430: return '';
2431: }
2432: $syval.=$1;
2433: }
2434: } else {
2435: # ------------------------------------------------------- Was not in symb table
2436: if (tie(%bighash,'GDBM_File',$ENV{'request.course.fn'}.'.db',
2437: &GDBM_READER,0640)) {
2438: # ---------------------------------------------- Get ID(s) for current resource
2439: my $ids=$bighash{'ids_/res/'.$thisfn};
1.65 www 2440: unless ($ids) {
2441: $ids=$bighash{'ids_/'.$thisfn};
2442: }
1.37 www 2443: if ($ids) {
2444: # ------------------------------------------------------------------- Has ID(s)
2445: my @possibilities=split(/\,/,$ids);
1.39 www 2446: if ($#possibilities==0) {
2447: # ----------------------------------------------- There is only one possibility
1.37 www 2448: my ($mapid,$resid)=split(/\./,$ids);
2449: $syval=declutter($bighash{'map_id_'.$mapid}).'___'.$resid;
2450: } else {
1.39 www 2451: # ------------------------------------------ There is more than one possibility
2452: my $realpossible=0;
2453: map {
2454: my $file=$bighash{'src_'.$_};
2455: if (&allowed('bre',$file)) {
2456: my ($mapid,$resid)=split(/\./,$_);
2457: if ($bighash{'map_type_'.$mapid} ne 'page') {
2458: $realpossible++;
2459: $syval=declutter($bighash{'map_id_'.$mapid}).
2460: '___'.$resid;
2461: }
2462: }
2463: } @possibilities;
2464: if ($realpossible!=1) { $syval=''; }
1.37 www 2465: }
2466: }
2467: untie(%bighash)
2468: }
1.31 www 2469: }
1.62 www 2470: if ($syval) {
2471: return $syval.'___'.$thisfn;
2472: }
1.31 www 2473: }
1.44 www 2474: &appenv('request.ambiguous' => $thisfn);
1.31 www 2475: return '';
2476: }
2477:
2478: # ---------------------------------------------------------- Return random seed
2479:
1.32 www 2480: sub numval {
2481: my $txt=shift;
2482: $txt=~tr/A-J/0-9/;
2483: $txt=~tr/a-j/0-9/;
2484: $txt=~tr/K-T/0-9/;
2485: $txt=~tr/k-t/0-9/;
2486: $txt=~tr/U-Z/0-5/;
2487: $txt=~tr/u-z/0-5/;
2488: $txt=~s/\D//g;
2489: return int($txt);
2490: }
2491:
1.31 www 2492: sub rndseed {
1.155 albertel 2493: my ($symb,$courseid,$domain,$username)=@_;
2494: if (!$symb) {
2495: unless ($symb=&symbread()) { return time; }
2496: }
2497: if (!$courseid) { $courseid=$ENV{'request.course.id'};}
2498: if (!$domain) {$domain=$ENV{'user.domain'};}
2499: if (!$username) {$username=$ENV{'user.name'};}
2500: {
1.98 albertel 2501: use integer;
2502: my $symbchck=unpack("%32C*",$symb) << 27;
1.100 albertel 2503: my $symbseed=numval($symb) << 22;
1.155 albertel 2504: my $namechck=unpack("%32C*",$username) << 17;
2505: my $nameseed=numval($username) << 12;
2506: my $domainseed=unpack("%32C*",$domain) << 7;
2507: my $courseseed=unpack("%32C*",$courseid);
1.98 albertel 2508: my $num=$symbseed+$nameseed+$domainseed+$courseseed+$namechck+$symbchck;
1.99 albertel 2509: #uncommenting these lines can break things!
2510: #&Apache::lonxml::debug("$symbseed:$nameseed;$domainseed|$courseseed;$namechck:$symbchck");
2511: #&Apache::lonxml::debug("rndseed :$num:$symb");
1.98 albertel 2512: return $num;
2513: }
1.36 albertel 2514: }
2515:
1.76 www 2516: sub ireceipt {
2517: my ($funame,$fudom,$fucourseid,$fusymb)=@_;
2518: my $cuname=unpack("%32C*",$funame);
2519: my $cudom=unpack("%32C*",$fudom);
2520: my $cucourseid=unpack("%32C*",$fucourseid);
2521: my $cusymb=unpack("%32C*",$fusymb);
1.77 www 2522: my $cunique=unpack("%32C*",$perlvar{'lonReceipt'});
1.76 www 2523: return unpack("%32C*",$perlvar{'lonHostID'}).'-'.
2524: ($cunique%$cuname+
2525: $cunique%$cudom+
2526: $cusymb%$cuname+
2527: $cusymb%$cudom+
2528: $cucourseid%$cuname+
2529: $cucourseid%$cudom);
2530: }
2531:
2532: sub receipt {
2533: return &ireceipt($ENV{'user.name'},$ENV{'user.domain'},
2534: $ENV{'request.course.id'},&symbread());
2535: }
2536:
1.36 albertel 2537: # ------------------------------------------------------------ Serves up a file
2538: # returns either the contents of the file or a -1
2539: sub getfile {
2540: my $file=shift;
1.37 www 2541: &repcopy($file);
1.36 albertel 2542: if (! -e $file ) { return -1; };
2543: my $fh=Apache::File->new($file);
2544: my $a='';
2545: while (<$fh>) { $a .=$_; }
2546: return $a
2547: }
2548:
2549: sub filelocation {
2550: my ($dir,$file) = @_;
2551: my $location;
2552: $file=~ s/^\s*(\S+)\s*$/$1/; ## strip off leading and trailing spaces
1.59 albertel 2553: if ($file=~m:^/~:) { # is a contruction space reference
2554: $location = $file;
2555: $location =~ s:/~(.*?)/(.*):/home/$1/public_html/$2:;
1.36 albertel 2556: } else {
1.59 albertel 2557: $file=~s/^$perlvar{'lonDocRoot'}//;
2558: $file=~s:^/*res::;
2559: if ( !( $file =~ m:^/:) ) {
2560: $location = $dir. '/'.$file;
2561: } else {
2562: $location = '/home/httpd/html/res'.$file;
2563: }
1.36 albertel 2564: }
2565: $location=~s://+:/:g; # remove duplicate /
1.46 www 2566: while ($location=~m:/\.\./:) {$location=~ s:/[^/]+/\.\./:/:g;} #remove dir/..
2567: return $location;
2568: }
1.36 albertel 2569:
1.46 www 2570: sub hreflocation {
2571: my ($dir,$file)=@_;
2572: unless (($_=~/^http:\/\//i) || ($_=~/^\//)) {
2573: my $finalpath=filelocation($dir,$file);
2574: $finalpath=~s/^\/home\/httpd\/html//;
2575: return $finalpath;
2576: } else {
2577: return $file;
2578: }
1.31 www 2579: }
2580:
2581: # ------------------------------------------------------------- Declutters URLs
2582:
2583: sub declutter {
2584: my $thisfn=shift;
2585: $thisfn=~s/^$perlvar{'lonDocRoot'}//;
2586: $thisfn=~s/^\///;
2587: $thisfn=~s/^res\///;
2588: return $thisfn;
1.12 www 2589: }
2590:
2591: # -------------------------------------------------------- Escape Special Chars
2592:
2593: sub escape {
2594: my $str=shift;
2595: $str =~ s/(\W)/"%".unpack('H2',$1)/eg;
2596: return $str;
2597: }
2598:
2599: # ----------------------------------------------------- Un-Escape Special Chars
2600:
2601: sub unescape {
2602: my $str=shift;
2603: $str =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C",hex($1))/eg;
2604: return $str;
2605: }
1.11 www 2606:
1.1 albertel 2607: # ================================================================ Main Program
2608:
1.179 www 2609: BEGIN {
1.1 albertel 2610: # ------------------------------------------------------------ Read access.conf
2611: {
2612: my $config=Apache::File->new("/etc/httpd/conf/access.conf");
2613:
2614: while (my $configline=<$config>) {
2615: if ($configline =~ /PerlSetVar/) {
2616: my ($dummy,$varname,$varvalue)=split(/\s+/,$configline);
1.8 www 2617: chomp($varvalue);
1.1 albertel 2618: $perlvar{$varname}=$varvalue;
2619: }
2620: }
2621: }
2622:
2623: # ------------------------------------------------------------- Read hosts file
2624: {
2625: my $config=Apache::File->new("$perlvar{'lonTabDir'}/hosts.tab");
2626:
2627: while (my $configline=<$config>) {
1.154 www 2628: chomp($configline);
1.1 albertel 2629: my ($id,$domain,$role,$name,$ip)=split(/:/,$configline);
2630: $hostname{$id}=$name;
2631: $hostdom{$id}=$domain;
1.150 www 2632: $hostip{$id}=$ip;
1.1 albertel 2633: if ($role eq 'library') { $libserv{$id}=$name; }
2634: }
2635: }
2636:
2637: # ------------------------------------------------------ Read spare server file
2638: {
2639: my $config=Apache::File->new("$perlvar{'lonTabDir'}/spare.tab");
2640:
2641: while (my $configline=<$config>) {
2642: chomp($configline);
2643: if (($configline) && ($configline ne $perlvar{'lonHostID'})) {
2644: $spareid{$configline}=1;
2645: }
2646: }
2647: }
1.11 www 2648: # ------------------------------------------------------------ Read permissions
2649: {
2650: my $config=Apache::File->new("$perlvar{'lonTabDir'}/roles.tab");
2651:
2652: while (my $configline=<$config>) {
2653: chomp($configline);
1.160 www 2654: if ($configline) {
1.11 www 2655: my ($role,$perm)=split(/ /,$configline);
2656: if ($perm ne '') { $pr{$role}=$perm; }
1.160 www 2657: }
1.11 www 2658: }
2659: }
2660:
2661: # -------------------------------------------- Read plain texts for permissions
2662: {
2663: my $config=Apache::File->new("$perlvar{'lonTabDir'}/rolesplain.tab");
2664:
2665: while (my $configline=<$config>) {
2666: chomp($configline);
1.160 www 2667: if ($configline) {
1.11 www 2668: my ($short,$plain)=split(/:/,$configline);
2669: if ($plain ne '') { $prp{$short}=$plain; }
1.160 www 2670: }
1.135 www 2671: }
2672: }
2673:
2674: # ---------------------------------------------------------- Read package table
2675: {
2676: my $config=Apache::File->new("$perlvar{'lonTabDir'}/packages.tab");
2677:
2678: while (my $configline=<$config>) {
2679: chomp($configline);
2680: my ($short,$plain)=split(/:/,$configline);
1.143 www 2681: my ($pack,$name)=split(/\&/,$short);
2682: if ($plain ne '') {
2683: $packagetab{$pack.'&'.$name.'&name'}=$name;
2684: $packagetab{$short}=$plain;
2685: }
1.25 www 2686: }
2687: }
2688:
2689: # ------------------------------------------------------------- Read file types
2690: {
2691: my $config=Apache::File->new("$perlvar{'lonTabDir'}/filetypes.tab");
2692:
2693: while (my $configline=<$config>) {
2694: chomp($configline);
2695: my ($ending,$emb,@descr)=split(/\s+/,$configline);
2696: if ($descr[0] ne '') {
2697: $fe{$ending}=$emb;
2698: $fd{$ending}=join(' ',@descr);
2699: }
1.11 www 2700: }
2701: }
2702:
1.71 www 2703: %metacache=();
1.22 www 2704:
1.1 albertel 2705: $readit='done';
1.163 harris41 2706: &logtouch();
1.12 www 2707: &logthis('<font color=yellow>INFO: Read configuration</font>');
1.1 albertel 2708: }
1.179 www 2709:
1.1 albertel 2710: 1;
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>