fixed the provision user method
[ga4php.git] / gaas / lib / gaasdMessages.php
index e518067..82a07e1 100644 (file)
@@ -11,8 +11,21 @@ function gaasStatus_server($messages)
        $return = "init";
        if($initState != false && $backEnd != "") {
                $return = "running";
+               $be = confGetVal("backend");
+               if($be == "AD") {
+                       $dom = confGetVal("ad.domain");
+                       $user = confGetVal("ad.user");
+                       $client = confGetVal("ad.clientdef");
+                       $admin = confGetVal("ad.admindef");
+                       $return .= " - AD integrated to $dom, GAASD Username: $user, Clients Group: $client, Admins Group: $admin";             
+               } else {
+                       $return .= " - internal database";
+               }
+               
        }
        
+       
+       
        return $return;
 }
 
@@ -21,6 +34,7 @@ function gaasInitServer_server($msg)
 {
        global $initState, $backEnd;
        
+       error_log("Init server called\n");
        // here we "init" the server, if we're ad, we attempt to connect to AD and if it all works
        // we then create the db
        // $m["backend"] = "AD|IN";
@@ -32,6 +46,16 @@ function gaasInitServer_server($msg)
        // IN expects
        // $m["user"] = "someuser";
        // $m["pass"] = "somepass";
+       echo "initstate is $initState";
+       if($initState) {
+               echo "true\n";
+       } else {
+               echo "false\n";
+       }
+       if($initState) {
+               error_log("init server called when server already init'd\n");
+               return false;
+       }
        
        if($msg["backend"] == "AD") {
                $backEnd = "AD";
@@ -41,7 +65,19 @@ function gaasInitServer_server($msg)
                $adpass = $msg["pass"];
                $adclientdef = $msg["clientdef"];
                $adadmindef = $msg["admindef"];
+               
                // now wee test our logins...
+               // first look up the domain name stuff
+               $servers = dns_get_record("_gc._tcp.$addom");
+               if(count($servers)<1) {
+                       echo "AD servers cant be found, fail!\n";
+               }
+               
+               // we should check all servers, but lets just go with 0 for now
+               $res =  adTestLogin($addom, $adlogin, $adpass);
+               if(!$res) {
+                       return false;
+               }
                
                
                // then
@@ -52,22 +88,157 @@ function gaasInitServer_server($msg)
                confSetVal("ad.encryptionkey", generateHexString(32));
                confSetVal("ad.clientdef", $adclientdef);
                confSetVal("ad.admindef", $adadmindef);
+               confSetVal("backend", "AD");
+               confSetVal("defaulttokentype", "TOTP");
                
-               $initState = "running";
+               $initState = true;
                $backEnd = "AD";
                
                // and that should be it... i think cept im in a forked erg.. lets assume it works, need pain i do not.
-               
                return true;
        } else if($msg["backend"] == "IN") {
                // this ones simpler
                $backEnd = "IN";
                createDB();
+               
+               // create the user in the db
+               $username = $msg["user"];
+               $password = $msg["pass"];
+               
+               $myga = new gaasdGA();
+               $myga->setUser($username);
+               
+               if($password == "") $pass = "";
+               else $pass = hash('sha512', $password);
+               
+               $db = getDB();
+               $db->query($sql = "update users set users_password='$pass' where users_username='$username'");
+               
                $initState = "running";
-               // then we need to "create user";
                return true;
        } else {
                return false;
        }
 }
+
+
+function gaasSetADLogin_server($msg)
+{
+       global $initState, $backEnd;
+       
+       if($initState != "running") {
+               return "not in running init state";
+       }
+       
+       if($backEnd != "AD") {
+               return "not setup as AD client";
+       }
+       
+       $addom = $msg["domain"];
+       $adlogin = $msg["user"];
+       $adpass = $msg["pass"];
+       
+       $res = adTestLogin($addmo, $adlogin, $adpass);
+       if($res != 0) {
+               return "not able to connect to AD with given cred's";
+       }
+       
+       confSetVal("ad.domain", $addom);
+       confSetVal("ad.user", $adlogin);
+       confSetVal("ad.pass", $adpass);
+       
+       return true;
+       
+}
+
+function gaasSetAdminGroup_server($msg)
+{
+       if(confGetVal("backend") == "AD") {
+               confSetVal("ad.admindef", $msg["admingroup"]);
+       } else return false;
+       
+       return true;
+}
+
+function gaasSetClientGroup_server($msg)
+{
+       if(confGetVal("backend") == "AD") {
+               confSetVal("ad.clientdef", $msg["clientgroup"]);
+       } else return false;
+       
+       return true;
+}
+
+function gaasProvisionUser_server($msg)
+{
+       
+       // function userInGroup($user, $domain, $adlogin, $adpass, $group)
+       echo "in provision user\n";
+       print_r($msg);
+       $dttype = confGetVal("defaulttokentype");
+       if($dttype != "HOTP" && $dttype != "TOTP") {
+               echo "default token type not set, setting to TOTP\n";
+               confSetVal("defaulttokentype", "TOTP");
+               $dttype = "TOTP";
+       }
+       if($msg["tokentype"] == "") {
+               $ttype = confGetVal("defaulttokentype");
+       } else {
+               $ttype = $msg["tokentype"];
+       }
+       if($ttype != "HOTP" && $ttype != "TOTP") {
+               echo "using default token type, $dttype because user entered value of $ttype doesnt make sense\n";
+               $ttype = $dttype;
+       }
+       $tkey = $msg["tokenkey"];
+       if(confGetVal("backend") == "AD") {
+               if(userInGroup($msg["username"], confGetVal("ad.domain"), confGetVal("ad.user"), confGetVal("ad.pass"), confGetVal("ad.clientdef"))) {
+                       $myga = new gaasdGA();
+                       $myga->setUser($msg["username"], $ttype, "", $tkey);
+               } else {
+                       echo "User not in client group\n";
+               }
+       } else {
+               // internal db
+       }
+       
+       
+       return true;
+}
+
+function gaasGetUsers_server($msg)
+{
+       $haveTokens = $msg["havetokens"];
+       $userPatter = $msg["userpattern"];
+       $group = $msg["group"];
+       
+       if(confGetval("backend") == "AD") {
+               $adgroup = "";
+               if($group == "admin") {
+                       $adgroup = confGetVal("ad.admindef");
+               } else {
+                       $adgroup = confGetVal("ad.clientdef");
+               }
+               $addom = confGetVal("ad.domain");
+               $aduser = confGetVal("ad.user");
+               $adpass = confGetVal("ad.pass");
+               //echo "using group $adgroup for $group\n";
+               
+               $users = getUsersInGroup($addom, $aduser, $adpass, $adgroup);
+               foreach($users as $user => $real) {
+                       hasToken($user);
+               }
+       } else {
+               // internal db
+       }
+       return $users;
+}
+
+function gaasDeleteUser_server($msg)
+{
+       $username = $msg["username"];
+       $db = getDB();
+       $db->query($sql = "delete from users where users_username='$username'");
+       
+}
 ?>
\ No newline at end of file