1: # The LearningOnline Network
2: # TCP networking package
3: #
4: # $Id: lonnet.pm,v 1.180 2001/12/04 15:19:11 albertel Exp $
5: #
6: # Copyright Michigan State University Board of Trustees
7: #
8: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
9: #
10: # LON-CAPA is free software; you can redistribute it and/or modify
11: # it under the terms of the GNU General Public License as published by
12: # the Free Software Foundation; either version 2 of the License, or
13: # (at your option) any later version.
14: #
15: # LON-CAPA is distributed in the hope that it will be useful,
16: # but WITHOUT ANY WARRANTY; without even the implied warranty of
17: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18: # GNU General Public License for more details.
19: #
20: # You should have received a copy of the GNU General Public License
21: # along with LON-CAPA; if not, write to the Free Software
22: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23: #
24: # /home/httpd/html/adm/gpl.txt
25: #
26: # http://www.lon-capa.org/
27: #
28: # 6/1/99,6/2,6/10,6/11,6/12,6/14,6/26,6/28,6/29,6/30,
29: # 7/1,7/2,7/9,7/10,7/12,7/14,7/15,7/19,
30: # 11/8,11/16,11/18,11/22,11/23,12/22,
31: # 01/06,01/13,02/24,02/28,02/29,
32: # 03/01,03/02,03/06,03/07,03/13,
33: # 04/05,05/29,05/31,06/01,
34: # 06/05,06/26 Gerd Kortemeyer
35: # 06/26 Ben Tyszka
36: # 06/30,07/15,07/17,07/18,07/20,07/21,07/22,07/25 Gerd Kortemeyer
37: # 08/14 Ben Tyszka
38: # 08/22,08/28,08/31,09/01,09/02,09/04,09/05,09/25,09/28,09/30 Gerd Kortemeyer
39: # 10/04 Gerd Kortemeyer
40: # 10/04 Guy Albertelli
41: # 10/06,10/09,10/10,10/11,10/14,10/20,10/23,10/25,10/26,10/27,10/28,10/29,
42: # 10/30,10/31,
43: # 11/2,11/14,11/15,11/16,11/20,11/21,11/22,11/25,11/27,
44: # 12/02,12/12,12/13,12/14,12/28,12/29 Gerd Kortemeyer
45: # 05/01/01 Guy Albertelli
46: # 05/01,06/01,09/01 Gerd Kortemeyer
47: # 09/01 Guy Albertelli
48: # 09/01,10/01,11/01 Gerd Kortemeyer
49: # YEAR=2001
50: # 02/27/01 Scott Harrison
51: # 3/2 Gerd Kortemeyer
52: # 3/15,3/19 Scott Harrison
53: # 3/19,3/20 Gerd Kortemeyer
54: # 3/22,3/27,4/2,4/16,4/17 Scott Harrison
55: # 5/26,5/28 Gerd Kortemeyer
56: # 5/30 H. K. Ng
57: # 6/1 Gerd Kortemeyer
58: # July Guy Albertelli
59: # 8/4,8/7,8/8,8/9,8/11,8/16,8/17,8/18,8/20,8/23,9/20,9/21,9/26,
60: # 10/2 Gerd Kortemeyer
61: # 10/5,10/10,11/13,11/15 Scott Harrison
62: # 11/17,11/20,11/22,11/29 Gerd Kortemeyer
63: #
64: # $Id: lonnet.pm,v 1.180 2001/12/04 15:19:11 albertel Exp $
65: #
66: ###
67:
68: # Functions for use by content handlers:
69: #
70: # metadata_query(sql-query-string,custom-metadata-regex) :
71: # returns file handle of where sql and
72: # regex results will be stored for query
73: # plaintext(short) : plain text explanation of short term
74: # fileembstyle(ext) : embed style in page for file extension
75: # filedescription(ext) : descriptor text for file extension
76: # allowed(short,url) : returns codes for allowed actions
77: # F: full access
78: # U,I,K: authentication modes (cxx only)
79: # '': forbidden
80: # 1: user needs to choose course
81: # 2: browse allowed
82: # definerole(rolename,sys,dom,cou) : define a custom role rolename
83: # set privileges in format of lonTabs/roles.tab for
84: # system, domain and course level,
85: # assignrole(udom,uname,url,role,end,start) : give a role to a user for the
86: # level given by url. Optional start and end dates
87: # (leave empty string or zero for "no date")
88: # assigncustomrole (udom,uname,url,rdom,rnam,rolename,end,start) : give a
89: # custom role to a user for the level given by url.
90: # Specify name and domain of role author, and role name
91: # revokerole (udom,uname,url,role) : Revoke a role for url
92: # revokecustomrole (udom,uname,url,rdom,rnam,rolename) : Revoke a custom role
93: # appenv(hash) : adds hash to session environment
94: # delenv(varname) : deletes all environment entries starting with varname
95: # store(hashref,symb,courseid,udom,uname)
96: # : stores hash permanently for this url
97: # hashref needs to be given, and should be a \%hashname
98: # the remaining args aren't required and if they aren't
99: # passed or are '' they will be derived from the ENV
100: # cstore(hashref,symb,courseid,udom,uname)
101: # : same as store but uses the critical interface to
102: # guarentee a store
103: # restore(symb,courseid,udom,uname)
104: # : returns hash for this symb, all args are optional
105: # if they aren't given they will be derived from the
106: # current enviroment
107: #
108: #
109: # for the next 6 functions udom and uname are optional
110: # if supplied they use udom as the domain and uname
111: # as the username for the function (supply a courseid
112: # for the uname if you want a course database)
113: # if not supplied it uses %ENV and looks at
114: # user. attribute for the values
115: #
116: # eget(namesp,arrayref,udom,uname)
117: # : returns hash with keys from array reference filled
118: # in from namesp (encrypts the return communication)
119: # get(namesp,arrayref,udom,uname)
120: # : returns hash with keys from array reference filled
121: # in from namesp
122: # dump(namesp,udom,uname) : dumps the complete namespace into a hash
123: # del(namesp,array,udom,uname) : deletes keys out of array from namesp
124: # put(namesp,hash,udom,uname) : stores hash in namesp
125: # cput(namesp,hash,udom,uname) : critical put
126: #
127: #
128: # ssi(url,hash) : does a complete request cycle on url to localhost, posts
129: # hash
130: # coursedescription(id) : returns and caches course description for id
131: # repcopy(filename) : replicate file
132: # dirlist(url) : gets a directory listing
133: # directcondval(index) : reading condition value of single condition from
134: # state string
135: # condval(index) : value of condition index based on state
136: # EXT(name) : value of a variable
137: # symblist(map,hash) : Updates symbolic storage links
138: # symbread([filename]) : returns the data handle (filename optional)
139: # rndseed([symb,courseid,domain,uname])
140: # : returns a random seed, all arguments are optional,
141: # if they aren't sent it use the environment to derive
142: # them
143: # Note: if symb isn't sent and it can't get one from
144: # &symbread it will use the current time as it's return
145: # receipt() : returns a receipt to be given out to users
146: # getfile(filename) : returns the contents of filename, or a -1 if it can't
147: # be found, replicates and subscribes to the file
148: # filelocation(dir,file) : returns a fairly clean absolute reference to file
149: # from the directory dir
150: # hreflocation(dir,file) : same as filelocation, but for hrefs
151: # log(domain,user,home,msg) : write to permanent log for user
152: # usection(domain,user,courseid) : output of section name/number or '' for
153: # "not in course" and '-1' for "no section"
154: # userenvironment(domain,user,what) : puts out any environment parameter
155: # for a user
156: # idput(domain,hash) : writes IDs for users from hash (name=>id,name=>id)
157: # idget(domain,array): returns hash with usernames (id=>name,id=>name) for
158: # an array of IDs
159: # idrget(domain,array): returns hash with IDs for usernames (name=>id,...) for
160: # an array of names
161: # metadata(file,entry): returns the metadata entry for a file. entry='keys'
162: # returns a comma separated list of keys
163: #
164:
165: package Apache::lonnet;
166:
167: use strict;
168: use Apache::File;
169: use LWP::UserAgent();
170: use HTTP::Headers;
171: use vars
172: qw(%perlvar %hostname %homecache %hostip %spareid %hostdom %libserv %pr %prp %fe %fd $readit %metacache %packagetab %courselogs);
173: use IO::Socket;
174: use GDBM_File;
175: use Apache::Constants qw(:common :http);
176: use HTML::TokeParser;
177: use Fcntl qw(:flock);
178:
179: # --------------------------------------------------------------------- Logging
180:
181: sub logtouch {
182: my $execdir=$perlvar{'lonDaemons'};
183: unless (-e "$execdir/logs/lonnet.log") {
184: my $fh=Apache::File->new(">>$execdir/logs/lonnet.log");
185: close $fh;
186: }
187: my ($wwwuid,$wwwgid)=(getpwnam('www'))[2,3];
188: chown($wwwuid,$wwwgid,$execdir.'/logs/lonnet.log');
189: }
190:
191: sub logthis {
192: my $message=shift;
193: my $execdir=$perlvar{'lonDaemons'};
194: my $now=time;
195: my $local=localtime($now);
196: my $fh=Apache::File->new(">>$execdir/logs/lonnet.log");
197: print $fh "$local ($$): $message\n";
198: return 1;
199: }
200:
201: sub logperm {
202: my $message=shift;
203: my $execdir=$perlvar{'lonDaemons'};
204: my $now=time;
205: my $local=localtime($now);
206: my $fh=Apache::File->new(">>$execdir/logs/lonnet.perm.log");
207: print $fh "$now:$message:$local\n";
208: return 1;
209: }
210:
211: # -------------------------------------------------- Non-critical communication
212: sub subreply {
213: my ($cmd,$server)=@_;
214: my $peerfile="$perlvar{'lonSockDir'}/$server";
215: my $client=IO::Socket::UNIX->new(Peer =>"$peerfile",
216: Type => SOCK_STREAM,
217: Timeout => 10)
218: or return "con_lost";
219: print $client "$cmd\n";
220: my $answer=<$client>;
221: if (!$answer) { $answer="con_lost"; }
222: chomp($answer);
223: return $answer;
224: }
225:
226: sub reply {
227: my ($cmd,$server)=@_;
228: my $answer=subreply($cmd,$server);
229: if ($answer eq 'con_lost') { $answer=subreply($cmd,$server); }
230: if (($answer=~/^refused/) || ($answer=~/^rejected/)) {
231: &logthis("<font color=blue>WARNING:".
232: " $cmd to $server returned $answer</font>");
233: }
234: return $answer;
235: }
236:
237: # ----------------------------------------------------------- Send USR1 to lonc
238:
239: sub reconlonc {
240: my $peerfile=shift;
241: &logthis("Trying to reconnect for $peerfile");
242: my $loncfile="$perlvar{'lonDaemons'}/logs/lonc.pid";
243: if (my $fh=Apache::File->new("$loncfile")) {
244: my $loncpid=<$fh>;
245: chomp($loncpid);
246: if (kill 0 => $loncpid) {
247: &logthis("lonc at pid $loncpid responding, sending USR1");
248: kill USR1 => $loncpid;
249: sleep 1;
250: if (-e "$peerfile") { return; }
251: &logthis("$peerfile still not there, give it another try");
252: sleep 5;
253: if (-e "$peerfile") { return; }
254: &logthis(
255: "<font color=blue>WARNING: $peerfile still not there, giving up</font>");
256: } else {
257: &logthis(
258: "<font color=blue>WARNING:".
259: " lonc at pid $loncpid not responding, giving up</font>");
260: }
261: } else {
262: &logthis('<font color=blue>WARNING: lonc not running, giving up</font>');
263: }
264: }
265:
266: # ------------------------------------------------------ Critical communication
267:
268: sub critical {
269: my ($cmd,$server)=@_;
270: unless ($hostname{$server}) {
271: &logthis("<font color=blue>WARNING:".
272: " Critical message to unknown server ($server)</font>");
273: return 'no_such_host';
274: }
275: my $answer=reply($cmd,$server);
276: if ($answer eq 'con_lost') {
277: my $pingreply=reply('ping',$server);
278: &reconlonc("$perlvar{'lonSockDir'}/$server");
279: my $pongreply=reply('pong',$server);
280: &logthis("Ping/Pong for $server: $pingreply/$pongreply");
281: $answer=reply($cmd,$server);
282: if ($answer eq 'con_lost') {
283: my $now=time;
284: my $middlename=$cmd;
285: $middlename=substr($middlename,0,16);
286: $middlename=~s/\W//g;
287: my $dfilename=
288: "$perlvar{'lonSockDir'}/delayed/$now.$middlename.$server";
289: {
290: my $dfh;
291: if ($dfh=Apache::File->new(">$dfilename")) {
292: print $dfh "$cmd\n";
293: }
294: }
295: sleep 2;
296: my $wcmd='';
297: {
298: my $dfh;
299: if ($dfh=Apache::File->new("$dfilename")) {
300: $wcmd=<$dfh>;
301: }
302: }
303: chomp($wcmd);
304: if ($wcmd eq $cmd) {
305: &logthis("<font color=blue>WARNING: ".
306: "Connection buffer $dfilename: $cmd</font>");
307: &logperm("D:$server:$cmd");
308: return 'con_delayed';
309: } else {
310: &logthis("<font color=red>CRITICAL:"
311: ." Critical connection failed: $server $cmd</font>");
312: &logperm("F:$server:$cmd");
313: return 'con_failed';
314: }
315: }
316: }
317: return $answer;
318: }
319:
320: # ---------------------------------------------------------- Append Environment
321:
322: sub appenv {
323: my %newenv=@_;
324: map {
325: if (($newenv{$_}=~/^user\.role/) || ($newenv{$_}=~/^user\.priv/)) {
326: &logthis("<font color=blue>WARNING: ".
327: "Attempt to modify environment ".$_." to ".$newenv{$_}
328: .'</font>');
329: delete($newenv{$_});
330: } else {
331: $ENV{$_}=$newenv{$_};
332: }
333: } keys %newenv;
334:
335: my $lockfh;
336: unless ($lockfh=Apache::File->new("$ENV{'user.environment'}")) {
337: return 'error: '.$!;
338: }
339: unless (flock($lockfh,LOCK_EX)) {
340: &logthis("<font color=blue>WARNING: ".
341: 'Could not obtain exclusive lock in appenv: '.$!);
342: $lockfh->close();
343: return 'error: '.$!;
344: }
345:
346: my @oldenv;
347: {
348: my $fh;
349: unless ($fh=Apache::File->new("$ENV{'user.environment'}")) {
350: return 'error: '.$!;
351: }
352: @oldenv=<$fh>;
353: $fh->close();
354: }
355: for (my $i=0; $i<=$#oldenv; $i++) {
356: chomp($oldenv[$i]);
357: if ($oldenv[$i] ne '') {
358: my ($name,$value)=split(/=/,$oldenv[$i]);
359: unless (defined($newenv{$name})) {
360: $newenv{$name}=$value;
361: }
362: }
363: }
364: {
365: my $fh;
366: unless ($fh=Apache::File->new(">$ENV{'user.environment'}")) {
367: return 'error';
368: }
369: my $newname;
370: foreach $newname (keys %newenv) {
371: print $fh "$newname=$newenv{$newname}\n";
372: }
373: $fh->close();
374: }
375:
376: $lockfh->close();
377: return 'ok';
378: }
379: # ----------------------------------------------------- Delete from Environment
380:
381: sub delenv {
382: my $delthis=shift;
383: my %newenv=();
384: if (($delthis=~/user\.role/) || ($delthis=~/user\.priv/)) {
385: &logthis("<font color=blue>WARNING: ".
386: "Attempt to delete from environment ".$delthis);
387: return 'error';
388: }
389: my @oldenv;
390: {
391: my $fh;
392: unless ($fh=Apache::File->new("$ENV{'user.environment'}")) {
393: return 'error';
394: }
395: unless (flock($fh,LOCK_SH)) {
396: &logthis("<font color=blue>WARNING: ".
397: 'Could not obtain shared lock in delenv: '.$!);
398: $fh->close();
399: return 'error: '.$!;
400: }
401: @oldenv=<$fh>;
402: $fh->close();
403: }
404: {
405: my $fh;
406: unless ($fh=Apache::File->new(">$ENV{'user.environment'}")) {
407: return 'error';
408: }
409: unless (flock($fh,LOCK_EX)) {
410: &logthis("<font color=blue>WARNING: ".
411: 'Could not obtain exclusive lock in delenv: '.$!);
412: $fh->close();
413: return 'error: '.$!;
414: }
415: map {
416: unless ($_=~/^$delthis/) { print $fh $_; }
417: } @oldenv;
418: $fh->close();
419: }
420: return 'ok';
421: }
422:
423: # ------------------------------ Find server with least workload from spare.tab
424:
425: sub spareserver {
426: my $tryserver;
427: my $spareserver='';
428: my $lowestserver=100;
429: foreach $tryserver (keys %spareid) {
430: my $answer=reply('load',$tryserver);
431: if (($answer =~ /\d/) && ($answer<$lowestserver)) {
432: $spareserver="http://$hostname{$tryserver}";
433: $lowestserver=$answer;
434: }
435: }
436: return $spareserver;
437: }
438:
439: # ----------------------- Try to determine user's current authentication scheme
440:
441: sub queryauthenticate {
442: my ($uname,$udom)=@_;
443: if (($perlvar{'lonRole'} eq 'library') &&
444: ($udom eq $perlvar{'lonDefDomain'})) {
445: my $answer=reply("encrypt:currentauth:$udom:$uname",
446: $perlvar{'lonHostID'});
447: unless ($answer eq 'unknown_user' or $answer eq 'refused') {
448: if (length($answer)) {
449: return $answer;
450: }
451: else {
452: &logthis("User $uname at $udom lacks an authentication mechanism");
453: return 'no_host';
454: }
455: }
456: }
457:
458: my $tryserver;
459: foreach $tryserver (keys %libserv) {
460: if ($hostdom{$tryserver} eq $udom) {
461: my $answer=reply("encrypt:currentauth:$udom:$uname",$tryserver);
462: unless ($answer eq 'unknown_user' or $answer eq 'refused') {
463: if (length($answer)) {
464: return $answer;
465: }
466: else {
467: &logthis("User $uname at $udom lacks an authentication mechanism");
468: return 'no_host';
469: }
470: }
471: }
472: }
473: &logthis("User $uname at $udom lacks an authentication mechanism");
474: return 'no_host';
475: }
476:
477: # --------- Try to authenticate user from domain's lib servers (first this one)
478:
479: sub authenticate {
480: my ($uname,$upass,$udom)=@_;
481: $upass=escape($upass);
482: if (($perlvar{'lonRole'} eq 'library') &&
483: ($udom eq $perlvar{'lonDefDomain'})) {
484: my $answer=reply("encrypt:auth:$udom:$uname:$upass",$perlvar{'lonHostID'});
485: if ($answer =~ /authorized/) {
486: if ($answer eq 'authorized') {
487: &logthis("User $uname at $udom authorized by local server");
488: return $perlvar{'lonHostID'};
489: }
490: if ($answer eq 'non_authorized') {
491: &logthis("User $uname at $udom rejected by local server");
492: return 'no_host';
493: }
494: }
495: }
496:
497: my $tryserver;
498: foreach $tryserver (keys %libserv) {
499: if ($hostdom{$tryserver} eq $udom) {
500: my $answer=reply("encrypt:auth:$udom:$uname:$upass",$tryserver);
501: if ($answer =~ /authorized/) {
502: if ($answer eq 'authorized') {
503: &logthis("User $uname at $udom authorized by $tryserver");
504: return $tryserver;
505: }
506: if ($answer eq 'non_authorized') {
507: &logthis("User $uname at $udom rejected by $tryserver");
508: return 'no_host';
509: }
510: }
511: }
512: }
513: &logthis("User $uname at $udom could not be authenticated");
514: return 'no_host';
515: }
516:
517: # ---------------------- Find the homebase for a user from domain's lib servers
518:
519: sub homeserver {
520: my ($uname,$udom)=@_;
521:
522: my $index="$uname:$udom";
523: if ($homecache{$index}) { return "$homecache{$index}"; }
524:
525: my $tryserver;
526: foreach $tryserver (keys %libserv) {
527: if ($hostdom{$tryserver} eq $udom) {
528: my $answer=reply("home:$udom:$uname",$tryserver);
529: if ($answer eq 'found') {
530: $homecache{$index}=$tryserver;
531: return $tryserver;
532: }
533: }
534: }
535: return 'no_host';
536: }
537:
538: # ------------------------------------- Find the usernames behind a list of IDs
539:
540: sub idget {
541: my ($udom,@ids)=@_;
542: my %returnhash=();
543:
544: my $tryserver;
545: foreach $tryserver (keys %libserv) {
546: if ($hostdom{$tryserver} eq $udom) {
547: my $idlist=join('&',@ids);
548: $idlist=~tr/A-Z/a-z/;
549: my $reply=&reply("idget:$udom:".$idlist,$tryserver);
550: my @answer=();
551: if (($reply ne 'con_lost') && ($reply!~/^error\:/)) {
552: @answer=split(/\&/,$reply);
553: } ;
554: my $i;
555: for ($i=0;$i<=$#ids;$i++) {
556: if ($answer[$i]) {
557: $returnhash{$ids[$i]}=$answer[$i];
558: }
559: }
560: }
561: }
562: return %returnhash;
563: }
564:
565: # ------------------------------------- Find the IDs behind a list of usernames
566:
567: sub idrget {
568: my ($udom,@unames)=@_;
569: my %returnhash=();
570: map {
571: $returnhash{$_}=(&userenvironment($udom,$_,'id'))[1];
572: } @unames;
573: return %returnhash;
574: }
575:
576: # ------------------------------- Store away a list of names and associated IDs
577:
578: sub idput {
579: my ($udom,%ids)=@_;
580: my %servers=();
581: map {
582: my $uhom=&homeserver($_,$udom);
583: if ($uhom ne 'no_host') {
584: my $id=&escape($ids{$_});
585: $id=~tr/A-Z/a-z/;
586: my $unam=&escape($_);
587: if ($servers{$uhom}) {
588: $servers{$uhom}.='&'.$id.'='.$unam;
589: } else {
590: $servers{$uhom}=$id.'='.$unam;
591: }
592: &critical('put:'.$udom.':'.$unam.':environment:id='.$id,$uhom);
593: }
594: } keys %ids;
595: map {
596: &critical('idput:'.$udom.':'.$servers{$_},$_);
597: } keys %servers;
598: }
599:
600: # ------------------------------------- Find the section of student in a course
601:
602: sub usection {
603: my ($udom,$unam,$courseid)=@_;
604: $courseid=~s/\_/\//g;
605: $courseid=~s/^(\w)/\/$1/;
606: map {
607: my ($key,$value)=split(/\=/,$_);
608: $key=&unescape($key);
609: if ($key=~/^$courseid(?:\/)*(\w+)*\_st$/) {
610: my $section=$1;
611: if ($key eq $courseid.'_st') { $section=''; }
612: my ($dummy,$end,$start)=split(/\_/,&unescape($value));
613: my $now=time;
614: my $notactive=0;
615: if ($start) {
616: if ($now<$start) { $notactive=1; }
617: }
618: if ($end) {
619: if ($now>$end) { $notactive=1; }
620: }
621: unless ($notactive) { return $section; }
622: }
623: } split(/\&/,&reply('dump:'.$udom.':'.$unam.':roles',
624: &homeserver($unam,$udom)));
625: return '-1';
626: }
627:
628: # ------------------------------------- Read an entry from a user's environment
629:
630: sub userenvironment {
631: my ($udom,$unam,@what)=@_;
632: my %returnhash=();
633: my @answer=split(/\&/,
634: &reply('get:'.$udom.':'.$unam.':environment:'.join('&',@what),
635: &homeserver($unam,$udom)));
636: my $i;
637: for ($i=0;$i<=$#what;$i++) {
638: $returnhash{$what[$i]}=&unescape($answer[$i]);
639: }
640: return %returnhash;
641: }
642:
643: # ----------------------------- Subscribe to a resource, return URL if possible
644:
645: sub subscribe {
646: my $fname=shift;
647: my $author=$fname;
648: $author=~s/\/home\/httpd\/html\/res\/([^\/]*)\/([^\/]*).*/$1\/$2/;
649: my ($udom,$uname)=split(/\//,$author);
650: my $home=homeserver($uname,$udom);
651: if (($home eq 'no_host') || ($home eq $perlvar{'lonHostID'})) {
652: return 'not_found';
653: }
654: my $answer=reply("sub:$fname",$home);
655: if (($answer eq 'con_lost') || ($answer eq 'rejected')) {
656: $answer.=' by '.$home;
657: }
658: return $answer;
659: }
660:
661: # -------------------------------------------------------------- Replicate file
662:
663: sub repcopy {
664: my $filename=shift;
665: $filename=~s/\/+/\//g;
666: my $transname="$filename.in.transfer";
667: if ((-e $filename) || (-e $transname)) { return OK; }
668: my $remoteurl=subscribe($filename);
669: if ($remoteurl =~ /^con_lost by/) {
670: &logthis("Subscribe returned $remoteurl: $filename");
671: return HTTP_SERVICE_UNAVAILABLE;
672: } elsif ($remoteurl eq 'not_found') {
673: &logthis("Subscribe returned not_found: $filename");
674: return HTTP_NOT_FOUND;
675: } elsif ($remoteurl =~ /^rejected by/) {
676: &logthis("Subscribe returned $remoteurl: $filename");
677: return FORBIDDEN;
678: } elsif ($remoteurl eq 'directory') {
679: return OK;
680: } else {
681: my @parts=split(/\//,$filename);
682: my $path="/$parts[1]/$parts[2]/$parts[3]/$parts[4]";
683: if ($path ne "$perlvar{'lonDocRoot'}/res") {
684: &logthis("Malconfiguration for replication: $filename");
685: return HTTP_BAD_REQUEST;
686: }
687: my $count;
688: for ($count=5;$count<$#parts;$count++) {
689: $path.="/$parts[$count]";
690: if ((-e $path)!=1) {
691: mkdir($path,0777);
692: }
693: }
694: my $ua=new LWP::UserAgent;
695: my $request=new HTTP::Request('GET',"$remoteurl");
696: my $response=$ua->request($request,$transname);
697: if ($response->is_error()) {
698: unlink($transname);
699: my $message=$response->status_line;
700: &logthis("<font color=blue>WARNING:"
701: ." LWP get: $message: $filename</font>");
702: return HTTP_SERVICE_UNAVAILABLE;
703: } else {
704: if ($remoteurl!~/\.meta$/) {
705: my $mrequest=new HTTP::Request('GET',$remoteurl.'.meta');
706: my $mresponse=$ua->request($mrequest,$filename.'.meta');
707: if ($mresponse->is_error()) {
708: unlink($filename.'.meta');
709: &logthis(
710: "<font color=yellow>INFO: No metadata: $filename</font>");
711: }
712: }
713: rename($transname,$filename);
714: return OK;
715: }
716: }
717: }
718:
719: # --------------------------------------------------------- Server Side Include
720:
721: sub ssi {
722:
723: my ($fn,%form)=@_;
724:
725: my $ua=new LWP::UserAgent;
726:
727: my $request;
728:
729: if (%form) {
730: $request=new HTTP::Request('POST',"http://".$ENV{'HTTP_HOST'}.$fn);
731: $request->content(join '&', map { "$_=$form{$_}" } keys %form);
732: } else {
733: $request=new HTTP::Request('GET',"http://".$ENV{'HTTP_HOST'}.$fn);
734: }
735:
736: $request->header(Cookie => $ENV{'HTTP_COOKIE'});
737: my $response=$ua->request($request);
738:
739: return $response->content;
740: }
741:
742: # ------------------------------------------------------------------------- Log
743:
744: sub log {
745: my ($dom,$nam,$hom,$what)=@_;
746: return critical("log:$dom:$nam:$what",$hom);
747: }
748:
749: # ------------------------------------------------------------------ Course Log
750:
751: sub flushcourselogs {
752: &logthis('Flushing course log buffers');
753: map {
754: my $crsid=$_;
755: if (&reply('log:'.$ENV{'course.'.$crsid.'.domain'}.':'.
756: $ENV{'course.'.$crsid.'.num'}.':'.
757: &escape($courselogs{$crsid}),
758: $ENV{'course.'.$crsid.'.home'}) eq 'ok') {
759: delete $courselogs{$crsid};
760: } else {
761: &logthis('Failed to flush log buffer for '.$crsid);
762: if (length($courselogs{$crsid})>40000) {
763: &logthis("<font color=blue>WARNING: Buffer for ".$crsid.
764: " exceeded maximum size, deleting.</font>");
765: delete $courselogs{$crsid};
766: }
767: }
768: } keys %courselogs;
769: }
770:
771: sub courselog {
772: my $what=shift;
773: $what=time.':'.$what;
774: unless ($ENV{'request.course.id'}) { return ''; }
775: if (defined $courselogs{$ENV{'request.course.id'}}) {
776: $courselogs{$ENV{'request.course.id'}}.='&'.$what;
777: } else {
778: $courselogs{$ENV{'request.course.id'}}.=$what;
779: }
780: if (length($courselogs{$ENV{'request.course.id'}})>4048) {
781: &flushcourselogs();
782: }
783: }
784:
785: sub courseacclog {
786: my $fnsymb=shift;
787: unless ($ENV{'request.course.id'}) { return ''; }
788: my $what=$fnsymb.':'.$ENV{'user.name'}.':'.$ENV{'user.domain'};
789: if ($what=~/(problem|exam|quiz|assess|survey|form)$/) {
790: map {
791: if ($_=~/^form\.(.*)/) {
792: $what.=':'.$1.'='.$ENV{$_};
793: }
794: } keys %ENV;
795: }
796: &courselog($what);
797: }
798:
799: # ----------------------------------------------------------- Check out an item
800:
801: sub checkout {
802: my ($symb,$tuname,$tudom,$tcrsid)=@_;
803: my $now=time;
804: my $lonhost=$perlvar{'lonHostID'};
805: my $infostr=&escape(
806: $tuname.'&'.
807: $tudom.'&'.
808: $tcrsid.'&'.
809: $symb.'&'.
810: $now.'&'.$ENV{'REMOTE_ADDR'});
811: my $token=&reply('tmpput:'.$infostr,$lonhost);
812: if ($token=~/^error\:/) {
813: &logthis("<font color=blue>WARNING: ".
814: "Checkout tmpput failed ".$tudom.' - '.$tuname.' - '.$symb.
815: "</font>");
816: return '';
817: }
818:
819: $token=~s/^(\d+)\_.*\_(\d+)$/$1\*$2\*$lonhost/;
820: $token=~tr/a-z/A-Z/;
821:
822: my %infohash=('resource.0.outtoken' => $token,
823: 'resource.0.checkouttime' => $now,
824: 'resource.0.outremote' => $ENV{'REMOTE_ADDR'});
825:
826: unless (&cstore(\%infohash,$symb,$tcrsid,$tudom,$tuname) eq 'ok') {
827: return '';
828: } else {
829: &logthis("<font color=blue>WARNING: ".
830: "Checkout cstore failed ".$tudom.' - '.$tuname.' - '.$symb.
831: "</font>");
832: }
833:
834: if (&log($tudom,$tuname,&homeserver($tuname,$tudom),
835: &escape('Checkout '.$infostr.' - '.
836: $token)) ne 'ok') {
837: return '';
838: } else {
839: &logthis("<font color=blue>WARNING: ".
840: "Checkout log failed ".$tudom.' - '.$tuname.' - '.$symb.
841: "</font>");
842: }
843: return $token;
844: }
845:
846: # ------------------------------------------------------------ Check in an item
847:
848: sub checkin {
849: my $token=shift;
850: my $now=time;
851: my ($ta,$tb,$lonhost)=split(/\*/,$token);
852: $lonhost=~tr/A-Z/a-z/;
853: my $dtoken=$ta.'_'.$hostip{$lonhost}.'_'.$tb;
854: $dtoken=~s/\W/\_/g;
855: my ($tuname,$tudom,$tcrsid,$symb,$chtim,$rmaddr)=
856: split(/\&/,&unescape(&reply('tmpget:'.$dtoken,$lonhost)));
857:
858: unless (($tuname) && ($tudom)) {
859: &logthis('Check in '.$token.' ('.$dtoken.') failed');
860: return '';
861: }
862:
863: unless (&allowed('mgr',$tcrsid)) {
864: &logthis('Check in '.$token.' ('.$dtoken.') unauthorized: '.
865: $ENV{'user.name'}.' - '.$ENV{'user.domain'});
866: return '';
867: }
868:
869: my %infohash=('resource.0.intoken' => $token,
870: 'resource.0.checkintime' => $now,
871: 'resource.0.inremote' => $ENV{'REMOTE_ADDR'});
872:
873: unless (&cstore(\%infohash,$symb,$tcrsid,$tudom,$tuname) eq 'ok') {
874: return '';
875: }
876:
877: if (&log($tudom,$tuname,&homeserver($tuname,$tudom),
878: &escape('Checkin - '.$token)) ne 'ok') {
879: return '';
880: }
881:
882: return ($symb,$tuname,$tudom,$tcrsid);
883: }
884:
885: # --------------------------------------------- Set Expire Date for Spreadsheet
886:
887: sub expirespread {
888: my ($uname,$udom,$stype,$usymb)=@_;
889: my $cid=$ENV{'request.course.id'};
890: if ($cid) {
891: my $now=time;
892: my $key=$uname.':'.$udom.':'.$stype.':'.$usymb;
893: return &reply('put:'.$ENV{'course.'.$cid.'.domain'}.':'.
894: $ENV{'course.'.$cid.'.num'}.
895: ':nohist_expirationdates:'.
896: &escape($key).'='.$now,
897: $ENV{'course.'.$cid.'.home'})
898: }
899: return 'ok';
900: }
901:
902: # ----------------------------------------------------- Devalidate Spreadsheets
903:
904: sub devalidate {
905: my $symb=shift;
906: my $cid=$ENV{'request.course.id'};
907: if ($cid) {
908: my $key=$ENV{'user.name'}.':'.$ENV{'user.domain'}.':';
909: my $status=
910: &del('nohist_calculatedsheet',
911: [$key.'studentcalc'],
912: $ENV{'course.'.$cid.'.domain'},
913: $ENV{'course.'.$cid.'.num'})
914: .' '.
915: &del('nohist_calculatedsheets_'.$cid,
916: [$key.'assesscalc:'.$symb]);
917: unless ($status eq 'ok ok') {
918: &logthis('Could not devalidate spreadsheet '.
919: $ENV{'user.name'}.' at '.$ENV{'user.domain'}.' for '.
920: $symb.': '.$status);
921: }
922: }
923: }
924:
925: sub hash2str {
926: my (%hash)=@_;
927: my $result='';
928: map { $result.=escape($_).'='.escape($hash{$_}).'&'; } keys %hash;
929: $result=~s/\&$//;
930: return $result;
931: }
932:
933: sub str2hash {
934: my ($string) = @_;
935: my %returnhash;
936: map {
937: my ($name,$value)=split(/\=/,$_);
938: $returnhash{&unescape($name)}=&unescape($value);
939: } split(/\&/,$string);
940: return %returnhash;
941: }
942:
943: # -------------------------------------------------------------------Temp Store
944:
945: sub tmpreset {
946: my ($symb,$namespace,$domain,$stuname) = @_;
947: if (!$symb) {
948: $symb=&symbread();
949: if (!$symb) { $symb= $ENV{'REQUEST_URI'}; }
950: }
951: $symb=escape($symb);
952:
953: if (!$namespace) { $namespace=$ENV{'request.state'}; }
954: $namespace=~s/\//\_/g;
955: $namespace=~s/\W//g;
956:
957: #FIXME needs to do something for /pub resources
958: if (!$domain) { $domain=$ENV{'user.domain'}; }
959: if (!$stuname) { $stuname=$ENV{'user.name'}; }
960: my $path=$perlvar{'lonDaemons'}.'/tmp';
961: my %hash;
962: if (tie(%hash,'GDBM_File',
963: $path.'/tmpstore_'.$stuname.'_'.$domain.'_'.$namespace.'.db',
964: &GDBM_WRCREAT,0640)) {
965: foreach my $key (keys %hash) {
966: if ($key=~ /:$symb/) {
967: delete($hash{$key});
968: }
969: }
970: }
971: }
972:
973: sub tmpstore {
974: my ($storehash,$symb,$namespace,$domain,$stuname) = @_;
975:
976: if (!$symb) {
977: $symb=&symbread();
978: if (!$symb) { $symb= $ENV{'request.url'}; }
979: }
980: $symb=escape($symb);
981:
982: if (!$namespace) {
983: # I don't think we would ever want to store this for a course.
984: # it seems this will only be used if we don't have a course.
985: #$namespace=$ENV{'request.course.id'};
986: #if (!$namespace) {
987: $namespace=$ENV{'request.state'};
988: #}
989: }
990: $namespace=~s/\//\_/g;
991: $namespace=~s/\W//g;
992: #FIXME needs to do something for /pub resources
993: if (!$domain) { $domain=$ENV{'user.domain'}; }
994: if (!$stuname) { $stuname=$ENV{'user.name'}; }
995: my $now=time;
996: my %hash;
997: my $path=$perlvar{'lonDaemons'}.'/tmp';
998: if (tie(%hash,'GDBM_File',
999: $path.'/tmpstore_'.$stuname.'_'.$domain.'_'.$namespace.'.db',
1000: &GDBM_WRCREAT,0640)) {
1001: $hash{"version:$symb"}++;
1002: my $version=$hash{"version:$symb"};
1003: my $allkeys='';
1004: foreach my $key (keys(%$storehash)) {
1005: $allkeys.=$key.':';
1006: $hash{"$version:$symb:$key"}=$$storehash{$key};
1007: }
1008: $hash{"$version:$symb:timestamp"}=$now;
1009: $allkeys.='timestamp';
1010: $hash{"$version:keys:$symb"}=$allkeys;
1011: if (untie(%hash)) {
1012: return 'ok';
1013: } else {
1014: return "error:$!";
1015: }
1016: } else {
1017: return "error:$!";
1018: }
1019: }
1020:
1021: # -----------------------------------------------------------------Temp Restore
1022:
1023: sub tmprestore {
1024: my ($symb,$namespace,$domain,$stuname) = @_;
1025:
1026: if (!$symb) {
1027: $symb=&symbread();
1028: if (!$symb) { $symb= $ENV{'request.url'}; }
1029: }
1030: $symb=escape($symb);
1031:
1032: if (!$namespace) { $namespace=$ENV{'request.state'}; }
1033: #FIXME needs to do something for /pub resources
1034: if (!$domain) { $domain=$ENV{'user.domain'}; }
1035: if (!$stuname) { $stuname=$ENV{'user.name'}; }
1036:
1037: my %returnhash;
1038: $namespace=~s/\//\_/g;
1039: $namespace=~s/\W//g;
1040: my %hash;
1041: my $path=$perlvar{'lonDaemons'}.'/tmp';
1042: if (tie(%hash,'GDBM_File',
1043: $path.'/tmpstore_'.$stuname.'_'.$domain.'_'.$namespace.'.db',
1044: &GDBM_READER,0640)) {
1045: my $version=$hash{"version:$symb"};
1046: $returnhash{'version'}=$version;
1047: my $scope;
1048: for ($scope=1;$scope<=$version;$scope++) {
1049: my $vkeys=$hash{"$scope:keys:$symb"};
1050: my @keys=split(/:/,$vkeys);
1051: my $key;
1052: $returnhash{"$scope:keys"}=$vkeys;
1053: foreach $key (@keys) {
1054: $returnhash{"$scope:$key"}=$hash{"$scope:$symb:$key"};
1055: $returnhash{"$key"}=$hash{"$scope:$symb:$key"};
1056: }
1057: }
1058: if (!(untie(%hash))) {
1059: return "error:$!";
1060: }
1061: } else {
1062: return "error:$!";
1063: }
1064: return %returnhash;
1065: }
1066:
1067: # ----------------------------------------------------------------------- Store
1068:
1069: sub store {
1070: my ($storehash,$symb,$namespace,$domain,$stuname) = @_;
1071: my $home='';
1072:
1073: if ($stuname) { $home=&homeserver($stuname,$domain); }
1074:
1075: if (!$symb) { unless ($symb=&symbread()) { return ''; } }
1076:
1077: &devalidate($symb);
1078:
1079: $symb=escape($symb);
1080: if (!$namespace) { unless ($namespace=$ENV{'request.course.id'}) { return ''; } }
1081: if (!$domain) { $domain=$ENV{'user.domain'}; }
1082: if (!$stuname) { $stuname=$ENV{'user.name'}; }
1083: if (!$home) { $home=$ENV{'user.home'}; }
1084: my $namevalue='';
1085: map {
1086: $namevalue.=escape($_).'='.escape($$storehash{$_}).'&';
1087: } keys %$storehash;
1088: $namevalue=~s/\&$//;
1089: return reply("store:$domain:$stuname:$namespace:$symb:$namevalue","$home");
1090: }
1091:
1092: # -------------------------------------------------------------- Critical Store
1093:
1094: sub cstore {
1095: my ($storehash,$symb,$namespace,$domain,$stuname) = @_;
1096: my $home='';
1097:
1098: if ($stuname) { $home=&homeserver($stuname,$domain); }
1099:
1100: if (!$symb) { unless ($symb=&symbread()) { return ''; } }
1101:
1102: &devalidate($symb);
1103:
1104: $symb=escape($symb);
1105: if (!$namespace) { unless ($namespace=$ENV{'request.course.id'}) { return ''; } }
1106: if (!$domain) { $domain=$ENV{'user.domain'}; }
1107: if (!$stuname) { $stuname=$ENV{'user.name'}; }
1108: if (!$home) { $home=$ENV{'user.home'}; }
1109:
1110: my $namevalue='';
1111: map {
1112: $namevalue.=escape($_).'='.escape($$storehash{$_}).'&';
1113: } keys %$storehash;
1114: $namevalue=~s/\&$//;
1115: return critical("store:$domain:$stuname:$namespace:$symb:$namevalue","$home");
1116: }
1117:
1118: # --------------------------------------------------------------------- Restore
1119:
1120: sub restore {
1121: my ($symb,$namespace,$domain,$stuname) = @_;
1122: my $home='';
1123:
1124: if ($stuname) { $home=&homeserver($stuname,$domain); }
1125:
1126: if (!$symb) {
1127: unless ($symb=escape(&symbread())) { return ''; }
1128: } else {
1129: $symb=&escape($symb);
1130: }
1131: if (!$namespace) { unless ($namespace=$ENV{'request.course.id'}) { return ''; } }
1132: if (!$domain) { $domain=$ENV{'user.domain'}; }
1133: if (!$stuname) { $stuname=$ENV{'user.name'}; }
1134: if (!$home) { $home=$ENV{'user.home'}; }
1135: my $answer=&reply("restore:$domain:$stuname:$namespace:$symb","$home");
1136:
1137: my %returnhash=();
1138: map {
1139: my ($name,$value)=split(/\=/,$_);
1140: $returnhash{&unescape($name)}=&unescape($value);
1141: } split(/\&/,$answer);
1142: my $version;
1143: for ($version=1;$version<=$returnhash{'version'};$version++) {
1144: map {
1145: $returnhash{$_}=$returnhash{$version.':'.$_};
1146: } split(/\:/,$returnhash{$version.':keys'});
1147: }
1148: return %returnhash;
1149: }
1150:
1151: # ---------------------------------------------------------- Course Description
1152:
1153: sub coursedescription {
1154: my $courseid=shift;
1155: $courseid=~s/^\///;
1156: $courseid=~s/\_/\//g;
1157: my ($cdomain,$cnum)=split(/\//,$courseid);
1158: my $chome=&homeserver($cnum,$cdomain);
1159: if ($chome ne 'no_host') {
1160: my %returnhash=&dump('environment',$cdomain,$cnum);
1161: if (!exists($returnhash{'con_lost'})) {
1162: my $normalid=$cdomain.'_'.$cnum;
1163: my %envhash=();
1164: $returnhash{'home'}= $chome;
1165: $returnhash{'domain'} = $cdomain;
1166: $returnhash{'num'} = $cnum;
1167: while (my ($name,$value) = each %returnhash) {
1168: $envhash{'course.'.$normalid.'.'.$name}=$value;
1169: }
1170: $returnhash{'url'}='/res/'.declutter($returnhash{'url'});
1171: $returnhash{'fn'}=$perlvar{'lonDaemons'}.'/tmp/'.
1172: $ENV{'user.name'}.'_'.$cdomain.'_'.$cnum;
1173: $envhash{'course.'.$normalid.'.last_cache'}=time;
1174: $envhash{'course.'.$normalid.'.home'}=$chome;
1175: $envhash{'course.'.$normalid.'.domain'}=$cdomain;
1176: $envhash{'course.'.$normalid.'.num'}=$cnum;
1177: &appenv(%envhash);
1178: return %returnhash;
1179: }
1180: }
1181: return ();
1182: }
1183:
1184: # -------------------------------------------------------- Get user privileges
1185:
1186: sub rolesinit {
1187: my ($domain,$username,$authhost)=@_;
1188: my $rolesdump=reply("dump:$domain:$username:roles",$authhost);
1189: if (($rolesdump eq 'con_lost') || ($rolesdump eq '')) { return ''; }
1190: my %allroles=();
1191: my %thesepriv=();
1192: my $now=time;
1193: my $userroles="user.login.time=$now\n";
1194: my $thesestr;
1195:
1196: if ($rolesdump ne '') {
1197: map {
1198: if ($_!~/^rolesdef\&/) {
1199: my ($area,$role)=split(/=/,$_);
1200: $area=~s/\_\w\w$//;
1201: my ($trole,$tend,$tstart)=split(/_/,$role);
1202: $userroles.='user.role.'.$trole.'.'.$area.'='.
1203: $tstart.'.'.$tend."\n";
1204: if ($tend!=0) {
1205: if ($tend<$now) {
1206: $trole='';
1207: }
1208: }
1209: if ($tstart!=0) {
1210: if ($tstart>$now) {
1211: $trole='';
1212: }
1213: }
1214: if (($area ne '') && ($trole ne '')) {
1215: my $spec=$trole.'.'.$area;
1216: my ($tdummy,$tdomain,$trest)=split(/\//,$area);
1217: if ($trole =~ /^cr\//) {
1218: my ($rdummy,$rdomain,$rauthor,$rrole)=split(/\//,$trole);
1219: my $homsvr=homeserver($rauthor,$rdomain);
1220: if ($hostname{$homsvr} ne '') {
1221: my $roledef=
1222: reply("get:$rdomain:$rauthor:roles:rolesdef_$rrole",
1223: $homsvr);
1224: if (($roledef ne 'con_lost') && ($roledef ne '')) {
1225: my ($syspriv,$dompriv,$coursepriv)=
1226: split(/\_/,unescape($roledef));
1227: $allroles{'cm./'}.=':'.$syspriv;
1228: $allroles{$spec.'./'}.=':'.$syspriv;
1229: if ($tdomain ne '') {
1230: $allroles{'cm./'.$tdomain.'/'}.=':'.$dompriv;
1231: $allroles{$spec.'./'.$tdomain.'/'}.=':'.$dompriv;
1232: if ($trest ne '') {
1233: $allroles{'cm.'.$area}.=':'.$coursepriv;
1234: $allroles{$spec.'.'.$area}.=':'.$coursepriv;
1235: }
1236: }
1237: }
1238: }
1239: } else {
1240: $allroles{'cm./'}.=':'.$pr{$trole.':s'};
1241: $allroles{$spec.'./'}.=':'.$pr{$trole.':s'};
1242: if ($tdomain ne '') {
1243: $allroles{'cm./'.$tdomain.'/'}.=':'.$pr{$trole.':d'};
1244: $allroles{$spec.'./'.$tdomain.'/'}.=':'.$pr{$trole.':d'};
1245: if ($trest ne '') {
1246: $allroles{'cm.'.$area}.=':'.$pr{$trole.':c'};
1247: $allroles{$spec.'.'.$area}.=':'.$pr{$trole.':c'};
1248: }
1249: }
1250: }
1251: }
1252: }
1253: } split(/&/,$rolesdump);
1254: my $adv=0;
1255: my $author=0;
1256: map {
1257: %thesepriv=();
1258: if (($_!~/^st/) && ($_!~/^ta/) && ($_!~/^cm/)) { $adv=1; }
1259: if (($_=~/^au/) || ($_=~/^ca/)) { $author=1; }
1260: map {
1261: if ($_ ne '') {
1262: my ($privilege,$restrictions)=split(/&/,$_);
1263: if ($restrictions eq '') {
1264: $thesepriv{$privilege}='F';
1265: } else {
1266: if ($thesepriv{$privilege} ne 'F') {
1267: $thesepriv{$privilege}.=$restrictions;
1268: }
1269: }
1270: }
1271: } split(/:/,$allroles{$_});
1272: $thesestr='';
1273: map { $thesestr.=':'.$_.'&'.$thesepriv{$_}; } keys %thesepriv;
1274: $userroles.='user.priv.'.$_.'='.$thesestr."\n";
1275: } keys %allroles;
1276: $userroles.='user.adv='.$adv."\n".
1277: 'user.author='.$author."\n";
1278: $ENV{'user.adv'}=$adv;
1279: }
1280: return $userroles;
1281: }
1282:
1283: # --------------------------------------------------------------- get interface
1284:
1285: sub get {
1286: my ($namespace,$storearr,$udomain,$uname)=@_;
1287: my $items='';
1288: map {
1289: $items.=escape($_).'&';
1290: } @$storearr;
1291: $items=~s/\&$//;
1292: if (!$udomain) { $udomain=$ENV{'user.domain'}; }
1293: if (!$uname) { $uname=$ENV{'user.name'}; }
1294: my $uhome=&homeserver($uname,$udomain);
1295:
1296: my $rep=&reply("get:$udomain:$uname:$namespace:$items",$uhome);
1297: my @pairs=split(/\&/,$rep);
1298: my %returnhash=();
1299: my $i=0;
1300: map {
1301: $returnhash{$_}=unescape($pairs[$i]);
1302: $i++;
1303: } @$storearr;
1304: return %returnhash;
1305: }
1306:
1307: # --------------------------------------------------------------- del interface
1308:
1309: sub del {
1310: my ($namespace,$storearr,$udomain,$uname)=@_;
1311: my $items='';
1312: map {
1313: $items.=escape($_).'&';
1314: } @$storearr;
1315: $items=~s/\&$//;
1316: if (!$udomain) { $udomain=$ENV{'user.domain'}; }
1317: if (!$uname) { $uname=$ENV{'user.name'}; }
1318: my $uhome=&homeserver($uname,$udomain);
1319:
1320: return &reply("del:$udomain:$uname:$namespace:$items",$uhome);
1321: }
1322:
1323: # -------------------------------------------------------------- dump interface
1324:
1325: sub dump {
1326: my ($namespace,$udomain,$uname)=@_;
1327: if (!$udomain) { $udomain=$ENV{'user.domain'}; }
1328: if (!$uname) { $uname=$ENV{'user.name'}; }
1329: my $uhome=&homeserver($uname,$udomain);
1330: my $rep=reply("dump:$udomain:$uname:$namespace",$uhome);
1331: my @pairs=split(/\&/,$rep);
1332: my %returnhash=();
1333: map {
1334: my ($key,$value)=split(/=/,$_);
1335: $returnhash{unescape($key)}=unescape($value);
1336: } @pairs;
1337: return %returnhash;
1338: }
1339:
1340: # --------------------------------------------------------------- put interface
1341:
1342: sub put {
1343: my ($namespace,$storehash,$udomain,$uname)=@_;
1344: if (!$udomain) { $udomain=$ENV{'user.domain'}; }
1345: if (!$uname) { $uname=$ENV{'user.name'}; }
1346: my $uhome=&homeserver($uname,$udomain);
1347: my $items='';
1348: map {
1349: $items.=&escape($_).'='.&escape($$storehash{$_}).'&';
1350: } keys %$storehash;
1351: $items=~s/\&$//;
1352: return &reply("put:$udomain:$uname:$namespace:$items",$uhome);
1353: }
1354:
1355: # ------------------------------------------------------ critical put interface
1356:
1357: sub cput {
1358: my ($namespace,$storehash,$udomain,$uname)=@_;
1359: if (!$udomain) { $udomain=$ENV{'user.domain'}; }
1360: if (!$uname) { $uname=$ENV{'user.name'}; }
1361: my $uhome=&homeserver($uname,$udomain);
1362: my $items='';
1363: map {
1364: $items.=escape($_).'='.escape($$storehash{$_}).'&';
1365: } keys %$storehash;
1366: $items=~s/\&$//;
1367: return &critical("put:$udomain:$uname:$namespace:$items",$uhome);
1368: }
1369:
1370: # -------------------------------------------------------------- eget interface
1371:
1372: sub eget {
1373: my ($namespace,$storearr,$udomain,$uname)=@_;
1374: my $items='';
1375: map {
1376: $items.=escape($_).'&';
1377: } @$storearr;
1378: $items=~s/\&$//;
1379: if (!$udomain) { $udomain=$ENV{'user.domain'}; }
1380: if (!$uname) { $uname=$ENV{'user.name'}; }
1381: my $uhome=&homeserver($uname,$udomain);
1382: my $rep=&reply("eget:$udomain:$uname:$namespace:$items",$uhome);
1383: my @pairs=split(/\&/,$rep);
1384: my %returnhash=();
1385: my $i=0;
1386: map {
1387: $returnhash{$_}=unescape($pairs[$i]);
1388: $i++;
1389: } @$storearr;
1390: return %returnhash;
1391: }
1392:
1393: # ------------------------------------------------- Check for a user privilege
1394:
1395: sub allowed {
1396: my ($priv,$uri)=@_;
1397:
1398: my $orguri=$uri;
1399: $uri=&declutter($uri);
1400:
1401: # Free bre access to adm and meta resources
1402:
1403: if ((($uri=~/^adm\//) || ($uri=~/\.meta$/)) && ($priv eq 'bre')) {
1404: return 'F';
1405: }
1406:
1407: # Free bre to public access
1408:
1409: if ($priv eq 'bre') {
1410: if (&metadata($uri,'copyright') eq 'public') { return 'F'; }
1411: }
1412:
1413: my $thisallowed='';
1414: my $statecond=0;
1415: my $courseprivid='';
1416:
1417: # Course
1418:
1419: if ($ENV{'user.priv.'.$ENV{'request.role'}.'./'}=~/$priv\&([^\:]*)/) {
1420: $thisallowed.=$1;
1421: }
1422:
1423: # Domain
1424:
1425: if ($ENV{'user.priv.'.$ENV{'request.role'}.'./'.(split(/\//,$uri))[0].'/'}
1426: =~/$priv\&([^\:]*)/) {
1427: $thisallowed.=$1;
1428: }
1429:
1430: # Course: uri itself is a course
1431: my $courseuri=$uri;
1432: $courseuri=~s/\_(\d)/\/$1/;
1433: $courseuri=~s/^([^\/])/\/$1/;
1434:
1435: if ($ENV{'user.priv.'.$ENV{'request.role'}.'.'.$courseuri}
1436: =~/$priv\&([^\:]*)/) {
1437: $thisallowed.=$1;
1438: }
1439:
1440: # Full access at system, domain or course-wide level? Exit.
1441:
1442: if ($thisallowed=~/F/) {
1443: return 'F';
1444: }
1445:
1446: # If this is generating or modifying users, exit with special codes
1447:
1448: if (':csu:cdc:ccc:cin:cta:cep:ccr:cst:cad:cli:cau:cdg:cca:'=~/\:$priv\:/) {
1449: return $thisallowed;
1450: }
1451: #
1452: # Gathered so far: system, domain and course wide privileges
1453: #
1454: # Course: See if uri or referer is an individual resource that is part of
1455: # the course
1456:
1457: if ($ENV{'request.course.id'}) {
1458: $courseprivid=$ENV{'request.course.id'};
1459: if ($ENV{'request.course.sec'}) {
1460: $courseprivid.='/'.$ENV{'request.course.sec'};
1461: }
1462: $courseprivid=~s/\_/\//;
1463: my $checkreferer=1;
1464: my @uriparts=split(/\//,$uri);
1465: my $filename=$uriparts[$#uriparts];
1466: my $pathname=$uri;
1467: $pathname=~s/\/$filename$//;
1468: if ($ENV{'acc.res.'.$ENV{'request.course.id'}.'.'.$pathname}=~
1469: /\&$filename\:([\d\|]+)\&/) {
1470: $statecond=$1;
1471: if ($ENV{'user.priv.'.$ENV{'request.role'}.'./'.$courseprivid}
1472: =~/$priv\&([^\:]*)/) {
1473: $thisallowed.=$1;
1474: $checkreferer=0;
1475: }
1476: }
1477:
1478: if ($checkreferer) {
1479: my $refuri=$ENV{'httpref.'.$orguri};
1480:
1481: unless ($refuri) {
1482: map {
1483: if ($_=~/^httpref\..*\*/) {
1484: my $pattern=$_;
1485: $pattern=~s/^httpref\.\/res\///;
1486: $pattern=~s/\*/\[\^\/\]\+/g;
1487: $pattern=~s/\//\\\//g;
1488: if ($orguri=~/$pattern/) {
1489: $refuri=$ENV{$_};
1490: }
1491: }
1492: } keys %ENV;
1493: }
1494: if ($refuri) {
1495: $refuri=&declutter($refuri);
1496: my @uriparts=split(/\//,$refuri);
1497: my $filename=$uriparts[$#uriparts];
1498: my $pathname=$refuri;
1499: $pathname=~s/\/$filename$//;
1500: if ($ENV{'acc.res.'.$ENV{'request.course.id'}.'.'.$pathname}=~
1501: /\&$filename\:([\d\|]+)\&/) {
1502: my $refstatecond=$1;
1503: if ($ENV{'user.priv.'.$ENV{'request.role'}.'./'.$courseprivid}
1504: =~/$priv\&([^\:]*)/) {
1505: $thisallowed.=$1;
1506: $uri=$refuri;
1507: $statecond=$refstatecond;
1508: }
1509: }
1510: }
1511: }
1512: }
1513:
1514: #
1515: # Gathered now: all privileges that could apply, and condition number
1516: #
1517: #
1518: # Full or no access?
1519: #
1520:
1521: if ($thisallowed=~/F/) {
1522: return 'F';
1523: }
1524:
1525: unless ($thisallowed) {
1526: return '';
1527: }
1528:
1529: # Restrictions exist, deal with them
1530: #
1531: # C:according to course preferences
1532: # R:according to resource settings
1533: # L:unless locked
1534: # X:according to user session state
1535: #
1536:
1537: # Possibly locked functionality, check all courses
1538: # Locks might take effect only after 10 minutes cache expiration for other
1539: # courses, and 2 minutes for current course
1540:
1541: my $envkey;
1542: if ($thisallowed=~/L/) {
1543: foreach $envkey (keys %ENV) {
1544: if ($envkey=~/^user\.role\.(st|ta)\.([^\.]*)/) {
1545: my $courseid=$2;
1546: my $roleid=$1.'.'.$2;
1547: $courseid=~s/^\///;
1548: my $expiretime=600;
1549: if ($ENV{'request.role'} eq $roleid) {
1550: $expiretime=120;
1551: }
1552: my ($cdom,$cnum,$csec)=split(/\//,$courseid);
1553: my $prefix='course.'.$cdom.'_'.$cnum.'.';
1554: if ((time-$ENV{$prefix.'last_cache'})>$expiretime) {
1555: &coursedescription($courseid);
1556: }
1557: if (($ENV{$prefix.'res.'.$uri.'.lock.sections'}=~/\,$csec\,/)
1558: || ($ENV{$prefix.'res.'.$uri.'.lock.sections'} eq 'all')) {
1559: if ($ENV{$prefix.'res.'.$uri.'.lock.expire'}>time) {
1560: &log($ENV{'user.domain'},$ENV{'user.name'},
1561: $ENV{'user.host'},
1562: 'Locked by res: '.$priv.' for '.$uri.' due to '.
1563: $cdom.'/'.$cnum.'/'.$csec.' expire '.
1564: $ENV{$prefix.'priv.'.$priv.'.lock.expire'});
1565: return '';
1566: }
1567: }
1568: if (($ENV{$prefix.'priv.'.$priv.'.lock.sections'}=~/\,$csec\,/)
1569: || ($ENV{$prefix.'priv.'.$priv.'.lock.sections'} eq 'all')) {
1570: if ($ENV{'priv.'.$priv.'.lock.expire'}>time) {
1571: &log($ENV{'user.domain'},$ENV{'user.name'},
1572: $ENV{'user.host'},
1573: 'Locked by priv: '.$priv.' for '.$uri.' due to '.
1574: $cdom.'/'.$cnum.'/'.$csec.' expire '.
1575: $ENV{$prefix.'priv.'.$priv.'.lock.expire'});
1576: return '';
1577: }
1578: }
1579: }
1580: }
1581: }
1582:
1583: #
1584: # Rest of the restrictions depend on selected course
1585: #
1586:
1587: unless ($ENV{'request.course.id'}) {
1588: return '1';
1589: }
1590:
1591: #
1592: # Now user is definitely in a course
1593: #
1594:
1595:
1596: # Course preferences
1597:
1598: if ($thisallowed=~/C/) {
1599: my $rolecode=(split(/\./,$ENV{'request.role'}))[0];
1600: if ($ENV{'course.'.$ENV{'request.course.id'}.'.'.$priv.'.roles.denied'}
1601: =~/\,$rolecode\,/) {
1602: &log($ENV{'user.domain'},$ENV{'user.name'},$ENV{'user.host'},
1603: 'Denied by role: '.$priv.' for '.$uri.' as '.$rolecode.' in '.
1604: $ENV{'request.course.id'});
1605: return '';
1606: }
1607: }
1608:
1609: # Resource preferences
1610:
1611: if ($thisallowed=~/R/) {
1612: my $rolecode=(split(/\./,$ENV{'request.role'}))[0];
1613: my $filename=$perlvar{'lonDocRoot'}.'/res/'.$uri.'.meta';
1614: if (-e $filename) {
1615: my @content;
1616: {
1617: my $fh=Apache::File->new($filename);
1618: @content=<$fh>;
1619: }
1620: if (join('',@content)=~
1621: /\<roledeny[^\>]*\>[^\<]*$rolecode[^\<]*\<\/roledeny\>/) {
1622: &log($ENV{'user.domain'},$ENV{'user.name'},$ENV{'user.host'},
1623: 'Denied by role: '.$priv.' for '.$uri.' as '.$rolecode);
1624: return '';
1625:
1626: }
1627: }
1628: }
1629:
1630: # Restricted by state?
1631:
1632: if ($thisallowed=~/X/) {
1633: if (&condval($statecond)) {
1634: return '2';
1635: } else {
1636: return '';
1637: }
1638: }
1639:
1640: return 'F';
1641: }
1642:
1643: # ----------------------------------------------------------------- Define Role
1644:
1645: sub definerole {
1646: if (allowed('mcr','/')) {
1647: my ($rolename,$sysrole,$domrole,$courole)=@_;
1648: map {
1649: my ($crole,$cqual)=split(/\&/,$_);
1650: if ($pr{'cr:s'}!~/$crole/) { return "refused:s:$crole"; }
1651: if ($pr{'cr:s'}=~/$crole\&/) {
1652: if ($pr{'cr:s'}!~/$crole\&\w*$cqual/) {
1653: return "refused:s:$crole&$cqual";
1654: }
1655: }
1656: } split('/',$sysrole);
1657: map {
1658: my ($crole,$cqual)=split(/\&/,$_);
1659: if ($pr{'cr:d'}!~/$crole/) { return "refused:d:$crole"; }
1660: if ($pr{'cr:d'}=~/$crole\&/) {
1661: if ($pr{'cr:d'}!~/$crole\&\w*$cqual/) {
1662: return "refused:d:$crole&$cqual";
1663: }
1664: }
1665: } split('/',$domrole);
1666: map {
1667: my ($crole,$cqual)=split(/\&/,$_);
1668: if ($pr{'cr:c'}!~/$crole/) { return "refused:c:$crole"; }
1669: if ($pr{'cr:c'}=~/$crole\&/) {
1670: if ($pr{'cr:c'}!~/$crole\&\w*$cqual/) {
1671: return "refused:c:$crole&$cqual";
1672: }
1673: }
1674: } split('/',$courole);
1675: my $command="encrypt:rolesput:$ENV{'user.domain'}:$ENV{'user.name'}:".
1676: "$ENV{'user.domain'}:$ENV{'user.name'}:".
1677: "rolesdef_$rolename=".
1678: escape($sysrole.'_'.$domrole.'_'.$courole);
1679: return reply($command,$ENV{'user.home'});
1680: } else {
1681: return 'refused';
1682: }
1683: }
1684:
1685: # ---------------- Make a metadata query against the network of library servers
1686:
1687: sub metadata_query {
1688: my ($query,$custom,$customshow)=@_;
1689: my %rhash;
1690: for my $server (keys %libserv) {
1691: unless ($custom or $customshow) {
1692: my $reply=&reply("querysend:".&escape($query),$server);
1693: $rhash{$server}=$reply;
1694: }
1695: else {
1696: my $reply=&reply("querysend:".&escape($query).':'.
1697: &escape($custom).':'.&escape($customshow),
1698: $server);
1699: $rhash{$server}=$reply;
1700: }
1701: }
1702: return \%rhash;
1703: }
1704:
1705: # ------------------------------------------------------------------ Plain Text
1706:
1707: sub plaintext {
1708: my $short=shift;
1709: return $prp{$short};
1710: }
1711:
1712: # ------------------------------------------------------------------ Plain Text
1713:
1714: sub fileembstyle {
1715: my $ending=shift;
1716: return $fe{$ending};
1717: }
1718:
1719: # ------------------------------------------------------------ Description Text
1720:
1721: sub filedescription {
1722: my $ending=shift;
1723: return $fd{$ending};
1724: }
1725:
1726: # ----------------------------------------------------------------- Assign Role
1727:
1728: sub assignrole {
1729: my ($udom,$uname,$url,$role,$end,$start)=@_;
1730: my $mrole;
1731: if ($role =~ /^cr\//) {
1732: unless (&allowed('ccr',$url)) {
1733: &logthis('Refused custom assignrole: '.
1734: $udom.' '.$uname.' '.$url.' '.$role.' '.$end.' '.$start.' by '.
1735: $ENV{'user.name'}.' at '.$ENV{'user.domain'});
1736: return 'refused';
1737: }
1738: $mrole='cr';
1739: } else {
1740: my $cwosec=$url;
1741: $cwosec=~s/^\/(\w+)\/(\w+)\/.*/$1\/$2/;
1742: unless (&allowed('c'.$role,$cwosec)) {
1743: &logthis('Refused assignrole: '.
1744: $udom.' '.$uname.' '.$url.' '.$role.' '.$end.' '.$start.' by '.
1745: $ENV{'user.name'}.' at '.$ENV{'user.domain'});
1746: return 'refused';
1747: }
1748: $mrole=$role;
1749: }
1750: my $command="encrypt:rolesput:$ENV{'user.domain'}:$ENV{'user.name'}:".
1751: "$udom:$uname:$url".'_'."$mrole=$role";
1752: if ($end) { $command.='_'.$end; }
1753: if ($start) {
1754: if ($end) {
1755: $command.='_'.$start;
1756: } else {
1757: $command.='_0_'.$start;
1758: }
1759: }
1760: return &reply($command,&homeserver($uname,$udom));
1761: }
1762:
1763: # -------------------------------------------------- Modify user authentication
1764: sub modifyuserauth {
1765: my ($udom,$uname,$umode,$upass)=@_;
1766: my $uhome=&homeserver($uname,$udom);
1767: &logthis('Call to modify user authentication'.$udom.', '.$uname.', '.
1768: $umode.' by '.$ENV{'user.name'}.' at '.$ENV{'user.domain'});
1769: my $reply=&reply('encrypt:changeuserauth:'.$udom.':'.$uname.':'.$umode.':'.
1770: &escape($upass),$uhome);
1771: unless ($reply eq 'ok') {
1772: return 'error: '.$reply;
1773: }
1774: return 'ok';
1775: }
1776:
1777: # --------------------------------------------------------------- Modify a user
1778:
1779:
1780: sub modifyuser {
1781: my ($udom,$uname,$uid,$umode,$upass,$first,$middle,$last,$gene)=@_;
1782: &logthis('Call to modify user '.$udom.', '.$uname.', '.$uid.', '.
1783: $umode.', '.$first.', '.$middle.', '.
1784: $last.', '.$gene.' by '.
1785: $ENV{'user.name'}.' at '.$ENV{'user.domain'});
1786: my $uhome=&homeserver($uname,$udom);
1787: # ----------------------------------------------------------------- Create User
1788: if (($uhome eq 'no_host') && ($umode) && ($upass)) {
1789: my $unhome='';
1790: if ($ENV{'course.'.$ENV{'request.course.id'}.'.domain'} eq $udom) {
1791: $unhome=$ENV{'course.'.$ENV{'request.course.id'}.'.home'};
1792: } else {
1793: my $tryserver;
1794: my $loadm=10000000;
1795: foreach $tryserver (keys %libserv) {
1796: if ($hostdom{$tryserver} eq $udom) {
1797: my $answer=reply('load',$tryserver);
1798: if (($answer=~/\d+/) && ($answer<$loadm)) {
1799: $loadm=$answer;
1800: $unhome=$tryserver;
1801: }
1802: }
1803: }
1804: }
1805: if (($unhome eq '') || ($unhome eq 'no_host')) {
1806: return 'error: find home';
1807: }
1808: my $reply=&reply('encrypt:makeuser:'.$udom.':'.$uname.':'.$umode.':'.
1809: &escape($upass),$unhome);
1810: unless ($reply eq 'ok') {
1811: return 'error: '.$reply;
1812: }
1813: $uhome=&homeserver($uname,$udom);
1814: if (($uhome eq '') || ($uhome eq 'no_host') || ($uhome ne $unhome)) {
1815: return 'error: verify home';
1816: }
1817: }
1818: # ---------------------------------------------------------------------- Add ID
1819: if ($uid) {
1820: $uid=~tr/A-Z/a-z/;
1821: my %uidhash=&idrget($udom,$uname);
1822: if (($uidhash{$uname}) && ($uidhash{$uname}!~/error\:/)) {
1823: unless ($uid eq $uidhash{$uname}) {
1824: return 'error: mismatch '.$uidhash{$uname}.' versus '.$uid;
1825: }
1826: } else {
1827: &idput($udom,($uname => $uid));
1828: }
1829: }
1830: # -------------------------------------------------------------- Add names, etc
1831: my %names=&get('environment',
1832: ['firstname','middlename','lastname','generation'],
1833: $udom,$uname);
1834: if ($first) { $names{'firstname'} = $first; }
1835: if ($middle) { $names{'middlename'} = $middle; }
1836: if ($last) { $names{'lastname'} = $last; }
1837: if ($gene) { $names{'generation'} = $gene; }
1838: my $reply = &put('environment', \%names, $udom,$uname);
1839: if ($reply ne 'ok') { return 'error: '.$reply; }
1840: &logthis('Success modifying user '.$udom.', '.$uname.', '.$uid.', '.
1841: $umode.', '.$first.', '.$middle.', '.
1842: $last.', '.$gene.' by '.
1843: $ENV{'user.name'}.' at '.$ENV{'user.domain'});
1844: return 'ok';
1845: }
1846:
1847: # -------------------------------------------------------------- Modify student
1848:
1849: sub modifystudent {
1850: my ($udom,$uname,$uid,$umode,$upass,$first,$middle,$last,$gene,$usec,
1851: $end,$start)=@_;
1852: my $cid='';
1853: unless ($cid=$ENV{'request.course.id'}) {
1854: return 'not_in_class';
1855: }
1856: # --------------------------------------------------------------- Make the user
1857: my $reply=&modifyuser
1858: ($udom,$uname,$uid,$umode,$upass,$first,$middle,$last,$gene);
1859: unless ($reply eq 'ok') { return $reply; }
1860: my $uhome=&homeserver($uname,$udom);
1861: if (($uhome eq '') || ($uhome eq 'no_host')) {
1862: return 'error: no such user';
1863: }
1864: # -------------------------------------------------- Add student to course list
1865: $reply=critical('put:'.$ENV{'course.'.$cid.'.domain'}.':'.
1866: $ENV{'course.'.$cid.'.num'}.':classlist:'.
1867: &escape($uname.':'.$udom).'='.
1868: &escape($end.':'.$start),
1869: $ENV{'course.'.$cid.'.home'});
1870: unless (($reply eq 'ok') || ($reply eq 'delayed')) {
1871: return 'error: '.$reply;
1872: }
1873: # ---------------------------------------------------- Add student role to user
1874: my $uurl='/'.$cid;
1875: $uurl=~s/\_/\//g;
1876: if ($usec) {
1877: $uurl.='/'.$usec;
1878: }
1879: return &assignrole($udom,$uname,$uurl,'st',$end,$start);
1880: }
1881:
1882: # ------------------------------------------------- Write to course preferences
1883:
1884: sub writecoursepref {
1885: my ($courseid,%prefs)=@_;
1886: $courseid=~s/^\///;
1887: $courseid=~s/\_/\//g;
1888: my ($cdomain,$cnum)=split(/\//,$courseid);
1889: my $chome=homeserver($cnum,$cdomain);
1890: if (($chome eq '') || ($chome eq 'no_host')) {
1891: return 'error: no such course';
1892: }
1893: my $cstring='';
1894: map {
1895: $cstring.=escape($_).'='.escape($prefs{$_}).'&';
1896: } keys %prefs;
1897: $cstring=~s/\&$//;
1898: return reply('put:'.$cdomain.':'.$cnum.':environment:'.$cstring,$chome);
1899: }
1900:
1901: # ---------------------------------------------------------- Make/modify course
1902:
1903: sub createcourse {
1904: my ($udom,$description,$url)=@_;
1905: $url=&declutter($url);
1906: my $cid='';
1907: unless (&allowed('ccc',$ENV{'user.domain'})) {
1908: return 'refused';
1909: }
1910: unless ($udom eq $ENV{'user.domain'}) {
1911: return 'refused';
1912: }
1913: # ------------------------------------------------------------------- Create ID
1914: my $uname=substr($$.time,0,5).unpack("H8",pack("I32",time)).
1915: unpack("H2",pack("I32",int(rand(255)))).$perlvar{'lonHostID'};
1916: # ----------------------------------------------- Make sure that does not exist
1917: my $uhome=&homeserver($uname,$udom);
1918: unless (($uhome eq '') || ($uhome eq 'no_host')) {
1919: $uname=substr($$.time,0,5).unpack("H8",pack("I32",time)).
1920: unpack("H2",pack("I32",int(rand(255)))).$perlvar{'lonHostID'};
1921: $uhome=&homeserver($uname,$udom);
1922: unless (($uhome eq '') || ($uhome eq 'no_host')) {
1923: return 'error: unable to generate unique course-ID';
1924: }
1925: }
1926: # ------------------------------------------------------------- Make the course
1927: my $reply=&reply('encrypt:makeuser:'.$udom.':'.$uname.':none::',
1928: $ENV{'user.home'});
1929: unless ($reply eq 'ok') { return 'error: '.$reply; }
1930: $uhome=&homeserver($uname,$udom);
1931: if (($uhome eq '') || ($uhome eq 'no_host')) {
1932: return 'error: no such course';
1933: }
1934: &writecoursepref($udom.'_'.$uname,
1935: ('description' => $description,
1936: 'url' => $url));
1937: return '/'.$udom.'/'.$uname;
1938: }
1939:
1940: # ---------------------------------------------------------- Assign Custom Role
1941:
1942: sub assigncustomrole {
1943: my ($udom,$uname,$url,$rdom,$rnam,$rolename,$end,$start)=@_;
1944: return &assignrole($udom,$uname,$url,'cr/'.$rdom.'/'.$rnam.'/'.$rolename,
1945: $end,$start);
1946: }
1947:
1948: # ----------------------------------------------------------------- Revoke Role
1949:
1950: sub revokerole {
1951: my ($udom,$uname,$url,$role)=@_;
1952: my $now=time;
1953: return &assignrole($udom,$uname,$url,$role,$now);
1954: }
1955:
1956: # ---------------------------------------------------------- Revoke Custom Role
1957:
1958: sub revokecustomrole {
1959: my ($udom,$uname,$url,$rdom,$rnam,$rolename)=@_;
1960: my $now=time;
1961: return &assigncustomrole($udom,$uname,$url,$rdom,$rnam,$rolename,$now);
1962: }
1963:
1964: # ------------------------------------------------------------ Directory lister
1965:
1966: sub dirlist {
1967: my $uri=shift;
1968: $uri=~s/^\///;
1969: $uri=~s/\/$//;
1970: my ($res,$udom,$uname,@rest)=split(/\//,$uri);
1971: if ($udom) {
1972: if ($uname) {
1973: my $listing=reply('ls:'.$perlvar{'lonDocRoot'}.'/'.$uri,
1974: homeserver($uname,$udom));
1975: return split(/:/,$listing);
1976: } else {
1977: my $tryserver;
1978: my %allusers=();
1979: foreach $tryserver (keys %libserv) {
1980: if ($hostdom{$tryserver} eq $udom) {
1981: my $listing=reply('ls:'.$perlvar{'lonDocRoot'}.'/res/'.$udom,
1982: $tryserver);
1983: if (($listing ne 'no_such_dir') && ($listing ne 'empty')
1984: && ($listing ne 'con_lost')) {
1985: map {
1986: my ($entry,@stat)=split(/&/,$_);
1987: $allusers{$entry}=1;
1988: } split(/:/,$listing);
1989: }
1990: }
1991: }
1992: my $alluserstr='';
1993: map {
1994: $alluserstr.=$_.'&user:';
1995: } sort keys %allusers;
1996: $alluserstr=~s/:$//;
1997: return split(/:/,$alluserstr);
1998: }
1999: } else {
2000: my $tryserver;
2001: my %alldom=();
2002: foreach $tryserver (keys %libserv) {
2003: $alldom{$hostdom{$tryserver}}=1;
2004: }
2005: my $alldomstr='';
2006: map {
2007: $alldomstr.=$perlvar{'lonDocRoot'}.'/res/'.$_.'&domain:';
2008: } sort keys %alldom;
2009: $alldomstr=~s/:$//;
2010: return split(/:/,$alldomstr);
2011: }
2012: }
2013:
2014: # -------------------------------------------------------- Value of a Condition
2015:
2016: sub directcondval {
2017: my $number=shift;
2018: if ($ENV{'user.state.'.$ENV{'request.course.id'}}) {
2019: return substr($ENV{'user.state.'.$ENV{'request.course.id'}},$number,1);
2020: } else {
2021: return 2;
2022: }
2023: }
2024:
2025: sub condval {
2026: my $condidx=shift;
2027: my $result=0;
2028: my $allpathcond='';
2029: map {
2030: if (defined($ENV{'acc.cond.'.$ENV{'request.course.id'}.'.'.$_})) {
2031: $allpathcond.=
2032: '('.$ENV{'acc.cond.'.$ENV{'request.course.id'}.'.'.$_}.')|';
2033: }
2034: } split(/\|/,$condidx);
2035: $allpathcond=~s/\|$//;
2036: if ($ENV{'request.course.id'}) {
2037: if ($allpathcond) {
2038: my $operand='|';
2039: my @stack;
2040: map {
2041: if ($_ eq '(') {
2042: push @stack,($operand,$result)
2043: } elsif ($_ eq ')') {
2044: my $before=pop @stack;
2045: if (pop @stack eq '&') {
2046: $result=$result>$before?$before:$result;
2047: } else {
2048: $result=$result>$before?$result:$before;
2049: }
2050: } elsif (($_ eq '&') || ($_ eq '|')) {
2051: $operand=$_;
2052: } else {
2053: my $new=directcondval($_);
2054: if ($operand eq '&') {
2055: $result=$result>$new?$new:$result;
2056: } else {
2057: $result=$result>$new?$result:$new;
2058: }
2059: }
2060: } ($allpathcond=~/(\d+|\(|\)|\&|\|)/g);
2061: }
2062: }
2063: return $result;
2064: }
2065:
2066: # --------------------------------------------------------- Value of a Variable
2067:
2068: sub EXT {
2069: my ($varname,$symbparm)=@_;
2070: unless ($varname) { return ''; }
2071: my ($realm,$space,$qualifier,@therest)=split(/\./,$varname);
2072: my $rest;
2073: if ($therest[0]) {
2074: $rest=join('.',@therest);
2075: } else {
2076: $rest='';
2077: }
2078: my $qualifierrest=$qualifier;
2079: if ($rest) { $qualifierrest.='.'.$rest; }
2080: my $spacequalifierrest=$space;
2081: if ($qualifierrest) { $spacequalifierrest.='.'.$qualifierrest; }
2082: if ($realm eq 'user') {
2083: # --------------------------------------------------------------- user.resource
2084: if ($space eq 'resource') {
2085: my %restored=&restore();
2086: return $restored{$qualifierrest};
2087: # ----------------------------------------------------------------- user.access
2088: } elsif ($space eq 'access') {
2089: return &allowed($qualifier,$rest);
2090: # ------------------------------------------ user.preferences, user.environment
2091: } elsif (($space eq 'preferences') || ($space eq 'environment')) {
2092: return $ENV{join('.',('environment',$qualifierrest))};
2093: # ----------------------------------------------------------------- user.course
2094: } elsif ($space eq 'course') {
2095: return $ENV{join('.',('request.course',$qualifier))};
2096: # ------------------------------------------------------------------- user.role
2097: } elsif ($space eq 'role') {
2098: my ($role,$where)=split(/\./,$ENV{'request.role'});
2099: if ($qualifier eq 'value') {
2100: return $role;
2101: } elsif ($qualifier eq 'extent') {
2102: return $where;
2103: }
2104: # ----------------------------------------------------------------- user.domain
2105: } elsif ($space eq 'domain') {
2106: return $ENV{'user.domain'};
2107: # ------------------------------------------------------------------- user.name
2108: } elsif ($space eq 'name') {
2109: return $ENV{'user.name'};
2110: # ---------------------------------------------------- Any other user namespace
2111: } else {
2112: my $item=($rest)?$qualifier.'.'.$rest:$qualifier;
2113: my %reply=&get($space,[$item]);
2114: return $reply{$item};
2115: }
2116: } elsif ($realm eq 'request') {
2117: # ------------------------------------------------------------- request.browser
2118: if ($space eq 'browser') {
2119: return $ENV{'browser.'.$qualifier};
2120: # ------------------------------------------------------------ request.filename
2121: } else {
2122: return $ENV{'request.'.$spacequalifierrest};
2123: }
2124: } elsif ($realm eq 'course') {
2125: # ---------------------------------------------------------- course.description
2126: return $ENV{'course.'.$ENV{'request.course.id'}.'.'.
2127: $spacequalifierrest};
2128: } elsif ($realm eq 'resource') {
2129: if ($ENV{'request.course.id'}) {
2130:
2131: # print '<br>'.$space.' - '.$qualifier.' - '.$spacequalifierrest;
2132:
2133:
2134: # ----------------------------------------------------- Cascading lookup scheme
2135: my $symbp;
2136: if ($symbparm) {
2137: $symbp=$symbparm;
2138: } else {
2139: $symbp=&symbread();
2140: }
2141: my $mapp=(split(/\_\_\_/,$symbp))[0];
2142:
2143: my $symbparm=$symbp.'.'.$spacequalifierrest;
2144: my $mapparm=$mapp.'___(all).'.$spacequalifierrest;
2145:
2146: my $seclevel=
2147: $ENV{'request.course.id'}.'.['.
2148: $ENV{'request.course.sec'}.'].'.$spacequalifierrest;
2149: my $seclevelr=
2150: $ENV{'request.course.id'}.'.['.
2151: $ENV{'request.course.sec'}.'].'.$symbparm;
2152: my $seclevelm=
2153: $ENV{'request.course.id'}.'.['.
2154: $ENV{'request.course.sec'}.'].'.$mapparm;
2155:
2156: my $courselevel=
2157: $ENV{'request.course.id'}.'.'.$spacequalifierrest;
2158: my $courselevelr=
2159: $ENV{'request.course.id'}.'.'.$symbparm;
2160: my $courselevelm=
2161: $ENV{'request.course.id'}.'.'.$mapparm;
2162:
2163: # ----------------------------------------------------------- first, check user
2164: my %resourcedata=get('resourcedata',
2165: [$courselevelr,$courselevelm,$courselevel]);
2166: if (($resourcedata{$courselevelr}!~/^error\:/) &&
2167: ($resourcedata{$courselevelr}!~/^con_lost/)) {
2168:
2169: if ($resourcedata{$courselevelr}) {
2170: return $resourcedata{$courselevelr}; }
2171: if ($resourcedata{$courselevelm}) {
2172: return $resourcedata{$courselevelm}; }
2173: if ($resourcedata{$courselevel}) { return $resourcedata{$courselevel}; }
2174:
2175: } else {
2176: if ($resourcedata{$courselevelr}!~/No such file/) {
2177: &logthis("<font color=blue>WARNING:".
2178: " Trying to get resource data for ".$ENV{'user.name'}." at "
2179: .$ENV{'user.domain'}.": ".$resourcedata{$courselevelr}.
2180: "</font>");
2181: }
2182: }
2183:
2184: # -------------------------------------------------------- second, check course
2185:
2186: my $reply=&reply('get:'.
2187: $ENV{'course.'.$ENV{'request.course.id'}.'.domain'}.':'.
2188: $ENV{'course.'.$ENV{'request.course.id'}.'.num'}.
2189: ':resourcedata:'.
2190: &escape($seclevelr).'&'.&escape($seclevelm).'&'.&escape($seclevel).'&'.
2191: &escape($courselevelr).'&'.&escape($courselevelm).'&'.&escape($courselevel),
2192: $ENV{'course.'.$ENV{'request.course.id'}.'.home'});
2193: if ($reply!~/^error\:/) {
2194: map {
2195: if ($_) { return &unescape($_); }
2196: } split(/\&/,$reply);
2197: }
2198: if (($reply=~/^con_lost/) || ($reply=~/^error\:/)) {
2199: &logthis("<font color=blue>WARNING:".
2200: " Getting ".$reply." asking for ".$varname." for ".
2201: $ENV{'course.'.$ENV{'request.course.id'}.'.num'}.
2202: ' at '.
2203: $ENV{'course.'.$ENV{'request.course.id'}.'.domain'}.
2204: ' from '.
2205: $ENV{'course.'.$ENV{'request.course.id'}.'.home'}.
2206: "</font>");
2207: }
2208: # ------------------------------------------------------ third, check map parms
2209: my %parmhash=();
2210: my $thisparm='';
2211: if (tie(%parmhash,'GDBM_File',
2212: $ENV{'request.course.fn'}.'_parms.db',&GDBM_READER,0640)) {
2213: $thisparm=$parmhash{$symbparm};
2214: untie(%parmhash);
2215: }
2216: if ($thisparm) { return $thisparm; }
2217: }
2218:
2219: # --------------------------------------------- last, look in resource metadata
2220:
2221: $spacequalifierrest=~s/\./\_/;
2222: my $metadata=&metadata($ENV{'request.filename'},$spacequalifierrest);
2223: if ($metadata) { return $metadata; }
2224: $metadata=&metadata($ENV{'request.filename'},
2225: 'parameter_'.$spacequalifierrest);
2226: if ($metadata) { return $metadata; }
2227:
2228: # ------------------------------------------------------------------ Cascade up
2229:
2230: unless ($space eq '0') {
2231: my ($part,$id)=split(/\_/,$space);
2232: if ($id) {
2233: my $partgeneral=&EXT('resource.'.$part.'.'.$qualifierrest,
2234: $symbparm);
2235: if ($partgeneral) { return $partgeneral; }
2236: } else {
2237: my $resourcegeneral=&EXT('resource.0.'.$qualifierrest,
2238: $symbparm);
2239: if ($resourcegeneral) { return $resourcegeneral; }
2240: }
2241: }
2242:
2243: # ---------------------------------------------------- Any other user namespace
2244: } elsif ($realm eq 'environment') {
2245: # ----------------------------------------------------------------- environment
2246: return $ENV{'environment.'.$spacequalifierrest};
2247: } elsif ($realm eq 'system') {
2248: # ----------------------------------------------------------------- system.time
2249: if ($space eq 'time') {
2250: return time;
2251: }
2252: }
2253: return '';
2254: }
2255:
2256: # ---------------------------------------------------------------- Get metadata
2257:
2258: sub metadata {
2259: my ($uri,$what,$liburi,$prefix,$depthcount)=@_;
2260:
2261: $uri=&declutter($uri);
2262: my $filename=$uri;
2263: $uri=~s/\.meta$//;
2264: #
2265: # Is the metadata already cached?
2266: # Look at timestamp of caching
2267: # Everything is cached by the main uri, libraries are never directly cached
2268: #
2269: unless (abs($metacache{$uri.':cachedtimestamp'}-time)<600) {
2270: #
2271: # Is this a recursive call for a library?
2272: #
2273: if ($liburi) {
2274: $liburi=&declutter($liburi);
2275: $filename=$liburi;
2276: }
2277: my %metathesekeys=();
2278: unless ($filename=~/\.meta$/) { $filename.='.meta'; }
2279: my $metastring=&getfile($perlvar{'lonDocRoot'}.'/res/'.$filename);
2280: my $parser=HTML::TokeParser->new(\$metastring);
2281: my $token;
2282: undef %metathesekeys;
2283: while ($token=$parser->get_token) {
2284: if ($token->[0] eq 'S') {
2285: if (defined($token->[2]->{'package'})) {
2286: #
2287: # This is a package - get package info
2288: #
2289: my $package=$token->[2]->{'package'};
2290: my $keyroot='';
2291: if ($prefix) {
2292: $keyroot.='_'.$prefix;
2293: } else {
2294: if (defined($token->[2]->{'part'})) {
2295: $keyroot.='_'.$token->[2]->{'part'};
2296: }
2297: }
2298: if (defined($token->[2]->{'id'})) {
2299: $keyroot.='_'.$token->[2]->{'id'};
2300: }
2301: if ($metacache{$uri.':packages'}) {
2302: $metacache{$uri.':packages'}.=','.$package.$keyroot;
2303: } else {
2304: $metacache{$uri.':packages'}=$package.$keyroot;
2305: }
2306: map {
2307: if ($_=~/^$package\&/) {
2308: my ($pack,$name,$subp)=split(/\&/,$_);
2309: my $value=$packagetab{$_};
2310: my $part=$keyroot;
2311: $part=~s/^\_//;
2312: if ($subp eq 'display') {
2313: $value.=' [Part: '.$part.']';
2314: }
2315: my $unikey='parameter'.$keyroot.'_'.$name;
2316: $metathesekeys{$unikey}=1;
2317: $metacache{$uri.':'.$unikey.'.part'}=$part;
2318: unless
2319: (defined($metacache{$uri.':'.$unikey.'.'.$subp})) {
2320: $metacache{$uri.':'.$unikey.'.'.$subp}=$value;
2321: }
2322: }
2323: } keys %packagetab;
2324: } else {
2325: #
2326: # This is not a package - some other kind of start tag
2327: #
2328: my $entry=$token->[1];
2329: my $unikey;
2330: if ($entry eq 'import') {
2331: $unikey='';
2332: } else {
2333: $unikey=$entry;
2334: }
2335: if ($prefix) {
2336: $unikey.=$prefix;
2337: } else {
2338: if (defined($token->[2]->{'part'})) {
2339: $unikey.='_'.$token->[2]->{'part'};
2340: }
2341: }
2342: if (defined($token->[2]->{'id'})) {
2343: $unikey.='_'.$token->[2]->{'id'};
2344: }
2345:
2346: if ($entry eq 'import') {
2347: #
2348: # Importing a library here
2349: #
2350: if (defined($depthcount)) { $depthcount++; } else
2351: { $depthcount=0; }
2352: if ($depthcount<20) {
2353: map {
2354: $metathesekeys{$_}=1;
2355: } split(/\,/,&metadata($uri,'keys',
2356: $parser->get_text('/import'),$unikey,
2357: $depthcount));
2358: }
2359: } else {
2360:
2361: if (defined($token->[2]->{'name'})) {
2362: $unikey.='_'.$token->[2]->{'name'};
2363: }
2364: $metathesekeys{$unikey}=1;
2365: map {
2366: $metacache{$uri.':'.$unikey.'.'.$_}=$token->[2]->{$_};
2367: } @{$token->[3]};
2368: unless (
2369: $metacache{$uri.':'.$unikey}=$parser->get_text('/'.$entry)
2370: ) { $metacache{$uri.':'.$unikey}=
2371: $metacache{$uri.':'.$unikey.'.default'};
2372: }
2373: # end of not-a-package not-a-library import
2374: }
2375: # end of not-a-package start tag
2376: }
2377: # the next is the end of "start tag"
2378: }
2379: }
2380: $metacache{$uri.':keys'}=join(',',keys %metathesekeys);
2381: $metacache{$uri.':cachedtimestamp'}=time;
2382: # this is the end of "was not already recently cached
2383: }
2384: return $metacache{$uri.':'.$what};
2385: }
2386:
2387: # ------------------------------------------------- Update symbolic store links
2388:
2389: sub symblist {
2390: my ($mapname,%newhash)=@_;
2391: $mapname=declutter($mapname);
2392: my %hash;
2393: if (($ENV{'request.course.fn'}) && (%newhash)) {
2394: if (tie(%hash,'GDBM_File',$ENV{'request.course.fn'}.'_symb.db',
2395: &GDBM_WRCREAT,0640)) {
2396: map {
2397: $hash{declutter($_)}=$mapname.'___'.$newhash{$_};
2398: } keys %newhash;
2399: if (untie(%hash)) {
2400: return 'ok';
2401: }
2402: }
2403: }
2404: return 'error';
2405: }
2406:
2407: # ------------------------------------------------------ Return symb list entry
2408:
2409: sub symbread {
2410: my $thisfn=shift;
2411: unless ($thisfn) {
2412: if ($ENV{'request.symb'}) { return $ENV{'request.symb'}; }
2413: $thisfn=$ENV{'request.filename'};
2414: }
2415: $thisfn=declutter($thisfn);
2416: my %hash;
2417: my %bighash;
2418: my $syval='';
2419: if (($ENV{'request.course.fn'}) && ($thisfn)) {
2420: if (tie(%hash,'GDBM_File',$ENV{'request.course.fn'}.'_symb.db',
2421: &GDBM_READER,0640)) {
2422: $syval=$hash{$thisfn};
2423: untie(%hash);
2424: }
2425: # ---------------------------------------------------------- There was an entry
2426: if ($syval) {
2427: unless ($syval=~/\_\d+$/) {
2428: unless ($ENV{'form.request.prefix'}=~/\.(\d+)\_$/) {
2429: &appenv('request.ambiguous' => $thisfn);
2430: return '';
2431: }
2432: $syval.=$1;
2433: }
2434: } else {
2435: # ------------------------------------------------------- Was not in symb table
2436: if (tie(%bighash,'GDBM_File',$ENV{'request.course.fn'}.'.db',
2437: &GDBM_READER,0640)) {
2438: # ---------------------------------------------- Get ID(s) for current resource
2439: my $ids=$bighash{'ids_/res/'.$thisfn};
2440: unless ($ids) {
2441: $ids=$bighash{'ids_/'.$thisfn};
2442: }
2443: if ($ids) {
2444: # ------------------------------------------------------------------- Has ID(s)
2445: my @possibilities=split(/\,/,$ids);
2446: if ($#possibilities==0) {
2447: # ----------------------------------------------- There is only one possibility
2448: my ($mapid,$resid)=split(/\./,$ids);
2449: $syval=declutter($bighash{'map_id_'.$mapid}).'___'.$resid;
2450: } else {
2451: # ------------------------------------------ There is more than one possibility
2452: my $realpossible=0;
2453: map {
2454: my $file=$bighash{'src_'.$_};
2455: if (&allowed('bre',$file)) {
2456: my ($mapid,$resid)=split(/\./,$_);
2457: if ($bighash{'map_type_'.$mapid} ne 'page') {
2458: $realpossible++;
2459: $syval=declutter($bighash{'map_id_'.$mapid}).
2460: '___'.$resid;
2461: }
2462: }
2463: } @possibilities;
2464: if ($realpossible!=1) { $syval=''; }
2465: }
2466: }
2467: untie(%bighash)
2468: }
2469: }
2470: if ($syval) {
2471: return $syval.'___'.$thisfn;
2472: }
2473: }
2474: &appenv('request.ambiguous' => $thisfn);
2475: return '';
2476: }
2477:
2478: # ---------------------------------------------------------- Return random seed
2479:
2480: sub numval {
2481: my $txt=shift;
2482: $txt=~tr/A-J/0-9/;
2483: $txt=~tr/a-j/0-9/;
2484: $txt=~tr/K-T/0-9/;
2485: $txt=~tr/k-t/0-9/;
2486: $txt=~tr/U-Z/0-5/;
2487: $txt=~tr/u-z/0-5/;
2488: $txt=~s/\D//g;
2489: return int($txt);
2490: }
2491:
2492: sub rndseed {
2493: my ($symb,$courseid,$domain,$username)=@_;
2494: if (!$symb) {
2495: unless ($symb=&symbread()) { return time; }
2496: }
2497: if (!$courseid) { $courseid=$ENV{'request.course.id'};}
2498: if (!$domain) {$domain=$ENV{'user.domain'};}
2499: if (!$username) {$username=$ENV{'user.name'};}
2500: {
2501: use integer;
2502: my $symbchck=unpack("%32C*",$symb) << 27;
2503: my $symbseed=numval($symb) << 22;
2504: my $namechck=unpack("%32C*",$username) << 17;
2505: my $nameseed=numval($username) << 12;
2506: my $domainseed=unpack("%32C*",$domain) << 7;
2507: my $courseseed=unpack("%32C*",$courseid);
2508: my $num=$symbseed+$nameseed+$domainseed+$courseseed+$namechck+$symbchck;
2509: #uncommenting these lines can break things!
2510: #&Apache::lonxml::debug("$symbseed:$nameseed;$domainseed|$courseseed;$namechck:$symbchck");
2511: #&Apache::lonxml::debug("rndseed :$num:$symb");
2512: return $num;
2513: }
2514: }
2515:
2516: sub ireceipt {
2517: my ($funame,$fudom,$fucourseid,$fusymb)=@_;
2518: my $cuname=unpack("%32C*",$funame);
2519: my $cudom=unpack("%32C*",$fudom);
2520: my $cucourseid=unpack("%32C*",$fucourseid);
2521: my $cusymb=unpack("%32C*",$fusymb);
2522: my $cunique=unpack("%32C*",$perlvar{'lonReceipt'});
2523: return unpack("%32C*",$perlvar{'lonHostID'}).'-'.
2524: ($cunique%$cuname+
2525: $cunique%$cudom+
2526: $cusymb%$cuname+
2527: $cusymb%$cudom+
2528: $cucourseid%$cuname+
2529: $cucourseid%$cudom);
2530: }
2531:
2532: sub receipt {
2533: return &ireceipt($ENV{'user.name'},$ENV{'user.domain'},
2534: $ENV{'request.course.id'},&symbread());
2535: }
2536:
2537: # ------------------------------------------------------------ Serves up a file
2538: # returns either the contents of the file or a -1
2539: sub getfile {
2540: my $file=shift;
2541: &repcopy($file);
2542: if (! -e $file ) { return -1; };
2543: my $fh=Apache::File->new($file);
2544: my $a='';
2545: while (<$fh>) { $a .=$_; }
2546: return $a
2547: }
2548:
2549: sub filelocation {
2550: my ($dir,$file) = @_;
2551: my $location;
2552: $file=~ s/^\s*(\S+)\s*$/$1/; ## strip off leading and trailing spaces
2553: if ($file=~m:^/~:) { # is a contruction space reference
2554: $location = $file;
2555: $location =~ s:/~(.*?)/(.*):/home/$1/public_html/$2:;
2556: } else {
2557: $file=~s/^$perlvar{'lonDocRoot'}//;
2558: $file=~s:^/*res::;
2559: if ( !( $file =~ m:^/:) ) {
2560: $location = $dir. '/'.$file;
2561: } else {
2562: $location = '/home/httpd/html/res'.$file;
2563: }
2564: }
2565: $location=~s://+:/:g; # remove duplicate /
2566: while ($location=~m:/\.\./:) {$location=~ s:/[^/]+/\.\./:/:g;} #remove dir/..
2567: return $location;
2568: }
2569:
2570: sub hreflocation {
2571: my ($dir,$file)=@_;
2572: unless (($_=~/^http:\/\//i) || ($_=~/^\//)) {
2573: my $finalpath=filelocation($dir,$file);
2574: $finalpath=~s/^\/home\/httpd\/html//;
2575: return $finalpath;
2576: } else {
2577: return $file;
2578: }
2579: }
2580:
2581: # ------------------------------------------------------------- Declutters URLs
2582:
2583: sub declutter {
2584: my $thisfn=shift;
2585: $thisfn=~s/^$perlvar{'lonDocRoot'}//;
2586: $thisfn=~s/^\///;
2587: $thisfn=~s/^res\///;
2588: return $thisfn;
2589: }
2590:
2591: # -------------------------------------------------------- Escape Special Chars
2592:
2593: sub escape {
2594: my $str=shift;
2595: $str =~ s/(\W)/"%".unpack('H2',$1)/eg;
2596: return $str;
2597: }
2598:
2599: # ----------------------------------------------------- Un-Escape Special Chars
2600:
2601: sub unescape {
2602: my $str=shift;
2603: $str =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C",hex($1))/eg;
2604: return $str;
2605: }
2606:
2607: # ================================================================ Main Program
2608:
2609: BEGIN {
2610: # ------------------------------------------------------------ Read access.conf
2611: {
2612: my $config=Apache::File->new("/etc/httpd/conf/access.conf");
2613:
2614: while (my $configline=<$config>) {
2615: if ($configline =~ /PerlSetVar/) {
2616: my ($dummy,$varname,$varvalue)=split(/\s+/,$configline);
2617: chomp($varvalue);
2618: $perlvar{$varname}=$varvalue;
2619: }
2620: }
2621: }
2622:
2623: # ------------------------------------------------------------- Read hosts file
2624: {
2625: my $config=Apache::File->new("$perlvar{'lonTabDir'}/hosts.tab");
2626:
2627: while (my $configline=<$config>) {
2628: chomp($configline);
2629: my ($id,$domain,$role,$name,$ip)=split(/:/,$configline);
2630: $hostname{$id}=$name;
2631: $hostdom{$id}=$domain;
2632: $hostip{$id}=$ip;
2633: if ($role eq 'library') { $libserv{$id}=$name; }
2634: }
2635: }
2636:
2637: # ------------------------------------------------------ Read spare server file
2638: {
2639: my $config=Apache::File->new("$perlvar{'lonTabDir'}/spare.tab");
2640:
2641: while (my $configline=<$config>) {
2642: chomp($configline);
2643: if (($configline) && ($configline ne $perlvar{'lonHostID'})) {
2644: $spareid{$configline}=1;
2645: }
2646: }
2647: }
2648: # ------------------------------------------------------------ Read permissions
2649: {
2650: my $config=Apache::File->new("$perlvar{'lonTabDir'}/roles.tab");
2651:
2652: while (my $configline=<$config>) {
2653: chomp($configline);
2654: if ($configline) {
2655: my ($role,$perm)=split(/ /,$configline);
2656: if ($perm ne '') { $pr{$role}=$perm; }
2657: }
2658: }
2659: }
2660:
2661: # -------------------------------------------- Read plain texts for permissions
2662: {
2663: my $config=Apache::File->new("$perlvar{'lonTabDir'}/rolesplain.tab");
2664:
2665: while (my $configline=<$config>) {
2666: chomp($configline);
2667: if ($configline) {
2668: my ($short,$plain)=split(/:/,$configline);
2669: if ($plain ne '') { $prp{$short}=$plain; }
2670: }
2671: }
2672: }
2673:
2674: # ---------------------------------------------------------- Read package table
2675: {
2676: my $config=Apache::File->new("$perlvar{'lonTabDir'}/packages.tab");
2677:
2678: while (my $configline=<$config>) {
2679: chomp($configline);
2680: my ($short,$plain)=split(/:/,$configline);
2681: my ($pack,$name)=split(/\&/,$short);
2682: if ($plain ne '') {
2683: $packagetab{$pack.'&'.$name.'&name'}=$name;
2684: $packagetab{$short}=$plain;
2685: }
2686: }
2687: }
2688:
2689: # ------------------------------------------------------------- Read file types
2690: {
2691: my $config=Apache::File->new("$perlvar{'lonTabDir'}/filetypes.tab");
2692:
2693: while (my $configline=<$config>) {
2694: chomp($configline);
2695: my ($ending,$emb,@descr)=split(/\s+/,$configline);
2696: if ($descr[0] ne '') {
2697: $fe{$ending}=$emb;
2698: $fd{$ending}=join(' ',@descr);
2699: }
2700: }
2701: }
2702:
2703: %metacache=();
2704:
2705: $readit='done';
2706: &logtouch();
2707: &logthis('<font color=yellow>INFO: Read configuration</font>');
2708: }
2709:
2710: 1;
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>