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