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