From 1e9e22e8e9fe699e470e2157c665b275da4957a8 Mon Sep 17 00:00:00 2001 From: moodler Date: Sat, 31 Jul 2004 14:17:50 +0000 Subject: [PATCH] Added a new authentication module to connect to a First Class server! Contributed by Tortsten Anderson --- auth/fc/Readme.txt | 71 +++++++++++++++ auth/fc/config.html | 84 +++++++++++++++++ auth/fc/fcFPP.php | 218 ++++++++++++++++++++++++++++++++++++++++++++ auth/fc/lib.php | 154 +++++++++++++++++++++++++++++++ 4 files changed, 527 insertions(+) create mode 100644 auth/fc/Readme.txt create mode 100644 auth/fc/config.html create mode 100644 auth/fc/fcFPP.php create mode 100644 auth/fc/lib.php diff --git a/auth/fc/Readme.txt b/auth/fc/Readme.txt new file mode 100644 index 0000000000..fad629a242 --- /dev/null +++ b/auth/fc/Readme.txt @@ -0,0 +1,71 @@ +Moodle - FirstClass authentication module +----------------------------------------- +This module uses the FirstClass Flexible Provisining Protocol (FPP) to communicate between the FirstClass server +and the Moodle host. + +Installation +------------ + +1. Enable FPP on the FirstClass server +FPP is not doumented in the FirstClass documentation and is not enable by default. +To enable the protocol you need to edit the file \FCPO\Server\Netinfo. Open the file and insert the +following lines. + +// TCP port for Flexible Provisioning Protocol (FPP). +TCPFPPPORT = 3333 + + +2. Create an account on the FirstClass server with privilege "Subadministrator". +Using the FPP protocoll this module logs in to the FirstClass server and issuess batch admin commands. +Batch admin command can only be issued in the context of a user with subadministrative privileges. + +Default account name is "fcMoodle". + + +3. Check that the FPP protocoll is working by running a Telnet session. If everyting is working you +should get a "+0" answer from the server. + +> telnet yourhost.domain.com 3333 ++0 + +Check that the "fcMoodle" is working by entering the following sequens of commands: + +> telnet yourhost.domain.com 3333 ++0 +fcMoodle ++0 + +the_password_you_gave_fcmoodle ++0 + +Get user some_user_id 1201 + +1201 0 some_user_id ++0 + + + +4. On the Moodle host go to the directory where you have installed Moodle. +Open the folder "auth", where all other authentication modules are installed, + and create a new directory with the name "fc". + +Copy the files "config.html", "fcFPP.php" and "lib.php" to the "auth" directory. + +Now you need to add som strings to the language file. This distribution contains +string for the English (en) and Swedish (sv) translation. + +Open the file "auth.php" in the folder "lang/sv" and paste the text from the file +"auth.php - sv.txt" at the end of the file above the line "?>" + +Open the file "auth.php" in the folder "lang/en" and paste the text from the file +"auth.php - en.txt" at the end of the file above the line "?>" + + + + + + + + + + diff --git a/auth/fc/config.html b/auth/fc/config.html new file mode 100644 index 0000000000..c8e4b5008d --- /dev/null +++ b/auth/fc/config.html @@ -0,0 +1,84 @@ +auth_fchost)) { + $config->auth_fchost = "127.0.0.1"; + } + if (!isset($config->auth_fcfppport)) { + $config->auth_fcfppport = "3333"; + } + if (!isset($config->auth_fcuserid)) { + $config->auth_fcuserid = "fcMoodle"; + } + if (!isset($config->auth_fcpasswd)) { + $config->auth_fcpasswd = ""; + } + if (!isset($config->auth_fccreators)) { + $config->auth_fccreators = ""; + } +?> + + +

auth_fchost: + + + + + + + + + + +

auth_fcfppport: + + + + + + + + + + +

auth_fcuserid: + + + + + + + + + + +

auth_fcpasswd: + + + + + + + + + + +

auth_fccreators: + + + + + + + + + + + +

