3 require_once("globalLib.php");
4 require_once("gaasdMessages.php");
7 require_once(dirname(__FILE__)."/../../lib/ga4php.php");
9 // first we check if our db exists, if not, we're not inited
12 global $initState, $backEnd;
13 if(file_exists($BASE_DIR."/gaas/gaasd/gaasd.sqlite")) {
14 // then we check if the config vars we need exist in the db
15 $backEndType = confGetVal("backend");
17 echo "backend type is $backEndType\n";
19 if($backEndType == "AD") {
20 echo "init state should be true\n";
23 // TODO: we should now check all vars are set, but for now this will surfice
27 if($backEndType == "internal") {
33 // have a gloval db handle so we dont have to keep opening the db all the time
34 // this may go away when we consider the implications for a parallel gaasd
39 // a function to create our db
40 // TODO: error checking
44 global $BASE_DIR, $initState, $backEnd;
46 $dbobject = new PDO("sqlite:$BASE_DIR/gaas/gaasd/gaasd.sqlite");
47 } catch(PDOException $exep) {
48 error_log("execpt on db open");
52 // users_tokendata is used by ga4php, users_otk is the qrcode data link if needed,
53 // tokentype is the software/hardware token types
54 $sql = 'CREATE TABLE "users" ("users_id" INTEGER PRIMARY KEY AUTOINCREMENT,"users_username" TEXT, "users_realname" TEXT, "users_password" TEXT, "users_tokendata" TEXT, "users_qrcodeid" TEXT, "user_enabled" TEXT, "users_tokentype" TEXT, "users_isadmin" TEXT, "users_pin" TEXT);';
55 $dbobject->query($sql);
57 //echo "Create user table failed\n";
59 $sql = 'CREATE TABLE "config" ("conf_id" INTEGER PRIMARY KEY AUTOINCREMENT,"conf_name" TEXT, "conf_value" TEXT);';
60 $dbobject->query($sql);
61 $sql = 'CREATE TABLE "radclients" ("rad_id" INTEGER PRIMARY KEY AUTOINCREMENT,"rad_name" TEXT, "rad_ip" TEXT, "rad_secret" TEXT, "rad_desc" TEXT);';
62 $dbobject->query($sql);
63 $sql = 'CREATE TABLE "hardwaretokens" ("tok_id" INTEGER PRIMARY KEY AUTOINCREMENT,"tok_name" TEXT, "tok_key" TEXT, "tok_type" TEXT);';
64 $dbobject->query($sql);
69 // a function to get the database
73 global $BASE_DIR, $DB_HANDLE;
74 if($DB_HANDLE != false) return $DB_HANDLE;
75 if(file_exists("$BASE_DIR/gaas/gaasd/gaasd.sqlite")) {
77 $dbobject = new PDO("sqlite:$BASE_DIR/gaas/gaasd/gaasd.sqlite");
78 } catch(PDOException $exep) {
79 error_log("execpt on db open");
86 $DB_HANDLE = $dbobject;
91 function confDelVar($varname)
95 $sql = "delete from config where conf_name='$varname'";
102 function hasToken($username)
106 $sql = "select * from users where users_username='$username'";
107 $res = $db->query($sql);
108 if(!$res) return false;
109 foreach($res as $row) {
116 function createUserInDB($username, $realname)
120 $sql = "insert into users values (NULL, '$username', '$realname', '', '$data', '', '1', '', '0', '')";
123 // a funciton to deal with Config Vars
124 function confGetVal($varname)
128 $sql = "select conf_value from config where conf_name='$varname'";
130 $result = $db->query($sql);
132 if(!$result) return false;
135 foreach($result as $row) {
136 $val = $row["conf_value"];
139 // TOTALLY GUNNA WORK!
143 // and a function to put vars
144 function confSetVal($varname, $value)
148 $sql = "delete from config where conf_name='$varname'";
151 $sql = "insert into config values (NULL, '$varname','$value')";
154 // TODO: do all this better
158 // now we define our extended class
159 class gaasdGA extends GoogleAuthenticator
162 function getData($username) {
163 //echo "called into getdata\n";
165 // get our database connection
168 // set the sql for retreiving the data
169 $sql = "select users_tokendata from users where users_username='$username'";
172 $result = $dbObject->query($sql);
176 if(!$result) return false;
178 // now just retreieve all the data (there should only be one, but whatever)
181 foreach($result as $row) {
182 $tokendata = $row["users_tokendata"];
185 //echo "next3, $username, $tokendata\n";
186 // now we have our data, we just return it. If we got no data
187 // we'll just return false by default
190 // and there you have it, simple eh?
194 function putData($username, $data) {
195 // get our database connection
198 // we need to check if the user exists, and if so put the data, if not create the data
199 $sql = "select * from users where users_username='$username'";
200 echo "sql was: $sql\n";
201 $res = $dbObject->query($sql);
202 if($res->fetchColumn() > 0) {
204 //error_log("doing userdata update");
205 //"users_id" INTEGER PRIMARY KEY AUTOINCREMENT,"users_username" TEXT, "users_realname" TEXT, "users_password" TEXT, "users_tokendata" TEXT, "users_qrcodeid" TEXT, "user_enabled" TEXT, "users_tokentype" TEXT)
206 $sql = "update users set users_tokendata='$data' where users_username='$username'";
209 //error_log("doing user data create");
210 $sql = "insert into users values (NULL, '$username', '', '', '$data', '', '1', 'software', '0', '')";
213 if($dbObject->query($sql)) {
221 function getUsers() {
222 // get our database connection
226 $sql = "select users_username from users";
229 $result = $dbObject->query($sql);
231 // iterate over the results - we expect a simple array containing
232 // a list of usernames
235 foreach($result as $row) {
236 $users[$i] = $row["username"];
240 // now return the list