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_otk" TEXT, "user_enabled" TEXT, "users_tokentype" TEXT);';
55 $dbobject->query($sql);
56 $sql = 'CREATE TABLE "config" ("conf_id" INTEGER PRIMARY KEY AUTOINCREMENT,"conf_name" TEXT, "conf_value" TEXT);';
57 $dbobject->query($sql);
58 $sql = 'CREATE TABLE "radclients" ("rad_id" INTEGER PRIMARY KEY AUTOINCREMENT,"rad_name" TEXT, "rad_ip" TEXT, "rad_secret" TEXT, "rad_desc" TEXT);';
59 $dbobject->query($sql);
60 $sql = 'CREATE TABLE "hardwaretokens" ("tok_id" INTEGER PRIMARY KEY AUTOINCREMENT,"tok_name" TEXT, "tok_key" TEXT, "tok_type" TEXT);';
61 $dbobject->query($sql);
66 // a function to get the database
70 global $BASE_DIR, $DB_HANDLE;
71 if($DB_HANDLE != false) return $DB_HANDLE;
72 if(file_exists("$BASE_DIR/gaas/gaasd/gaasd.sqlite")) {
74 $dbobject = new PDO("sqlite:$BASE_DIR/gaas/gaasd/gaasd.sqlite");
75 } catch(PDOException $exep) {
76 error_log("execpt on db open");
83 $DB_HANDLE = $dbobject;
88 function confDelVar($varname)
92 $sql = "delete from config where conf_name='$varname'";
98 // a funciton to deal with Config Vars
99 function confGetVal($varname)
103 $sql = "select conf_value from config where conf_name='$varname'";
105 $result = $db->query($sql);
107 if(!$result) return false;
110 foreach($result as $row) {
111 $val = $row["conf_value"];
114 // TOTALLY GUNNA WORK!
118 // and a function to put vars
119 function confSetVal($varname, $value)
123 $sql = "delete from config where conf_name='$varname'";
126 $sql = "insert into config values (NULL, '$varname','$value')";
129 // TODO: do all this better
133 // now we define our extended class
134 class gaasdGA extends GoogleAuthenticator
137 function getData($username) {
138 //echo "called into getdata\n";
140 // get our database connection
143 // set the sql for retreiving the data
144 $sql = "select users_tokendata from users where users_username='$username'";
147 $result = $dbObject->query($sql);
151 if(!$result) return false;
153 // now just retreieve all the data (there should only be one, but whatever)
156 foreach($result as $row) {
157 $tokendata = $row["users_tokendata"];
160 //echo "next3, $username, $tokendata\n";
161 // now we have our data, we just return it. If we got no data
162 // we'll just return false by default
165 // and there you have it, simple eh?
169 function putData($username, $data) {
170 // get our database connection
173 // we need to check if the user exists, and if so put the data, if not create the data
174 $sql = "select * from users where users_username='$username'";
175 $res = $dbObject->query($sql);
176 if($res->fetchColumn() > 0) {
178 //error_log("doing userdata update");
179 $sql = "update users set users_tokendata='$data' where users_username='$username'";
182 //error_log("doing user data create");
183 $sql = "insert into users values (NULL, '$username', '', '', '$data', '')";
186 if($dbObject->query($sql)) {
194 function getUsers() {
195 // get our database connection
199 $sql = "select users_username from users";
202 $result = $dbObject->query($sql);
204 // iterate over the results - we expect a simple array containing
205 // a list of usernames
208 foreach($result as $row) {
209 $users[$i] = $row["username"];
213 // now return the list