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);';
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) {
117 // a funciton to deal with Config Vars
118 function confGetVal($varname)
122 $sql = "select conf_value from config where conf_name='$varname'";
124 $result = $db->query($sql);
126 if(!$result) return false;
129 foreach($result as $row) {
130 $val = $row["conf_value"];
133 // TOTALLY GUNNA WORK!
137 // and a function to put vars
138 function confSetVal($varname, $value)
142 $sql = "delete from config where conf_name='$varname'";
145 $sql = "insert into config values (NULL, '$varname','$value')";
148 // TODO: do all this better
152 // now we define our extended class
153 class gaasdGA extends GoogleAuthenticator
156 function getData($username) {
157 //echo "called into getdata\n";
159 // get our database connection
162 // set the sql for retreiving the data
163 $sql = "select users_tokendata from users where users_username='$username'";
166 $result = $dbObject->query($sql);
170 if(!$result) return false;
172 // now just retreieve all the data (there should only be one, but whatever)
175 foreach($result as $row) {
176 $tokendata = $row["users_tokendata"];
179 //echo "next3, $username, $tokendata\n";
180 // now we have our data, we just return it. If we got no data
181 // we'll just return false by default
184 // and there you have it, simple eh?
188 function putData($username, $data) {
189 // get our database connection
192 // we need to check if the user exists, and if so put the data, if not create the data
193 $sql = "select * from users where users_username='$username'";
194 echo "sql was: $sql\n";
195 $res = $dbObject->query($sql);
196 if($res->fetchColumn() > 0) {
198 //error_log("doing userdata update");
199 //"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)
200 $sql = "update users set users_tokendata='$data' where users_username='$username'";
203 //error_log("doing user data create");
204 $sql = "insert into users values (NULL, '$username', '', '', '$data', '', '1', 'software')";
207 if($dbObject->query($sql)) {
215 function getUsers() {
216 // get our database connection
220 $sql = "select users_username from users";
223 $result = $dbObject->query($sql);
225 // iterate over the results - we expect a simple array containing
226 // a list of usernames
229 foreach($result as $row) {
230 $users[$i] = $row["username"];
234 // now return the list