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 if($backEndType == "AD") {
20 // TODO: we should now check all vars are set, but for now this will surfice
24 if($backEndType == "internal") {
30 // have a gloval db handle so we dont have to keep opening the db all the time
31 // this may go away when we consider the implications for a parallel gaasd
36 // a function to create our db
37 // TODO: error checking
41 global $BASE_DIR, $initState, $backEnd;
43 $dbobject = new PDO("sqlite:$BASE_DIR/gaas/gaasd/gaasd.sqlite");
44 } catch(PDOException $exep) {
45 error_log("execpt on db open");
49 if($backEnd == "IN") {
50 $sql = 'CREATE TABLE "users" ("users_id" INTEGER PRIMARY KEY AUTOINCREMENT,"users_username" TEXT, "users_realname" TEXT, "users_password" TEXT, "users_tokendata" TEXT, "users_otk" TEXT);';
51 $dbobject->query($sql);
54 $sql = 'CREATE TABLE "config" ("conf_id" INTEGER PRIMARY KEY AUTOINCREMENT,"conf_name" TEXT, "conf_value" TEXT);';
55 $dbobject->query($sql);
56 $sql = 'CREATE TABLE "radclients" ("rad_id" INTEGER PRIMARY KEY AUTOINCREMENT,"rad_name" TEXT, "rad_ip" TEXT, "rad_secret" TEXT, "rad_desc" TEXT);';
57 $dbobject->query($sql);
58 $sql = 'CREATE TABLE "hardwaretokens" ("tok_id" INTEGER PRIMARY KEY AUTOINCREMENT,"tok_name" TEXT, "tok_key" TEXT, "tok_type" TEXT);';
59 $dbobject->query($sql);
64 // a function to get the database
68 global $BASE_DIR, $DB_HANDLE;
69 if($DB_HANDLE != false) return $DB_HANDLE;
70 if(file_exists("$BASE_DIR/gaas/gaasd/gaasd.sqlite")) {
72 $dbobject = new PDO("sqlite:$BASE_DIR/gaas/gaasd/gaasd.sqlite");
73 } catch(PDOException $exep) {
74 error_log("execpt on db open");
81 $DB_HANDLE = $dbobject;
86 function confDelVar($varname)
90 $sql = "delete from config where conf_name='$varname'";
96 // a funciton to deal with Config Vars
97 function confGetVal($varname)
101 $sql = "select conf_value from config where conf_name='$varname'";
103 $result = $db->query($sql);
105 if(!$result) return false;
108 foreach($result as $row) {
109 $val = $row["conf_value"];
112 // TOTALLY GUNNA WORK!
116 // and a function to put vars
117 function confSetVal($varname, $value)
121 $sql = "delete from config where conf_name='$varname'";
124 $sql = "insert into config values (NULL, '$varname','$value')";
127 // TODO: do all this better
131 // now we define our extended class
132 class gaasdGA extends GoogleAuthenticator
135 function getData($username) {
136 //echo "called into getdata\n";
138 // get our database connection
141 // set the sql for retreiving the data
142 $sql = "select users_tokendata from users where users_username='$username'";
145 $result = $dbObject->query($sql);
149 if(!$result) return false;
151 // now just retreieve all the data (there should only be one, but whatever)
154 foreach($result as $row) {
155 $tokendata = $row["users_tokendata"];
158 //echo "next3, $username, $tokendata\n";
159 // now we have our data, we just return it. If we got no data
160 // we'll just return false by default
163 // and there you have it, simple eh?
167 function putData($username, $data) {
168 // get our database connection
171 // we need to check if the user exists, and if so put the data, if not create the data
172 $sql = "select * from users where users_username='$username'";
173 $res = $dbObject->query($sql);
174 if($res->fetchColumn() > 0) {
176 //error_log("doing userdata update");
177 $sql = "update users set users_tokendata='$data' where users_username='$username'";
180 //error_log("doing user data create");
181 $sql = "insert into users values (NULL, '$username', '', '', '$data', '')";
184 if($dbObject->query($sql)) {
192 function getUsers() {
193 // get our database connection
197 $sql = "select users_username from users";
200 $result = $dbObject->query($sql);
202 // iterate over the results - we expect a simple array containing
203 // a list of usernames
206 foreach($result as $row) {
207 $users[$i] = $row["username"];
211 // now return the list