#!/usr/bin/perl
# The LearningOnline Network with CAPA
# Generate Guest Users on NSDL Server
#
# Only works on a library server!!!
# Has to be the only library server in the domain!!!
# Should not be used on a real production server.
use strict;
my $demodomain='nsdl';
my $demohome='nsdll1';
my $admemail='lon-capa@lon-capa.org';
my $demoserver='nsdl.lon-capa.org';
my %perlvar=();
my %form=();
my %democourses=();
my $courses;
my %hostname=();
my %hostdom=();
my %domaindescription=();
my %libserv=();
my %hostip=();
my $firsturl=&unescape($ENV{'QUERY_STRING'});
unless ($firsturl=~/^\//) { $firsturl='/'.$firsturl; }
my %formfields=('afirst' => 'First Name',
'blast' => 'Last Name',
'ctitle' => 'Title',
'dinst' => 'Company/School',
'eaddr' => 'Street Address',
'fcity' => 'City, State, ZIP',
'gemail' => 'EMail Address',
'huser' => 'Desired Username',
'icomm' => 'Area of Interest/Comments');
use lib '/home/httpd/lib/perl/';
use LONCAPA::Configuration;
use IO::File;
use IO::Socket;
# ------------------------------------------------------------- Declutters URLs
sub declutter {
my $thisfn=shift;
$thisfn=~s/^$perlvar{'lonDocRoot'}//;
$thisfn=~s/^\///;
$thisfn=~s/^res\///;
$thisfn=~s/\?.+$//;
return $thisfn;
}
# -------------------------------------------------------- Escape Special Chars
sub escape {
my $str=shift;
$str =~ s/(\W)/"%".unpack('H2',$1)/eg;
return $str;
}
# ----------------------------------------------------- Un-Escape Special Chars
sub unescape {
my $str=shift;
$str =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C",hex($1))/eg;
return $str;
}
# ------------------------------------------------------------------- Log stuff
sub logthis {
my $message=shift;
my $execdir=$perlvar{'lonDaemons'};
my $now=time;
my $local=localtime($now);
open(FH,">>$execdir/logs/demo.log");
print FH "$local ($$): $message\n";
close(FH);
return 1;
}
# -------------------------------------------------- Non-critical communication
sub reply {
my ($cmd,$server)=@_;
my $peerfile="$perlvar{'lonSockDir'}/$server";
my $client=IO::Socket::UNIX->new(Peer =>"$peerfile",
Type => SOCK_STREAM,
Timeout => 10)
or return "con_lost";
print $client "$cmd\n";
my $answer=<$client>;
chomp($answer);
if (!$answer) { $answer="con_lost"; }
return $answer;
}
sub put {
my ($namespace,$storehash,$udomain,$uname)=@_;
my $uhome=&homeserver($uname,$udomain);
my $items='';
foreach (keys %$storehash) {
$items.=&escape($_).'='.&escape($$storehash{$_}).'&';
}
$items=~s/\&$//;
return &reply("put:$udomain:$uname:$namespace:$items",$uhome);
}
# ------------- Modified routines from lonnet to make a new student in a course
# ---------------------- Find the homebase for a user from domain's lib servers
sub homeserver {
my ($uname,$udom)=@_;
my $index="$uname:$udom";
my $tryserver;
foreach $tryserver (keys %libserv) {
if ($hostdom{$tryserver} eq $udom) {
my $answer=reply("home:$udom:$uname",$tryserver);
if ($answer eq 'found') {
return $tryserver;
}
}
}
return 'no_host';
}
# ----------------------------------------------------------------- Assign Role
sub assignrole {
my ($uname,$url,$role,$end,$start)=@_;
my $command="encrypt:rolesput:$demodomain:auto:".
"$demodomain:$uname:$url".'_'."$role=$role";
if ($end) { $command.='_'.$end; }
if ($start) {
if ($end) {
$command.='_'.$start;
} else {
$command.='_0_'.$start;
}
}
return &reply($command,$demohome);
}
# --------------------------------------------------------------- Modify a user
sub modifyuser {
my ($uname, $upass, $first, $last)=@_;
my $udom=$demodomain;
my $desiredhome=$demohome;
my $middle='';
my $gene='';
my $umode='internal';
$udom=~s/\W//g;
$uname=~s/\W//g;
&logthis('Call to modify user '.$udom.', '.$uname.', '.
$umode.', '.$first.', '.
$last.', '.$desiredhome);
my $uhome=$demohome;
# ----------------------------------------------------------------- Create User
if (($umode) && ($upass)) {
my $unhome=$desiredhome;
if (($unhome eq '') || ($unhome eq 'no_host')) {
return 'error: unable to find a home server for '.$uname.
' in domain '.$udom;
}
my $reply=&reply('encrypt:makeuser:'.$udom.':'.$uname.':'.$umode.':'.
&escape($upass),$unhome);
unless ($reply eq 'ok') {
return 'error makeuser '.$udom.' '.$unhome.': '.$reply;
}
$uhome=&homeserver($uname,$udom,'true');
if (($uhome eq '') || ($uhome eq 'no_host') || ($uhome ne $unhome)) {
return 'error: verify home';
}
} # End of creation of new user
# -------------------------------------------------------------- Add names, etc
my %names;
if ($first) { $names{'firstname'} = $first; }
if ($last) { $names{'lastname'} = $last; }
my $reply = &put('environment', \%names, $udom,$uname);
if ($reply ne 'ok') { return 'error: '.$reply; }
&logthis('Success modifying user '.$udom.', '.$uname.', '.
$umode.', '.$first.', '.
$last);
return 'ok';
}
# -------------------------------------------------------------- Modify student
sub modifyrole {
my ($uname,$upass,$first,$last)=@_;
my $udom=$demodomain;
my $start=time;
my $end=$start+60*60*24*100;
# --------------------------------------------------------------- Make the user
my $reply=&modifyuser($uname,$upass,$first,$last);
unless ($reply eq 'ok') { return $reply; }
# ------------------------------------------------------ Add guest role to user
return &assignrole($uname,'nsdl','dg',$end,$start);
}
sub enroll {
my ($uname,$upass,$first,$last)=@_;
&logthis("Going to enroll $uname as guest");
my $returnval.=
&modifyrole($uname,$upass,$first,$last)."<br>\n";
return $returnval;
}
# ------------------------------------------------------------- Make a password
sub genpass {
srand($$);
my @chars=('A'..'Z','a'..'z',0..9);
return join('',@chars[map{ rand @chars } (1..8)]);
}
sub inputline {
my ($name,$output)=@_;
print "\n<tr><td>$output:</td><td>".
"<input type='text' name='$name' value='$form{$name}' size='40'></td></tr>";
}
sub makeform {
print
"\n<form method='post'><p>After successful generation of a username, ".
"the access information will be emailed to you.<p><table>";
foreach (sort keys %formfields) {
&inputline($_,$formfields{$_});
}
print "</table>\n<input type='hidden' name='courses' value='$courses'>".
"<input name='submitted' value='Generate Guest User' type='submit'>".
"</form>\n";
}
# ----------------------------------------- Check the user supplied information
sub errorwrap {
my $msg=shift;
return '<font color="red">'.$msg.'</font>';
}
sub checkform {
unless ($form{'submitted'}) {
return 'Please fill out the form below to generate a guest user.';
}
# --- Sloppy check of email address
unless ($form{'gemail'}=~/^[^\@]+\@[^\@]+\.\w+$/) {
return &errorwrap('Not a valid email address');
}
# --- Check Username
$form{'huser'}=~s/[^A-Za-z0-9]//g;
$form{'huser'}=~tr/A-Z/a-z/;
$form{'huser'}=~s/^\d+//;
$form{'huser'}=substr($form{'huser'},0,10);
if (length($form{'huser'})<4) {
return &errorwrap('Username too short');
}
# see if user exists
my $reply=&reply('home:'.$demodomain.':'.$form{'huser'},$demohome);
if ($reply eq 'found') {
return &errorwrap('Username '.$form{'huser'}.' already exists.');
}
unless ($reply eq 'not_found') {
return &errorwrap('Sorry, guest logins currently not available.');
}
return 0;
}
sub sendemail {
my $upass=shift;
open(MAILOUT,"|mail '$form{'gemail'}' -c '$admemail' -s 'Your LON-CAPA Guest Access Info'");
print MAILOUT "Welcome to LON-CAPA!\n\n";
print MAILOUT "Somebody at $ENV{'REMOTE_ADDR'}, probably you, signed up\n";
print MAILOUT "for an NSDL guest login to\n\n http://$demoserver$firsturl?username=$form{'huser'}\n\n";
print MAILOUT " Username: $form{'huser'}\n Password: $upass\n\n";
print MAILOUT "\n\nThe guest access will remain valid for 100 days, and can be used for future access to NSDL resources within LON-CAPA\n\n";
print MAILOUT "Additional information provided was:\n\n";
foreach (sort keys %formfields) {
print MAILOUT ' '.$formfields{$_}.': '.$form{$_}."\n";
}
print MAILOUT "\nNSDL Guest User\n\nThank you for your interest in LON-CAPA!\n".&footer;
close MAILOUT;
}
sub footer {
return (<<'ENDFOOTER');
--
www.lon-capa.org
lon-capa@lon-capa.org
User Help: http://help.lon-capa.org/
Bugs and Enhancements: http://bugs.lon-capa.org/
Mailing Lists: http://mail.lon-capa.org/
ENDFOOTER
}
# ================================================================ Main Program
print "Content-type: text/html\n\n".
"<html><head><title>LON-CAPA NSDL Guest Signup</title></head>".
"<body bgcolor='#BBBBAA'>\n".
"<h1>Welcome to the Learning<i>Online</i> Network with CAPA NSDL Gateway Server!</h1><img src='/adm/lonDomLogos/nsdl.gif' align='right' />";
# ---------------------------------- Read loncapa_apache.conf and loncapa.conf
my $perlvarref=LONCAPA::Configuration::read_conf('loncapa_apache.conf',
'loncapa.conf');
%perlvar=%{$perlvarref};
undef $perlvarref;
delete $perlvar{'lonReceipt'}; # remove since sensitive and not needed
delete $perlvar{'lonSqlAccess'}; # remove since sensitive and not needed
# ------------------------------------------------------------- Read hosts file
{
open(CONFIG,"$perlvar{'lonTabDir'}/hosts.tab");
while (my $configline=<CONFIG>) {
chomp($configline);
my ($id,$domain,$role,$name,$ip,$domdescr)=split(/:/,$configline);
$hostname{$id}=$name;
$hostdom{$id}=$domain;
$hostip{$id}=$ip;
if ($domdescr) {
$domaindescription{$domain}=$domdescr;
}
if ($role eq 'library') { $libserv{$id}=$name; }
}
close(CONFIG);
}
# --------------------------------------------------------------- Get post vars
my $buffer;
read(STDIN,$buffer,$ENV{'CONTENT_LENGTH'});
my @pairs=split(/&/,$buffer);
my $pair;
foreach $pair (@pairs) {
my ($name,$value) = split(/=/,$pair);
$value =~ tr/+/ /;
$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C",hex($1))/eg;
$name =~ tr/+/ /;
$name =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C",hex($1))/eg;
$name =~ s/[\~\'\"]//g;
$value =~ s/[\~\'\"]//g;
$form{$name}=$value;
}
my $error=&checkform();
if ($error) {
print "<p><b>$error</b>";
&makeform();
} else {
my $upass=&genpass();
my $result=&enroll($form{'huser'},$upass,$form{'afirst'},$form{'blast'});
if ($result=~/error/) {
&logthis($result);
print &errorwrap('Sorry, guest functionality currently not available');
} else {
print "Your access information will be emailed to ".$form{'gemail'};
&sendemail($upass);
}
}
# ------------------------------------------------------------------------- End
print('<p><table bgcolor="#999999" width="100%" cellspacing="3"><tr><td bgcolor="#FFFFFF"><pre>'.&footer().'</pre></td><td bgcolor="#FFFFFF"><img src="/adm/lonIcons/SMETE_white.gif" align="right"></td></tr></table></body></html>');
1;
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>