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