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