: + + + + + + + + diff --git a/auth/fc/fcFPP.php b/auth/fc/fcFPP.php new file mode 100644 index 0000000000..daac9b5e1d --- /dev/null +++ b/auth/fc/fcFPP.php @@ -0,0 +1,218 @@ +_hostname = $host; + $this->_port = $port; + $this->_user = ""; + $this->_pwd = ""; + } + + // open a connection to the FirstClass server + function open() + { + if($this->_debug) echo "Connecting to host "; + $host = $this->_hostname; + $port = $this->_port; + + if($this->_debug) echo "[$host:$port].."; + + // open the connection to the FirstClass server + $conn = fsockopen($host, $port, $errno, $errstr, 5); + if(!$conn) + { + echo "connection failed!".$errno. $errstr; + return false; + } + + // We are connected + if($this->_debug) echo "connected!"; + + // Read connection message. + $line = fgets ($conn); //+0 + $line = fgets ($conn); //new line + + // store the connection in this class, so we can use it later + $this->_conn = & $conn; + + return true; + } + + // close any open connections + function close() + { + // get the current connection + $conn = &$this->_conn; + + // close it if it's open + if($conn) + { + fclose($conn); + + // cleanup the variable + unset($this->_conn); + return true; + } + return; + } + + + // Authenticate to the FirstClass server + function login($userid, $passwd) + { + // we did have a connection right?! + if($this->_conn) + { + # Send username + fputs($this->_conn,"$userid\r\n"); + + $line = fgets ($this->_conn); //new line + $line = fgets ($this->_conn); //+0 + $line = fgets ($this->_conn); //new line + + # Send password + fputs($this->_conn,"$passwd\r\n"); + $line = fgets ($this->_conn); //new line + $line = fgets ($this->_conn); //+0 + $line = fgets ($this->_conn); //+0 or message + + if($this->_debug) echo $line; + + if (preg_match ("/^\+0/", $line)) { //+0, user with subadmin privileges + $this->_user = $userid; + $this->_pwd = $passwd; + return TRUE; + } elseif (preg_match ("/^\Sorry/",$line)){ //Denied access but a valid user and password + return TRUE; + } else { //Invalid user or password + return FALSE; + } + + + } + return FALSE; + } + + // Get the list of groups the user is a member of + function getGroups($userid){ + + $groups = array(); + + // we must be logged in as a user with subadmin privileges + if($this->_conn AND $this->_user) { + # Send BA-command to get groups + fputs($this->_conn,"GET USER '" . $userid . "' 4 -1\r"); + $line = ""; + while (!$line) { + $line = trim(fgets ($this->_conn)); + } + $n = 0; + while ($line AND !preg_match("/^\+0/", $line) AND $line != "-1003") { + list( , , $groups[$n++]) = explode(" ",$line,3); + $line = trim(fgets ($this->_conn)); + } + if($this->_debug) echo "getGroups:" . implode(",",$groups); + } + + return $groups; + } + + // Check if the user is member of any of the groups. + // Return the list of groups the user is member of. + function isMemberOf($userid, $groups){ + + $usergroups = array_map("strtolower",$this->getGroups($userid)); + $groups = array_map("strtolower",$groups); + + $result = array_intersect($groups,$usergroups); + + if($this->_debug) echo "isMemberOf:" . implode(",",$result); + + return $result; + + } + + function getUserInfo($userid, $field){ + + $userinfo = ""; + + if($this->_conn AND $this->_user) { + # Send BA-command to get data + fputs($this->_conn,"GET USER '" . $userid . "' " . $field . "\r"); + $line = ""; + while (!$line) { + $line = trim(fgets ($this->_conn)); + } + $n = 0; + while ($line AND !preg_match("/^\+0/", $line)) { + list( , , $userinfo) = explode(" ",$line,3); + $line = trim(fgets ($this->_conn)); + } + if($this->_debug) echo "getUserInfo:" . $userinfo; + } + + return str_replace('\r',' ',trim($userinfo,'"')); + + } + + function getResume($userid){ + + $resume = ""; + + $pattern = "/\[.+:.+\..+\]/"; // Remove references to pictures in resumes + + if($this->_conn AND $this->_user) { + # Send BA-command to get data + fputs($this->_conn,"GET RESUME '" . $userid . "' 6\r"); + $line = ""; + while (!$line) { + $line = trim(fgets ($this->_conn)); + } + $n = 0; + while ($line AND !preg_match("/^\+0/", $line)) { + $resume .= preg_replace($pattern,"",str_replace('\r',"\n",trim($line,'6 '))); + $line = trim(fgets ($this->_conn)); + //print $line; + + } + if($this->_debug) echo "getResume:" . $resume; + } + + return $resume; + + } + + +} + + +?> \ No newline at end of file diff --git a/auth/fc/lib.php b/auth/fc/lib.php new file mode 100644 index 0000000000..9cf94a61d7 --- /dev/null +++ b/auth/fc/lib.php @@ -0,0 +1,154 @@ +auth_fchost; + $port = $CFG->auth_fcfppport; + + $retval = FALSE; + + if (!$username or !$password) { // Don't allow blank usernames or passwords + return $retval; + } + + + $fpp = new fcFPP($hostname,$port); + if ($fpp->open()) { + if ($fpp->login($username,$password)){ + $retval = TRUE; + } + } + $fpp->close(); + + return $retval; + + +} + +function auth_get_userinfo($username){ +// Get user information from FirstCLass server and return it in an array. +// Localize this routine to fit your needs. + +/* +Moodle FirstCLass fieldID in UserInfo form +------ ----------------------------------- +firstname 1202 +lastname 1204 +email 1252 +icq - +phone1 1206 +phone2 1207 (Fax) +institution - +department - +address 1205 +city - +country - +lang - +timezone 8030 (Not used yet. Need to figure out how FC codes timezones) + +description Get data from users resume. Pictures will be removed. + +*/ + + global $CFG; + + $hostname = $CFG->auth_fchost; + $port = $CFG->auth_fcfppport; + $userid = $CFG->auth_fcuserid; + $passwd = $CFG->auth_fcpasswd; + + $userinfo = array(); + + $fpp = new fcFPP($hostname,$port); + if ($fpp->open()) { + if ($fpp->login($userid,$passwd)){ + + $userinfo['firstname'] = $fpp->getUserInfo($username,"1202"); + $userinfo['lastname'] = $fpp->getUserInfo($username,"1204"); + $userinfo['email'] = strtok($fpp->getUserInfo($username,"1252"),','); + $userinfo['phone1'] = $fpp->getUserInfo($username,"1206"); + $userinfo['phone2'] = $fpp->getUserInfo($username,"1207"); + $userinfo['description'] = $fpp->getResume($username); + + } + } + + $fpp->close(); + + foreach($userinfo as $key => $value) { + if (!$value) { + unset($userinfo[$key]); + } + } + + return $userinfo; + +} + + +function auth_iscreator($username=0) { +//Get users group membership from the FirstClass server user and check if +// user is member of one of the groups of creators. + + global $CFG, $USER; + + if (! $CFG->auth_fccreators) { + return false; + } + + if (! $username) { + $username=$USER->username; + } + + $fcgroups = array(); + + $hostname = $CFG->auth_fchost; + $port = $CFG->auth_fcfppport; + $userid = $CFG->auth_fcuserid; + $passwd = $CFG->auth_fcpasswd; + + $fpp = new fcFPP($hostname,$port); + if ($fpp->open()) { + if ($fpp->login($userid,$passwd)){ + $fcgroups = $fpp->getGroups($username); + } + } + $fpp->close(); + + + if ((! $fcgroups)) { + return false; + } + + $creators = explode(";",$CFG->auth_fccreators); + + foreach($creators as $creator) { + If (in_array($creator, $fcgroups)) return true; + } + + return false; +} + \ No newline at end of file -- 2.39.5