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