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 if($backEnd == "IN") {
53 $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);';
54 $dbobject->query($sql);
57 $sql = 'CREATE TABLE "config" ("conf_id" INTEGER PRIMARY KEY AUTOINCREMENT,"conf_name" TEXT, "conf_value" TEXT);';
58 $dbobject->query($sql);
59 $sql = 'CREATE TABLE "radclients" ("rad_id" INTEGER PRIMARY KEY AUTOINCREMENT,"rad_name" TEXT, "rad_ip" TEXT, "rad_secret" TEXT, "rad_desc" TEXT);';
60 $dbobject->query($sql);
61 $sql = 'CREATE TABLE "hardwaretokens" ("tok_id" INTEGER PRIMARY KEY AUTOINCREMENT,"tok_name" TEXT, "tok_key" TEXT, "tok_type" TEXT);';
62 $dbobject->query($sql);
67 // a function to get the database
71 global $BASE_DIR, $DB_HANDLE;
72 if($DB_HANDLE != false) return $DB_HANDLE;
73 if(file_exists("$BASE_DIR/gaas/gaasd/gaasd.sqlite")) {
75 $dbobject = new PDO("sqlite:$BASE_DIR/gaas/gaasd/gaasd.sqlite");
76 } catch(PDOException $exep) {
77 error_log("execpt on db open");
84 $DB_HANDLE = $dbobject;
89 function confDelVar($varname)
93 $sql = "delete from config where conf_name='$varname'";
99 // a funciton to deal with Config Vars
100 function confGetVal($varname)
104 $sql = "select conf_value from config where conf_name='$varname'";
106 $result = $db->query($sql);
108 if(!$result) return false;
111 foreach($result as $row) {
112 $val = $row["conf_value"];
115 // TOTALLY GUNNA WORK!
119 // and a function to put vars
120 function confSetVal($varname, $value)
124 $sql = "delete from config where conf_name='$varname'";
127 $sql = "insert into config values (NULL, '$varname','$value')";
130 // TODO: do all this better
134 // now we define our extended class
135 class gaasdGA extends GoogleAuthenticator
138 function getData($username) {
139 //echo "called into getdata\n";
141 // get our database connection
144 // set the sql for retreiving the data
145 $sql = "select users_tokendata from users where users_username='$username'";
148 $result = $dbObject->query($sql);
152 if(!$result) return false;
154 // now just retreieve all the data (there should only be one, but whatever)
157 foreach($result as $row) {
158 $tokendata = $row["users_tokendata"];
161 //echo "next3, $username, $tokendata\n";
162 // now we have our data, we just return it. If we got no data
163 // we'll just return false by default
166 // and there you have it, simple eh?
170 function putData($username, $data) {
171 // get our database connection
174 // we need to check if the user exists, and if so put the data, if not create the data
175 $sql = "select * from users where users_username='$username'";
176 $res = $dbObject->query($sql);
177 if($res->fetchColumn() > 0) {
179 //error_log("doing userdata update");
180 $sql = "update users set users_tokendata='$data' where users_username='$username'";
183 //error_log("doing user data create");
184 $sql = "insert into users values (NULL, '$username', '', '', '$data', '')";
187 if($dbObject->query($sql)) {
195 function getUsers() {
196 // get our database connection
200 $sql = "select users_username from users";
203 $result = $dbObject->query($sql);
205 // iterate over the results - we expect a simple array containing
206 // a list of usernames
209 foreach($result as $row) {
210 $users[$i] = $row["username"];
214 // now return the list