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