Annotation of loncom/lonnet/perl/lonnet.pm, revision 1.126
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.126 ! www 905: $ENV{'user.adv'}=$adv;
1.11 www 906: }
907: return $userroles;
908: }
909:
1.12 www 910: # --------------------------------------------------------------- get interface
911:
912: sub get {
913: my ($namespace,@storearr)=@_;
914: my $items='';
915: map {
916: $items.=escape($_).'&';
917: } @storearr;
918: $items=~s/\&$//;
919: my $rep=reply("get:$ENV{'user.domain'}:$ENV{'user.name'}:$namespace:$items",
920: $ENV{'user.home'});
1.15 www 921: my @pairs=split(/\&/,$rep);
922: my %returnhash=();
1.42 www 923: my $i=0;
1.15 www 924: map {
1.42 www 925: $returnhash{$_}=unescape($pairs[$i]);
926: $i++;
927: } @storearr;
1.15 www 928: return %returnhash;
1.27 www 929: }
930:
931: # --------------------------------------------------------------- del interface
932:
933: sub del {
934: my ($namespace,@storearr)=@_;
935: my $items='';
936: map {
937: $items.=escape($_).'&';
938: } @storearr;
939: $items=~s/\&$//;
940: return reply("del:$ENV{'user.domain'}:$ENV{'user.name'}:$namespace:$items",
941: $ENV{'user.home'});
1.15 www 942: }
943:
944: # -------------------------------------------------------------- dump interface
945:
946: sub dump {
947: my $namespace=shift;
948: my $rep=reply("dump:$ENV{'user.domain'}:$ENV{'user.name'}:$namespace",
949: $ENV{'user.home'});
1.12 www 950: my @pairs=split(/\&/,$rep);
951: my %returnhash=();
952: map {
953: my ($key,$value)=split(/=/,$_);
1.29 www 954: $returnhash{unescape($key)}=unescape($value);
1.12 www 955: } @pairs;
956: return %returnhash;
957: }
958:
959: # --------------------------------------------------------------- put interface
960:
961: sub put {
962: my ($namespace,%storehash)=@_;
963: my $items='';
964: map {
965: $items.=escape($_).'='.escape($storehash{$_}).'&';
966: } keys %storehash;
967: $items=~s/\&$//;
968: return reply("put:$ENV{'user.domain'}:$ENV{'user.name'}:$namespace:$items",
1.47 www 969: $ENV{'user.home'});
970: }
971:
972: # ------------------------------------------------------ critical put interface
973:
974: sub cput {
975: my ($namespace,%storehash)=@_;
976: my $items='';
977: map {
978: $items.=escape($_).'='.escape($storehash{$_}).'&';
979: } keys %storehash;
980: $items=~s/\&$//;
981: return critical
982: ("put:$ENV{'user.domain'}:$ENV{'user.name'}:$namespace:$items",
1.12 www 983: $ENV{'user.home'});
984: }
985:
986: # -------------------------------------------------------------- eget interface
987:
988: sub eget {
989: my ($namespace,@storearr)=@_;
990: my $items='';
991: map {
992: $items.=escape($_).'&';
993: } @storearr;
994: $items=~s/\&$//;
995: my $rep=reply("eget:$ENV{'user.domain'}:$ENV{'user.name'}:$namespace:$items",
996: $ENV{'user.home'});
997: my @pairs=split(/\&/,$rep);
998: my %returnhash=();
1.42 www 999: my $i=0;
1.12 www 1000: map {
1.42 www 1001: $returnhash{$_}=unescape($pairs[$i]);
1002: $i++;
1003: } @storearr;
1.12 www 1004: return %returnhash;
1005: }
1006:
1.103 harris41 1007: # ------------------------------------------------- Check for a user privilege
1.12 www 1008:
1009: sub allowed {
1010: my ($priv,$uri)=@_;
1.52 www 1011: $uri=&declutter($uri);
1.29 www 1012:
1.54 www 1013: # Free bre access to adm and meta resources
1.29 www 1014:
1.54 www 1015: if ((($uri=~/^adm\//) || ($uri=~/\.meta$/)) && ($priv eq 'bre')) {
1.14 www 1016: return 'F';
1017: }
1.29 www 1018:
1.52 www 1019: my $thisallowed='';
1020: my $statecond=0;
1021: my $courseprivid='';
1022:
1023: # Course
1024:
1025: if ($ENV{'user.priv.'.$ENV{'request.role'}.'./'}=~/$priv\&([^\:]*)/) {
1026: $thisallowed.=$1;
1027: }
1.29 www 1028:
1.52 www 1029: # Domain
1030:
1031: if ($ENV{'user.priv.'.$ENV{'request.role'}.'./'.(split(/\//,$uri))[0].'/'}
1032: =~/$priv\&([^\:]*)/) {
1.12 www 1033: $thisallowed.=$1;
1034: }
1.52 www 1035:
1036: # Course: uri itself is a course
1.66 www 1037: my $courseuri=$uri;
1038: $courseuri=~s/\_(\d)/\/$1/;
1.83 www 1039: $courseuri=~s/^([^\/])/\/$1/;
1.81 www 1040:
1.83 www 1041: if ($ENV{'user.priv.'.$ENV{'request.role'}.'.'.$courseuri}
1.52 www 1042: =~/$priv\&([^\:]*)/) {
1.12 www 1043: $thisallowed.=$1;
1044: }
1.29 www 1045:
1.52 www 1046: # Full access at system, domain or course-wide level? Exit.
1.29 www 1047:
1048: if ($thisallowed=~/F/) {
1049: return 'F';
1050: }
1051:
1.52 www 1052: # If this is generating or modifying users, exit with special codes
1.29 www 1053:
1.52 www 1054: if (':csu:cdc:ccc:cin:cta:cep:ccr:cst:cad:cli:cau:cdg:'=~/\:$priv\:/) {
1055: return $thisallowed;
1056: }
1057: #
1.103 harris41 1058: # Gathered so far: system, domain and course wide privileges
1.52 www 1059: #
1060: # Course: See if uri or referer is an individual resource that is part of
1061: # the course
1062:
1063: if ($ENV{'request.course.id'}) {
1064: $courseprivid=$ENV{'request.course.id'};
1065: if ($ENV{'request.course.sec'}) {
1066: $courseprivid.='/'.$ENV{'request.course.sec'};
1067: }
1068: $courseprivid=~s/\_/\//;
1069: my $checkreferer=1;
1070: my @uriparts=split(/\//,$uri);
1071: my $filename=$uriparts[$#uriparts];
1072: my $pathname=$uri;
1073: $pathname=~s/\/$filename$//;
1074: if ($ENV{'acc.res.'.$ENV{'request.course.id'}.'.'.$pathname}=~
1.54 www 1075: /\&$filename\:([\d\|]+)\&/) {
1.52 www 1076: $statecond=$1;
1077: if ($ENV{'user.priv.'.$ENV{'request.role'}.'./'.$courseprivid}
1078: =~/$priv\&([^\:]*)/) {
1079: $thisallowed.=$1;
1080: $checkreferer=0;
1081: }
1.29 www 1082: }
1.83 www 1083:
1.52 www 1084: if (($ENV{'HTTP_REFERER'}) && ($checkreferer)) {
1.55 www 1085: my $refuri=$ENV{'HTTP_REFERER'};
1086: $refuri=~s/^http\:\/\/$ENV{'request.host'}//i;
1087: $refuri=&declutter($refuri);
1.53 www 1088: my @uriparts=split(/\//,$refuri);
1.52 www 1089: my $filename=$uriparts[$#uriparts];
1.53 www 1090: my $pathname=$refuri;
1.52 www 1091: $pathname=~s/\/$filename$//;
1.55 www 1092: my @filenameparts=split(/\./,$uri);
1.53 www 1093: if (&fileembstyle($filenameparts[$#filenameparts]) ne 'ssi') {
1094: if ($ENV{'acc.res.'.$ENV{'request.course.id'}.'.'.$pathname}=~
1.54 www 1095: /\&$filename\:([\d\|]+)\&/) {
1.53 www 1096: my $refstatecond=$1;
1.52 www 1097: if ($ENV{'user.priv.'.$ENV{'request.role'}.'./'.$courseprivid}
1098: =~/$priv\&([^\:]*)/) {
1099: $thisallowed.=$1;
1.53 www 1100: $uri=$refuri;
1101: $statecond=$refstatecond;
1.52 www 1102: }
1.53 www 1103: }
1.52 www 1104: }
1.29 www 1105: }
1.52 www 1106: }
1.29 www 1107:
1.52 www 1108: #
1.103 harris41 1109: # Gathered now: all privileges that could apply, and condition number
1.52 www 1110: #
1111: #
1112: # Full or no access?
1113: #
1.29 www 1114:
1.52 www 1115: if ($thisallowed=~/F/) {
1116: return 'F';
1117: }
1.29 www 1118:
1.52 www 1119: unless ($thisallowed) {
1120: return '';
1121: }
1.29 www 1122:
1.52 www 1123: # Restrictions exist, deal with them
1124: #
1125: # C:according to course preferences
1126: # R:according to resource settings
1127: # L:unless locked
1128: # X:according to user session state
1129: #
1130:
1131: # Possibly locked functionality, check all courses
1.54 www 1132: # Locks might take effect only after 10 minutes cache expiration for other
1133: # courses, and 2 minutes for current course
1.52 www 1134:
1135: my $envkey;
1136: if ($thisallowed=~/L/) {
1137: foreach $envkey (keys %ENV) {
1.54 www 1138: if ($envkey=~/^user\.role\.(st|ta)\.([^\.]*)/) {
1139: my $courseid=$2;
1140: my $roleid=$1.'.'.$2;
1.92 www 1141: $courseid=~s/^\///;
1.54 www 1142: my $expiretime=600;
1143: if ($ENV{'request.role'} eq $roleid) {
1144: $expiretime=120;
1145: }
1146: my ($cdom,$cnum,$csec)=split(/\//,$courseid);
1147: my $prefix='course.'.$cdom.'_'.$cnum.'.';
1148: if ((time-$ENV{$prefix.'last_cache'})>$expiretime) {
1149: &coursedescription($courseid);
1150: }
1151: if (($ENV{$prefix.'res.'.$uri.'.lock.sections'}=~/\,$csec\,/)
1152: || ($ENV{$prefix.'res.'.$uri.'.lock.sections'} eq 'all')) {
1153: if ($ENV{$prefix.'res.'.$uri.'.lock.expire'}>time) {
1.57 www 1154: &log($ENV{'user.domain'},$ENV{'user.name'},
1155: $ENV{'user.host'},
1156: 'Locked by res: '.$priv.' for '.$uri.' due to '.
1.52 www 1157: $cdom.'/'.$cnum.'/'.$csec.' expire '.
1.54 www 1158: $ENV{$prefix.'priv.'.$priv.'.lock.expire'});
1.52 www 1159: return '';
1160: }
1161: }
1.54 www 1162: if (($ENV{$prefix.'priv.'.$priv.'.lock.sections'}=~/\,$csec\,/)
1163: || ($ENV{$prefix.'priv.'.$priv.'.lock.sections'} eq 'all')) {
1164: if ($ENV{'priv.'.$priv.'.lock.expire'}>time) {
1.57 www 1165: &log($ENV{'user.domain'},$ENV{'user.name'},
1166: $ENV{'user.host'},
1167: 'Locked by priv: '.$priv.' for '.$uri.' due to '.
1.52 www 1168: $cdom.'/'.$cnum.'/'.$csec.' expire '.
1.54 www 1169: $ENV{$prefix.'priv.'.$priv.'.lock.expire'});
1.52 www 1170: return '';
1171: }
1172: }
1173: }
1.29 www 1174: }
1.52 www 1175: }
1176:
1177: #
1178: # Rest of the restrictions depend on selected course
1179: #
1180:
1181: unless ($ENV{'request.course.id'}) {
1182: return '1';
1183: }
1.29 www 1184:
1.52 www 1185: #
1186: # Now user is definitely in a course
1187: #
1.53 www 1188:
1189:
1190: # Course preferences
1191:
1192: if ($thisallowed=~/C/) {
1.54 www 1193: my $rolecode=(split(/\./,$ENV{'request.role'}))[0];
1194: if ($ENV{'course.'.$ENV{'request.course.id'}.'.'.$priv.'.roles.denied'}
1195: =~/\,$rolecode\,/) {
1.57 www 1196: &log($ENV{'user.domain'},$ENV{'user.name'},$ENV{'user.host'},
1197: 'Denied by role: '.$priv.' for '.$uri.' as '.$rolecode.' in '.
1.54 www 1198: $ENV{'request.course.id'});
1199: return '';
1200: }
1.53 www 1201: }
1202:
1203: # Resource preferences
1204:
1205: if ($thisallowed=~/R/) {
1.54 www 1206: my $rolecode=(split(/\./,$ENV{'request.role'}))[0];
1207: my $filename=$perlvar{'lonDocRoot'}.'/res/'.$uri.'.meta';
1208: if (-e $filename) {
1209: my @content;
1210: {
1211: my $fh=Apache::File->new($filename);
1212: @content=<$fh>;
1213: }
1214: if (join('',@content)=~
1215: /\<roledeny[^\>]*\>[^\<]*$rolecode[^\<]*\<\/roledeny\>/) {
1.57 www 1216: &log($ENV{'user.domain'},$ENV{'user.name'},$ENV{'user.host'},
1217: 'Denied by role: '.$priv.' for '.$uri.' as '.$rolecode);
1.54 www 1218: return '';
1219:
1220: }
1221: }
1.53 www 1222: }
1.30 www 1223:
1.52 www 1224: # Restricted by state?
1.30 www 1225:
1.52 www 1226: if ($thisallowed=~/X/) {
1227: if (&condval($statecond)) {
1228: return '2';
1229: } else {
1230: return '';
1231: }
1232: }
1.30 www 1233:
1.52 www 1234: return 'F';
1.12 www 1235: }
1236:
1237: # ----------------------------------------------------------------- Define Role
1238:
1239: sub definerole {
1240: if (allowed('mcr','/')) {
1241: my ($rolename,$sysrole,$domrole,$courole)=@_;
1.21 www 1242: map {
1243: my ($crole,$cqual)=split(/\&/,$_);
1244: if ($pr{'cr:s'}!~/$crole/) { return "refused:s:$crole"; }
1245: if ($pr{'cr:s'}=~/$crole\&/) {
1246: if ($pr{'cr:s'}!~/$crole\&\w*$cqual/) {
1247: return "refused:s:$crole&$cqual";
1248: }
1249: }
1250: } split('/',$sysrole);
1251: map {
1252: my ($crole,$cqual)=split(/\&/,$_);
1253: if ($pr{'cr:d'}!~/$crole/) { return "refused:d:$crole"; }
1254: if ($pr{'cr:d'}=~/$crole\&/) {
1255: if ($pr{'cr:d'}!~/$crole\&\w*$cqual/) {
1256: return "refused:d:$crole&$cqual";
1257: }
1258: }
1259: } split('/',$domrole);
1260: map {
1261: my ($crole,$cqual)=split(/\&/,$_);
1262: if ($pr{'cr:c'}!~/$crole/) { return "refused:c:$crole"; }
1263: if ($pr{'cr:c'}=~/$crole\&/) {
1264: if ($pr{'cr:c'}!~/$crole\&\w*$cqual/) {
1265: return "refused:c:$crole&$cqual";
1266: }
1267: }
1268: } split('/',$courole);
1.12 www 1269: my $command="encrypt:rolesput:$ENV{'user.domain'}:$ENV{'user.name'}:".
1270: "$ENV{'user.domain'}:$ENV{'user.name'}:".
1.21 www 1271: "rolesdef_$rolename=".
1272: escape($sysrole.'_'.$domrole.'_'.$courole);
1.12 www 1273: return reply($command,$ENV{'user.home'});
1274: } else {
1275: return 'refused';
1276: }
1.105 harris41 1277: }
1278:
1279: # ---------------- Make a metadata query against the network of library servers
1280:
1281: sub metadata_query {
1.116 harris41 1282: my ($query,$custom,$customshow)=@_;
1.118 harris41 1283: # need to put in a library server loop here and return a hash
1.120 harris41 1284: my %rhash;
1.123 harris41 1285: for my $server (keys %libserv) {
1.118 harris41 1286: unless ($custom or $customshow) {
1287: my $reply=&reply("querysend:".&escape($query),$server);
1288: $rhash{$server}=$reply;
1289: }
1290: else {
1291: my $reply=&reply("querysend:".&escape($query).':'.
1292: &escape($custom).':'.&escape($customshow),
1293: $server);
1294: $rhash{$server}=$reply;
1295: }
1.112 harris41 1296: }
1.118 harris41 1297: return \%rhash;
1.12 www 1298: }
1299:
1300: # ------------------------------------------------------------------ Plain Text
1301:
1302: sub plaintext {
1.22 www 1303: my $short=shift;
1304: return $prp{$short};
1.12 www 1305: }
1306:
1.25 www 1307: # ------------------------------------------------------------------ Plain Text
1308:
1309: sub fileembstyle {
1310: my $ending=shift;
1311: return $fe{$ending};
1312: }
1313:
1314: # ------------------------------------------------------------ Description Text
1315:
1.74 www 1316: sub filedescription {
1.25 www 1317: my $ending=shift;
1318: return $fd{$ending};
1319: }
1320:
1.12 www 1321: # ----------------------------------------------------------------- Assign Role
1322:
1323: sub assignrole {
1.21 www 1324: my ($udom,$uname,$url,$role,$end,$start)=@_;
1325: my $mrole;
1326: if ($role =~ /^cr\//) {
1.104 www 1327: unless (&allowed('ccr',$url)) {
1328: &logthis('Refused custom assignrole: '.
1329: $udom.' '.$uname.' '.$url.' '.$role.' '.$end.' '.$start.' by '.
1330: $ENV{'user.name'}.' at '.$ENV{'user.domain'});
1331: return 'refused';
1332: }
1.21 www 1333: $mrole='cr';
1334: } else {
1.82 www 1335: my $cwosec=$url;
1.83 www 1336: $cwosec=~s/^\/(\w+)\/(\w+)\/.*/$1\/$2/;
1.104 www 1337: unless (&allowed('c'.$role,$cwosec)) {
1338: &logthis('Refused assignrole: '.
1339: $udom.' '.$uname.' '.$url.' '.$role.' '.$end.' '.$start.' by '.
1340: $ENV{'user.name'}.' at '.$ENV{'user.domain'});
1341: return 'refused';
1342: }
1.21 www 1343: $mrole=$role;
1344: }
1345: my $command="encrypt:rolesput:$ENV{'user.domain'}:$ENV{'user.name'}:".
1346: "$udom:$uname:$url".'_'."$mrole=$role";
1.81 www 1347: if ($end) { $command.='_'.$end; }
1.21 www 1348: if ($start) {
1349: if ($end) {
1.81 www 1350: $command.='_'.$start;
1.21 www 1351: } else {
1.81 www 1352: $command.='_0_'.$start;
1.21 www 1353: }
1354: }
1355: return &reply($command,&homeserver($uname,$udom));
1.80 www 1356: }
1357:
1.81 www 1358: # --------------------------------------------------------------- Modify a user
1.80 www 1359:
1360:
1.81 www 1361: sub modifyuser {
1.80 www 1362: my ($udom,$uname,$uid,$umode,$upass,$first,$middle,$last,$gene)=@_;
1.81 www 1363: &logthis('Call to modify user '.$udom.', '.$uname.', '.$uid.', '.
1.80 www 1364: $umode.', '.$first.', '.$middle.', '.
1365: $last.', '.$gene.' by '.
1366: $ENV{'user.name'}.' at '.$ENV{'user.domain'});
1367: my $uhome=&homeserver($uname,$udom);
1368: # ----------------------------------------------------------------- Create User
1.81 www 1369: if (($uhome eq 'no_host') && ($umode) && ($upass)) {
1.80 www 1370: my $unhome='';
1371: if ($ENV{'course.'.$ENV{'request.course.id'}.'.domain'} eq $udom) {
1372: $unhome=$ENV{'course.'.$ENV{'request.course.id'}.'.home'};
1373: } else {
1374: my $tryserver;
1.81 www 1375: my $loadm=10000000;
1.80 www 1376: foreach $tryserver (keys %libserv) {
1377: if ($hostdom{$tryserver} eq $udom) {
1378: my $answer=reply('load',$tryserver);
1379: if (($answer=~/\d+/) && ($answer<$loadm)) {
1380: $loadm=$answer;
1381: $unhome=$tryserver;
1382: }
1383: }
1384: }
1385: }
1386: if (($unhome eq '') || ($unhome eq 'no_host')) {
1387: return 'error: find home';
1388: }
1389: my $reply=&reply('encrypt:makeuser:'.$udom.':'.$uname.':'.$umode.':'.
1390: &escape($upass),$unhome);
1391: unless ($reply eq 'ok') {
1392: return 'error: '.$reply;
1393: }
1394: $uhome=&homeserver($uname,$udom);
1395: if (($uhome eq '') || ($uhome eq 'no_host') || ($uhome ne $unhome)) {
1396: return 'error: verify home';
1397: }
1398: }
1399: # ---------------------------------------------------------------------- Add ID
1400: if ($uid) {
1401: $uid=~tr/A-Z/a-z/;
1402: my %uidhash=&idrget($udom,$uname);
1403: if (($uidhash{$uname}) && ($uidhash{$uname}!~/error\:/)) {
1404: unless ($uid eq $uidhash{$uname}) {
1405: return 'error: mismatch '.$uidhash{$uname}.' versus '.$uid;
1406: }
1407: } else {
1408: &idput($udom,($uname => $uid));
1409: }
1410: }
1411: # -------------------------------------------------------------- Add names, etc
1412: my $names=&reply('get:'.$udom.':'.$uname.
1413: ':environment:firstname&middlename&lastname&generation',
1414: $uhome);
1415: my ($efirst,$emiddle,$elast,$egene)=split(/\&/,$names);
1.81 www 1416: if ($first) { $efirst = &escape($first); }
1417: if ($middle) { $emiddle = &escape($middle); }
1418: if ($last) { $elast = &escape($last); }
1419: if ($gene) { $egene = &escape($gene); }
1.80 www 1420: my $reply=&reply('put:'.$udom.':'.$uname.
1421: ':environment:firstname='.$efirst.
1422: '&middlename='.$emiddle.
1423: '&lastname='.$elast.
1424: '&generation='.$egene,$uhome);
1425: if ($reply ne 'ok') {
1426: return 'error: '.$reply;
1427: }
1.81 www 1428: &logthis('Success modifying user '.$udom.', '.$uname.', '.$uid.', '.
1.80 www 1429: $umode.', '.$first.', '.$middle.', '.
1430: $last.', '.$gene.' by '.
1431: $ENV{'user.name'}.' at '.$ENV{'user.domain'});
1432: return 'ok';
1433: }
1434:
1.81 www 1435: # -------------------------------------------------------------- Modify student
1.80 www 1436:
1.81 www 1437: sub modifystudent {
1438: my ($udom,$uname,$uid,$umode,$upass,$first,$middle,$last,$gene,$usec,
1439: $end,$start)=@_;
1440: my $cid='';
1441: unless ($cid=$ENV{'request.course.id'}) {
1.80 www 1442: return 'not_in_class';
1443: }
1444: # --------------------------------------------------------------- Make the user
1.81 www 1445: my $reply=&modifyuser
1.80 www 1446: ($udom,$uname,$uid,$umode,$upass,$first,$middle,$last,$gene);
1447: unless ($reply eq 'ok') { return $reply; }
1.81 www 1448: my $uhome=&homeserver($uname,$udom);
1449: if (($uhome eq '') || ($uhome eq 'no_host')) {
1450: return 'error: no such user';
1451: }
1.80 www 1452: # -------------------------------------------------- Add student to course list
1.81 www 1453: my $reply=critical('put:'.$ENV{'course.'.$cid.'.domain'}.':'.
1454: $ENV{'course.'.$cid.'.num'}.':classlist:'.
1455: &escape($uname.':'.$udom).'='.
1456: &escape($end.':'.$start),
1457: $ENV{'course.'.$cid.'.home'});
1458: unless (($reply eq 'ok') || ($reply eq 'delayed')) {
1459: return 'error: '.$reply;
1460: }
1.80 www 1461: # ---------------------------------------------------- Add student role to user
1.83 www 1462: my $uurl='/'.$cid;
1.81 www 1463: $uurl=~s/\_/\//g;
1464: if ($usec) {
1465: $uurl.='/'.$usec;
1466: }
1467: return &assignrole($udom,$uname,$uurl,'st',$end,$start);
1.21 www 1468: }
1469:
1.84 www 1470: # ------------------------------------------------- Write to course preferences
1471:
1472: sub writecoursepref {
1473: my ($courseid,%prefs)=@_;
1474: $courseid=~s/^\///;
1475: $courseid=~s/\_/\//g;
1476: my ($cdomain,$cnum)=split(/\//,$courseid);
1477: my $chome=homeserver($cnum,$cdomain);
1478: if (($chome eq '') || ($chome eq 'no_host')) {
1479: return 'error: no such course';
1480: }
1481: my $cstring='';
1482: map {
1483: $cstring.=escape($_).'='.escape($prefs{$_}).'&';
1484: } keys %prefs;
1485: $cstring=~s/\&$//;
1486: return reply('put:'.$cdomain.':'.$cnum.':environment:'.$cstring,$chome);
1487: }
1488:
1489: # ---------------------------------------------------------- Make/modify course
1490:
1491: sub createcourse {
1492: my ($udom,$description,$url)=@_;
1493: $url=&declutter($url);
1494: my $cid='';
1495: unless (&allowed('ccc',$ENV{'user.domain'})) {
1496: return 'refused';
1497: }
1498: unless ($udom eq $ENV{'user.domain'}) {
1499: return 'refused';
1500: }
1501: # ------------------------------------------------------------------- Create ID
1502: my $uname=substr($$.time,0,5).unpack("H8",pack("I32",time)).
1503: unpack("H2",pack("I32",int(rand(255)))).$perlvar{'lonHostID'};
1504: # ----------------------------------------------- Make sure that does not exist
1505: my $uhome=&homeserver($uname,$udom);
1506: unless (($uhome eq '') || ($uhome eq 'no_host')) {
1507: $uname=substr($$.time,0,5).unpack("H8",pack("I32",time)).
1508: unpack("H2",pack("I32",int(rand(255)))).$perlvar{'lonHostID'};
1509: $uhome=&homeserver($uname,$udom);
1510: unless (($uhome eq '') || ($uhome eq 'no_host')) {
1511: return 'error: unable to generate unique course-ID';
1512: }
1513: }
1514: # ------------------------------------------------------------- Make the course
1515: my $reply=&reply('encrypt:makeuser:'.$udom.':'.$uname.':none::',
1516: $ENV{'user.home'});
1517: unless ($reply eq 'ok') { return 'error: '.$reply; }
1518: my $uhome=&homeserver($uname,$udom);
1519: if (($uhome eq '') || ($uhome eq 'no_host')) {
1520: return 'error: no such course';
1521: }
1522: &writecoursepref($udom.'_'.$uname,
1523: ('description' => $description,
1524: 'url' => $url));
1525: return '/'.$udom.'/'.$uname;
1526: }
1527:
1.21 www 1528: # ---------------------------------------------------------- Assign Custom Role
1529:
1530: sub assigncustomrole {
1531: my ($udom,$uname,$url,$rdom,$rnam,$rolename,$end,$start)=@_;
1532: return &assignrole($udom,$uname,$url,'cr/'.$rdom.'/'.$rnam.'/'.$rolename,
1533: $end,$start);
1534: }
1535:
1536: # ----------------------------------------------------------------- Revoke Role
1537:
1538: sub revokerole {
1539: my ($udom,$uname,$url,$role)=@_;
1540: my $now=time;
1541: return &assignrole($udom,$uname,$url,$role,$now);
1542: }
1543:
1544: # ---------------------------------------------------------- Revoke Custom Role
1545:
1546: sub revokecustomrole {
1547: my ($udom,$uname,$url,$rdom,$rnam,$rolename)=@_;
1548: my $now=time;
1549: return &assigncustomrole($udom,$uname,$url,$rdom,$rnam,$rolename,$now);
1.17 www 1550: }
1551:
1552: # ------------------------------------------------------------ Directory lister
1553:
1554: sub dirlist {
1555: my $uri=shift;
1.18 www 1556: $uri=~s/^\///;
1557: $uri=~s/\/$//;
1.19 www 1558: my ($res,$udom,$uname,@rest)=split(/\//,$uri);
1559: if ($udom) {
1560: if ($uname) {
1561: my $listing=reply('ls:'.$perlvar{'lonDocRoot'}.'/'.$uri,
1562: homeserver($uname,$udom));
1563: return split(/:/,$listing);
1564: } else {
1565: my $tryserver;
1566: my %allusers=();
1567: foreach $tryserver (keys %libserv) {
1568: if ($hostdom{$tryserver} eq $udom) {
1569: my $listing=reply('ls:'.$perlvar{'lonDocRoot'}.'/res/'.$udom,
1570: $tryserver);
1571: if (($listing ne 'no_such_dir') && ($listing ne 'empty')
1572: && ($listing ne 'con_lost')) {
1573: map {
1574: my ($entry,@stat)=split(/&/,$_);
1575: $allusers{$entry}=1;
1576: } split(/:/,$listing);
1577: }
1578: }
1579: }
1580: my $alluserstr='';
1581: map {
1582: $alluserstr.=$_.'&user:';
1583: } sort keys %allusers;
1584: $alluserstr=~s/:$//;
1585: return split(/:/,$alluserstr);
1586: }
1587: } else {
1588: my $tryserver;
1589: my %alldom=();
1590: foreach $tryserver (keys %libserv) {
1591: $alldom{$hostdom{$tryserver}}=1;
1592: }
1593: my $alldomstr='';
1594: map {
1595: $alldomstr.=$perlvar{'lonDocRoot'}.'/res/'.$_.'&domain:';
1596: } sort keys %alldom;
1597: $alldomstr=~s/:$//;
1598: return split(/:/,$alldomstr);
1599: }
1.26 www 1600: }
1601:
1602: # -------------------------------------------------------- Value of a Condition
1603:
1.40 www 1604: sub directcondval {
1605: my $number=shift;
1606: if ($ENV{'user.state.'.$ENV{'request.course.id'}}) {
1607: return substr($ENV{'user.state.'.$ENV{'request.course.id'}},$number,1);
1608: } else {
1609: return 2;
1610: }
1611: }
1612:
1.26 www 1613: sub condval {
1614: my $condidx=shift;
1615: my $result=0;
1.54 www 1616: my $allpathcond='';
1617: map {
1618: if (defined($ENV{'acc.cond.'.$ENV{'request.course.id'}.'.'.$_})) {
1619: $allpathcond.=
1620: '('.$ENV{'acc.cond.'.$ENV{'request.course.id'}.'.'.$_}.')|';
1621: }
1622: } split(/\|/,$condidx);
1623: $allpathcond=~s/\|$//;
1.33 www 1624: if ($ENV{'request.course.id'}) {
1.54 www 1625: if ($allpathcond) {
1.26 www 1626: my $operand='|';
1627: my @stack;
1628: map {
1629: if ($_ eq '(') {
1630: push @stack,($operand,$result)
1631: } elsif ($_ eq ')') {
1632: my $before=pop @stack;
1633: if (pop @stack eq '&') {
1634: $result=$result>$before?$before:$result;
1635: } else {
1636: $result=$result>$before?$result:$before;
1637: }
1638: } elsif (($_ eq '&') || ($_ eq '|')) {
1639: $operand=$_;
1640: } else {
1.40 www 1641: my $new=directcondval($_);
1.26 www 1642: if ($operand eq '&') {
1643: $result=$result>$new?$new:$result;
1644: } else {
1645: $result=$result>$new?$result:$new;
1646: }
1647: }
1.54 www 1648: } ($allpathcond=~/(\d+|\(|\)|\&|\|)/g);
1.26 www 1649: }
1650: }
1651: return $result;
1.28 www 1652: }
1653:
1654: # --------------------------------------------------------- Value of a Variable
1655:
1.58 www 1656: sub EXT {
1.102 www 1657: my $varname=shift;
1.68 www 1658: unless ($varname) { return ''; }
1.48 www 1659: my ($realm,$space,$qualifier,@therest)=split(/\./,$varname);
1660: my $rest;
1661: if ($therest[0]) {
1662: $rest=join('.',@therest);
1663: } else {
1664: $rest='';
1665: }
1.57 www 1666: my $qualifierrest=$qualifier;
1667: if ($rest) { $qualifierrest.='.'.$rest; }
1668: my $spacequalifierrest=$space;
1669: if ($qualifierrest) { $spacequalifierrest.='.'.$qualifierrest; }
1.28 www 1670: if ($realm eq 'user') {
1.48 www 1671: # --------------------------------------------------------------- user.resource
1672: if ($space eq 'resource') {
1.122 albertel 1673: my %restored=&restore();
1.57 www 1674: return $restored{$qualifierrest};
1.48 www 1675: # ----------------------------------------------------------------- user.access
1676: } elsif ($space eq 'access') {
1677: return &allowed($qualifier,$rest);
1678: # ------------------------------------------ user.preferences, user.environment
1679: } elsif (($space eq 'preferences') || ($space eq 'environment')) {
1.57 www 1680: return $ENV{join('.',('environment',$qualifierrest))};
1.48 www 1681: # ----------------------------------------------------------------- user.course
1682: } elsif ($space eq 'course') {
1683: return $ENV{join('.',('request.course',$qualifier))};
1684: # ------------------------------------------------------------------- user.role
1685: } elsif ($space eq 'role') {
1686: my ($role,$where)=split(/\./,$ENV{'request.role'});
1687: if ($qualifier eq 'value') {
1688: return $role;
1689: } elsif ($qualifier eq 'extent') {
1690: return $where;
1691: }
1692: # ----------------------------------------------------------------- user.domain
1693: } elsif ($space eq 'domain') {
1694: return $ENV{'user.domain'};
1695: # ------------------------------------------------------------------- user.name
1696: } elsif ($space eq 'name') {
1697: return $ENV{'user.name'};
1698: # ---------------------------------------------------- Any other user namespace
1.29 www 1699: } else {
1.48 www 1700: my $item=($rest)?$qualifier.'.'.$rest:$qualifier;
1701: my %reply=&get($space,$item);
1702: return $reply{$item};
1703: }
1704: } elsif ($realm eq 'request') {
1705: # ------------------------------------------------------------- request.browser
1706: if ($space eq 'browser') {
1707: return $ENV{'browser.'.$qualifier};
1.57 www 1708: # ------------------------------------------------------------ request.filename
1709: } else {
1710: return $ENV{'request.'.$spacequalifierrest};
1.29 www 1711: }
1.28 www 1712: } elsif ($realm eq 'course') {
1.48 www 1713: # ---------------------------------------------------------- course.description
1.57 www 1714: my $section='';
1715: if ($ENV{'request.course.sec'}) {
1716: $section='_'.$ENV{'request.course.sec'};
1.48 www 1717: }
1.57 www 1718: return $ENV{'course.'.$ENV{'request.course.id'}.$section.'.'.
1719: $spacequalifierrest};
1720: } elsif ($realm eq 'resource') {
1.60 www 1721: if ($ENV{'request.course.id'}) {
1722: # ----------------------------------------------------- Cascading lookup scheme
1.102 www 1723: my $symbp=&symbread();
1.69 www 1724: my $mapp=(split(/\_\_\_/,$symbp))[0];
1725:
1726: my $symbparm=$symbp.'.'.$spacequalifierrest;
1727: my $mapparm=$mapp.'___(all).'.$spacequalifierrest;
1728:
1.60 www 1729: my $seclevel=
1.69 www 1730: $ENV{'request.course.id'}.'.['.
1731: $ENV{'request.course.sec'}.'].'.$spacequalifierrest;
1732: my $seclevelr=
1733: $ENV{'request.course.id'}.'.['.
1734: $ENV{'request.course.sec'}.'].'.$symbparm;
1735: my $seclevelm=
1736: $ENV{'request.course.id'}.'.['.
1737: $ENV{'request.course.sec'}.'].'.$mapparm;
1738:
1.60 www 1739: my $courselevel=
1740: $ENV{'request.course.id'}.'.'.$spacequalifierrest;
1.69 www 1741: my $courselevelr=
1742: $ENV{'request.course.id'}.'.'.$symbparm;
1743: my $courselevelm=
1744: $ENV{'request.course.id'}.'.'.$mapparm;
1745:
1.60 www 1746: # ----------------------------------------------------------- first, check user
1.69 www 1747: my %resourcedata=get('resourcedata',
1748: ($courselevelr,$courselevelm,$courselevel));
1.94 www 1749: if (($resourcedata{$courselevelr}!~/^error\:/) &&
1750: ($resourcedata{$courselevelr}!~/^con_lost/)) {
1.69 www 1751:
1752: if ($resourcedata{$courselevelr}) {
1753: return $resourcedata{$courselevelr}; }
1754: if ($resourcedata{$courselevelm}) {
1755: return $resourcedata{$courselevelm}; }
1.60 www 1756: if ($resourcedata{$courselevel}) { return $resourcedata{$courselevel}; }
1.69 www 1757:
1.94 www 1758: } else {
1759: if ($resourcedata{$courselevelr}!~/No such file/) {
1760: &logthis("<font color=blue>WARNING:".
1761: " Trying to get resource data for ".$ENV{'user.name'}." at "
1762: .$ENV{'user.domain'}.": ".$resourcedata{$courselevelr}.
1763: "</font>");
1764: }
1.63 www 1765: }
1.95 www 1766:
1.60 www 1767: # -------------------------------------------------------- second, check course
1.96 www 1768:
1.60 www 1769: my $reply=&reply('get:'.
1.96 www 1770: $ENV{'course.'.$ENV{'request.course.id'}.'.domain'}.':'.
1771: $ENV{'course.'.$ENV{'request.course.id'}.'.num'}.
1.79 www 1772: ':resourcedata:'.
1773: &escape($seclevelr).'&'.&escape($seclevelm).'&'.&escape($seclevel).'&'.
1774: &escape($courselevelr).'&'.&escape($courselevelm).'&'.&escape($courselevel),
1.96 www 1775: $ENV{'course.'.$ENV{'request.course.id'}.'.home'});
1.63 www 1776: if ($reply!~/^error\:/) {
1.79 www 1777: map {
1778: if ($_) { return &unescape($_); }
1779: } split(/\&/,$reply);
1.63 www 1780: }
1.94 www 1781: if (($reply=~/^con_lost/) || ($reply=~/^error\:/)) {
1782: &logthis("<font color=blue>WARNING:".
1.95 www 1783: " Getting ".$reply." asking for ".$varname." for ".
1.96 www 1784: $ENV{'course.'.$ENV{'request.course.id'}.'.num'}.
1.95 www 1785: ' at '.
1.96 www 1786: $ENV{'course.'.$ENV{'request.course.id'}.'.domain'}.
1.95 www 1787: ' from '.
1.96 www 1788: $ENV{'course.'.$ENV{'request.course.id'}.'.home'}.
1.94 www 1789: "</font>");
1790: }
1.60 www 1791: # ------------------------------------------------------ third, check map parms
1.65 www 1792: my %parmhash=();
1793: my $thisparm='';
1794: if (tie(%parmhash,'GDBM_File',
1795: $ENV{'request.course.fn'}.'_parms.db',&GDBM_READER,0640)) {
1796: $thisparm=$parmhash{$symbparm};
1797: untie(%parmhash);
1.60 www 1798: }
1.65 www 1799: if ($thisparm) { return $thisparm; }
1.60 www 1800: }
1801:
1802: # --------------------------------------------- last, look in resource metadata
1.71 www 1803:
1.78 www 1804: $spacequalifierrest=~s/\./\_/;
1.71 www 1805: my $metadata=&metadata($ENV{'request.filename'},$spacequalifierrest);
1806: if ($metadata) { return $metadata; }
1.78 www 1807: $metadata=&metadata($ENV{'request.filename'},
1808: 'parameter_'.$spacequalifierrest);
1809: if ($metadata) { return $metadata; }
1.71 www 1810:
1.48 www 1811: # ---------------------------------------------------- Any other user namespace
1812: } elsif ($realm eq 'environment') {
1813: # ----------------------------------------------------------------- environment
1.57 www 1814: return $ENV{$spacequalifierrest};
1.28 www 1815: } elsif ($realm eq 'system') {
1.48 www 1816: # ----------------------------------------------------------------- system.time
1817: if ($space eq 'time') {
1818: return time;
1819: }
1.28 www 1820: }
1.48 www 1821: return '';
1.61 www 1822: }
1823:
1.71 www 1824: # ---------------------------------------------------------------- Get metadata
1825:
1826: sub metadata {
1827: my ($uri,$what)=@_;
1.78 www 1828:
1.71 www 1829: $uri=&declutter($uri);
1.73 www 1830: my $filename=$uri;
1831: $uri=~s/\.meta$//;
1.71 www 1832: unless ($metacache{$uri.':keys'}) {
1.73 www 1833: unless ($filename=~/\.meta$/) { $filename.='.meta'; }
1834: my $metastring=&getfile($perlvar{'lonDocRoot'}.'/res/'.$filename);
1.71 www 1835: my $parser=HTML::TokeParser->new(\$metastring);
1836: my $token;
1837: while ($token=$parser->get_token) {
1838: if ($token->[0] eq 'S') {
1839: my $entry=$token->[1];
1840: my $unikey=$entry;
1.72 www 1841: if (defined($token->[2]->{'part'})) {
1.71 www 1842: $unikey.='_'.$token->[2]->{'part'};
1843: }
1.72 www 1844: if (defined($token->[2]->{'name'})) {
1.71 www 1845: $unikey.='_'.$token->[2]->{'name'};
1846: }
1847: if ($metacache{$uri.':keys'}) {
1848: $metacache{$uri.':keys'}.=','.$unikey;
1849: } else {
1850: $metacache{$uri.':keys'}=$unikey;
1851: }
1852: map {
1853: $metacache{$uri.':'.$unikey.'.'.$_}=$token->[2]->{$_};
1.72 www 1854: } @{$token->[3]};
1.78 www 1855: unless (
1856: $metacache{$uri.':'.$unikey}=$parser->get_text('/'.$entry)
1857: ) { $metacache{$uri.':'.$unikey}=
1858: $metacache{$uri.':'.$unikey.'.default'};
1859: }
1.71 www 1860: }
1861: }
1862: }
1863: return $metacache{$uri.':'.$what};
1864: }
1865:
1.31 www 1866: # ------------------------------------------------- Update symbolic store links
1867:
1868: sub symblist {
1869: my ($mapname,%newhash)=@_;
1870: $mapname=declutter($mapname);
1871: my %hash;
1872: if (($ENV{'request.course.fn'}) && (%newhash)) {
1873: if (tie(%hash,'GDBM_File',$ENV{'request.course.fn'}.'_symb.db',
1874: &GDBM_WRCREAT,0640)) {
1875: map {
1876: $hash{declutter($_)}=$mapname.'___'.$newhash{$_};
1877: } keys %newhash;
1878: if (untie(%hash)) {
1879: return 'ok';
1880: }
1881: }
1882: }
1883: return 'error';
1884: }
1885:
1886: # ------------------------------------------------------ Return symb list entry
1887:
1888: sub symbread {
1.44 www 1889: my $thisfn=shift;
1890: unless ($thisfn) {
1891: $thisfn=$ENV{'request.filename'};
1892: }
1893: $thisfn=declutter($thisfn);
1.31 www 1894: my %hash;
1.37 www 1895: my %bighash;
1896: my $syval='';
1.45 www 1897: if (($ENV{'request.course.fn'}) && ($thisfn)) {
1.31 www 1898: if (tie(%hash,'GDBM_File',$ENV{'request.course.fn'}.'_symb.db',
1899: &GDBM_READER,0640)) {
1900: $syval=$hash{$thisfn};
1.37 www 1901: untie(%hash);
1902: }
1903: # ---------------------------------------------------------- There was an entry
1904: if ($syval) {
1905: unless ($syval=~/\_\d+$/) {
1906: unless ($ENV{'form.request.prefix'}=~/\.(\d+)\_$/) {
1.44 www 1907: &appenv('request.ambiguous' => $thisfn);
1.37 www 1908: return '';
1909: }
1910: $syval.=$1;
1911: }
1912: } else {
1913: # ------------------------------------------------------- Was not in symb table
1914: if (tie(%bighash,'GDBM_File',$ENV{'request.course.fn'}.'.db',
1915: &GDBM_READER,0640)) {
1916: # ---------------------------------------------- Get ID(s) for current resource
1917: my $ids=$bighash{'ids_/res/'.$thisfn};
1.65 www 1918: unless ($ids) {
1919: $ids=$bighash{'ids_/'.$thisfn};
1920: }
1.37 www 1921: if ($ids) {
1922: # ------------------------------------------------------------------- Has ID(s)
1923: my @possibilities=split(/\,/,$ids);
1.39 www 1924: if ($#possibilities==0) {
1925: # ----------------------------------------------- There is only one possibility
1.37 www 1926: my ($mapid,$resid)=split(/\./,$ids);
1927: $syval=declutter($bighash{'map_id_'.$mapid}).'___'.$resid;
1928: } else {
1.39 www 1929: # ------------------------------------------ There is more than one possibility
1930: my $realpossible=0;
1931: map {
1932: my $file=$bighash{'src_'.$_};
1933: if (&allowed('bre',$file)) {
1934: my ($mapid,$resid)=split(/\./,$_);
1935: if ($bighash{'map_type_'.$mapid} ne 'page') {
1936: $realpossible++;
1937: $syval=declutter($bighash{'map_id_'.$mapid}).
1938: '___'.$resid;
1939: }
1940: }
1941: } @possibilities;
1942: if ($realpossible!=1) { $syval=''; }
1.37 www 1943: }
1944: }
1945: untie(%bighash)
1946: }
1.31 www 1947: }
1.62 www 1948: if ($syval) {
1949: return $syval.'___'.$thisfn;
1950: }
1.31 www 1951: }
1.44 www 1952: &appenv('request.ambiguous' => $thisfn);
1.31 www 1953: return '';
1954: }
1955:
1956: # ---------------------------------------------------------- Return random seed
1957:
1.32 www 1958: sub numval {
1959: my $txt=shift;
1960: $txt=~tr/A-J/0-9/;
1961: $txt=~tr/a-j/0-9/;
1962: $txt=~tr/K-T/0-9/;
1963: $txt=~tr/k-t/0-9/;
1964: $txt=~tr/U-Z/0-5/;
1965: $txt=~tr/u-z/0-5/;
1966: $txt=~s/\D//g;
1967: return int($txt);
1968: }
1969:
1.31 www 1970: sub rndseed {
1971: my $symb;
1.44 www 1972: unless ($symb=&symbread()) { return time; }
1.98 albertel 1973: {
1974: use integer;
1975: my $symbchck=unpack("%32C*",$symb) << 27;
1.100 albertel 1976: my $symbseed=numval($symb) << 22;
1.98 albertel 1977: my $namechck=unpack("%32C*",$ENV{'user.name'}) << 17;
1.100 albertel 1978: my $nameseed=numval($ENV{'user.name'}) << 12;
1.98 albertel 1979: my $domainseed=unpack("%32C*",$ENV{'user.domain'}) << 7;
1980: my $courseseed=unpack("%32C*",$ENV{'request.course.id'});
1981: my $num=$symbseed+$nameseed+$domainseed+$courseseed+$namechck+$symbchck;
1.99 albertel 1982: #uncommenting these lines can break things!
1983: #&Apache::lonxml::debug("$symbseed:$nameseed;$domainseed|$courseseed;$namechck:$symbchck");
1984: #&Apache::lonxml::debug("rndseed :$num:$symb");
1.98 albertel 1985: return $num;
1986: }
1.36 albertel 1987: }
1988:
1.76 www 1989: sub ireceipt {
1990: my ($funame,$fudom,$fucourseid,$fusymb)=@_;
1991: my $cuname=unpack("%32C*",$funame);
1992: my $cudom=unpack("%32C*",$fudom);
1993: my $cucourseid=unpack("%32C*",$fucourseid);
1994: my $cusymb=unpack("%32C*",$fusymb);
1.77 www 1995: my $cunique=unpack("%32C*",$perlvar{'lonReceipt'});
1.76 www 1996: return unpack("%32C*",$perlvar{'lonHostID'}).'-'.
1997: ($cunique%$cuname+
1998: $cunique%$cudom+
1999: $cusymb%$cuname+
2000: $cusymb%$cudom+
2001: $cucourseid%$cuname+
2002: $cucourseid%$cudom);
2003: }
2004:
2005: sub receipt {
2006: return &ireceipt($ENV{'user.name'},$ENV{'user.domain'},
2007: $ENV{'request.course.id'},&symbread());
2008: }
2009:
1.36 albertel 2010: # ------------------------------------------------------------ Serves up a file
2011: # returns either the contents of the file or a -1
2012: sub getfile {
2013: my $file=shift;
1.37 www 2014: &repcopy($file);
1.36 albertel 2015: if (! -e $file ) { return -1; };
2016: my $fh=Apache::File->new($file);
2017: my $a='';
2018: while (<$fh>) { $a .=$_; }
2019: return $a
2020: }
2021:
2022: sub filelocation {
2023: my ($dir,$file) = @_;
2024: my $location;
2025: $file=~ s/^\s*(\S+)\s*$/$1/; ## strip off leading and trailing spaces
1.59 albertel 2026: if ($file=~m:^/~:) { # is a contruction space reference
2027: $location = $file;
2028: $location =~ s:/~(.*?)/(.*):/home/$1/public_html/$2:;
1.36 albertel 2029: } else {
1.59 albertel 2030: $file=~s/^$perlvar{'lonDocRoot'}//;
2031: $file=~s:^/*res::;
2032: if ( !( $file =~ m:^/:) ) {
2033: $location = $dir. '/'.$file;
2034: } else {
2035: $location = '/home/httpd/html/res'.$file;
2036: }
1.36 albertel 2037: }
2038: $location=~s://+:/:g; # remove duplicate /
1.46 www 2039: while ($location=~m:/\.\./:) {$location=~ s:/[^/]+/\.\./:/:g;} #remove dir/..
2040: return $location;
2041: }
1.36 albertel 2042:
1.46 www 2043: sub hreflocation {
2044: my ($dir,$file)=@_;
2045: unless (($_=~/^http:\/\//i) || ($_=~/^\//)) {
2046: my $finalpath=filelocation($dir,$file);
2047: $finalpath=~s/^\/home\/httpd\/html//;
2048: return $finalpath;
2049: } else {
2050: return $file;
2051: }
1.31 www 2052: }
2053:
2054: # ------------------------------------------------------------- Declutters URLs
2055:
2056: sub declutter {
2057: my $thisfn=shift;
2058: $thisfn=~s/^$perlvar{'lonDocRoot'}//;
2059: $thisfn=~s/^\///;
2060: $thisfn=~s/^res\///;
2061: return $thisfn;
1.12 www 2062: }
2063:
2064: # -------------------------------------------------------- Escape Special Chars
2065:
2066: sub escape {
2067: my $str=shift;
2068: $str =~ s/(\W)/"%".unpack('H2',$1)/eg;
2069: return $str;
2070: }
2071:
2072: # ----------------------------------------------------- Un-Escape Special Chars
2073:
2074: sub unescape {
2075: my $str=shift;
2076: $str =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C",hex($1))/eg;
2077: return $str;
2078: }
1.11 www 2079:
1.1 albertel 2080: # ================================================================ Main Program
2081:
2082: sub BEGIN {
2083: if ($readit ne 'done') {
2084: # ------------------------------------------------------------ Read access.conf
2085: {
2086: my $config=Apache::File->new("/etc/httpd/conf/access.conf");
2087:
2088: while (my $configline=<$config>) {
2089: if ($configline =~ /PerlSetVar/) {
2090: my ($dummy,$varname,$varvalue)=split(/\s+/,$configline);
1.8 www 2091: chomp($varvalue);
1.1 albertel 2092: $perlvar{$varname}=$varvalue;
2093: }
2094: }
2095: }
2096:
2097: # ------------------------------------------------------------- Read hosts file
2098: {
2099: my $config=Apache::File->new("$perlvar{'lonTabDir'}/hosts.tab");
2100:
2101: while (my $configline=<$config>) {
2102: my ($id,$domain,$role,$name,$ip)=split(/:/,$configline);
2103: $hostname{$id}=$name;
2104: $hostdom{$id}=$domain;
2105: if ($role eq 'library') { $libserv{$id}=$name; }
2106: }
2107: }
2108:
2109: # ------------------------------------------------------ Read spare server file
2110: {
2111: my $config=Apache::File->new("$perlvar{'lonTabDir'}/spare.tab");
2112:
2113: while (my $configline=<$config>) {
2114: chomp($configline);
2115: if (($configline) && ($configline ne $perlvar{'lonHostID'})) {
2116: $spareid{$configline}=1;
2117: }
2118: }
2119: }
1.11 www 2120: # ------------------------------------------------------------ Read permissions
2121: {
2122: my $config=Apache::File->new("$perlvar{'lonTabDir'}/roles.tab");
2123:
2124: while (my $configline=<$config>) {
2125: chomp($configline);
2126: my ($role,$perm)=split(/ /,$configline);
2127: if ($perm ne '') { $pr{$role}=$perm; }
2128: }
2129: }
2130:
2131: # -------------------------------------------- Read plain texts for permissions
2132: {
2133: my $config=Apache::File->new("$perlvar{'lonTabDir'}/rolesplain.tab");
2134:
2135: while (my $configline=<$config>) {
2136: chomp($configline);
2137: my ($short,$plain)=split(/:/,$configline);
2138: if ($plain ne '') { $prp{$short}=$plain; }
1.25 www 2139: }
2140: }
2141:
2142: # ------------------------------------------------------------- Read file types
2143: {
2144: my $config=Apache::File->new("$perlvar{'lonTabDir'}/filetypes.tab");
2145:
2146: while (my $configline=<$config>) {
2147: chomp($configline);
2148: my ($ending,$emb,@descr)=split(/\s+/,$configline);
2149: if ($descr[0] ne '') {
2150: $fe{$ending}=$emb;
2151: $fd{$ending}=join(' ',@descr);
2152: }
1.11 www 2153: }
2154: }
2155:
1.71 www 2156: %metacache=();
1.22 www 2157:
1.1 albertel 2158: $readit='done';
1.12 www 2159: &logthis('<font color=yellow>INFO: Read configuration</font>');
1.1 albertel 2160: }
2161: }
2162: 1;
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>