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 createUserInDB($username, $realname)
106 $sql = "insert into users values (NULL, '$username', '$realname', '', '$data', '', '1', '', '0', '')";
109 // a funciton to deal with Config Vars
110 function confGetVal($varname)
114 $sql = "select conf_value from config where conf_name='$varname'";
116 $result = $db->query($sql);
118 if(!$result) return false;
121 foreach($result as $row) {
122 $val = $row["conf_value"];
125 // TOTALLY GUNNA WORK!
129 // and a function to put vars
130 function confSetVal($varname, $value)
134 $sql = "delete from config where conf_name='$varname'";
137 $sql = "insert into config values (NULL, '$varname','$value')";
140 // TODO: do all this better
144 // now we define our extended class
145 class gaasdGA extends GoogleAuthenticator
148 function getData($username) {
149 //echo "called into getdata\n";
151 // get our database connection
154 // set the sql for retreiving the data
155 $sql = "select users_tokendata from users where users_username='$username'";
158 $result = $dbObject->query($sql);
162 if(!$result) return false;
164 // now just retreieve all the data (there should only be one, but whatever)
167 foreach($result as $row) {
168 $tokendata = $row["users_tokendata"];
171 //echo "next3, $username, $tokendata\n";
172 // now we have our data, we just return it. If we got no data
173 // we'll just return false by default
176 // and there you have it, simple eh?
180 function putData($username, $data) {
181 // get our database connection
184 // we need to check if the user exists, and if so put the data, if not create the data
185 $sql = "select * from users where users_username='$username'";
186 echo "sql was: $sql\n";
187 $res = $dbObject->query($sql);
188 if($res->fetchColumn() > 0) {
190 //error_log("doing userdata update");
191 //"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)
192 $sql = "update users set users_tokendata='$data' where users_username='$username'";
195 //error_log("doing user data create");
196 $sql = "insert into users values (NULL, '$username', '', '', '$data', '', '1', 'software', '0', '')";
199 if($dbObject->query($sql)) {
207 function getUsers() {
208 // get our database connection
212 $sql = "select users_username from users";
215 $result = $dbObject->query($sql);
217 // iterate over the results - we expect a simple array containing
218 // a list of usernames
221 foreach($result as $row) {
222 $users[$i] = $row["username"];
226 // now return the list