a2b50de42fda00b7422deb0e176bfd13f9ecac21
[ga4php.git] / gaas / lib / gaasdLib.php
1 <?php 
2
3 require_once("globalLib.php");
4 require_once("gaasdMessages.php");
5
6 // messy
7 require_once(dirname(__FILE__)."/../../lib/ga4php.php");
8
9 // first we check if our db exists, if not, we're not inited
10 $initState = false;
11 $backEnd = "";
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");
16         
17         echo "backend type is $backEndType\n";
18         
19         if($backEndType == "AD") {
20                 echo "init state should be true\n";
21                 $backEnd = "AD";
22                 
23                 // TODO: we should now check all vars are set, but for now this will surfice
24                 $initState = true;
25         }
26
27         if($backEndType == "internal") {
28                 $backEnd = "IN";
29                 $initState = true;
30         }
31 }
32
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
35 $DB_HANDLE = false;
36 global $DB_HANDLE;
37
38
39 // a function to create our db
40 // TODO: error checking
41 function createDB()
42 {
43         $dbobject = false;
44         global $BASE_DIR, $initState, $backEnd;
45         try {
46                 $dbobject = new PDO("sqlite:$BASE_DIR/gaas/gaasd/gaasd.sqlite");
47         } catch(PDOException $exep) {
48                 error_log("execpt on db open");
49                 return false;
50         }
51         
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);
62         
63         return true;
64 }
65
66 // a function to get the database
67 function getDB()
68 {
69         $dbobject = false;
70         global $BASE_DIR, $DB_HANDLE;
71         if($DB_HANDLE != false) return $DB_HANDLE;
72         if(file_exists("$BASE_DIR/gaas/gaasd/gaasd.sqlite")) {
73                 try {
74                         $dbobject = new PDO("sqlite:$BASE_DIR/gaas/gaasd/gaasd.sqlite");
75                 } catch(PDOException $exep) {
76                         error_log("execpt on db open");
77                         return false;
78                 }
79         } else {
80                 return false;
81         }
82         
83         $DB_HANDLE = $dbobject;
84         return $dbobject;
85 }
86
87
88 function confDelVar($varname)
89 {
90         $db = getDB();
91         
92         $sql = "delete from config where conf_name='$varname'";
93         $db->query($sql);
94         
95         return true;
96 }
97
98 // a funciton to deal with Config Vars
99 function confGetVal($varname)
100 {
101         $db = getDB();
102         
103         $sql = "select conf_value from config where conf_name='$varname'";
104         
105         $result = $db->query($sql);
106         
107         if(!$result) return false;
108         
109         $val = "";
110         foreach($result as $row) {
111                 $val = $row["conf_value"];
112         }
113
114         // TOTALLY GUNNA WORK!
115         return $val;
116 }
117
118 // and a function to put vars
119 function confSetVal($varname, $value)
120 {
121         $db = getDB();
122         
123         $sql = "delete from config where conf_name='$varname'";
124         $db->query($sql);
125         
126         $sql = "insert into config values (NULL, '$varname','$value')";
127         $db->query($sql);
128         
129         // TODO: do all this better
130         return true;
131 }
132
133 // now we define our extended class
134 class gaasdGA extends GoogleAuthenticator
135 {
136         
137         function getData($username) {
138                 //echo "called into getdata\n";
139                 
140                 // get our database connection
141                 $dbObject = getDB();
142                 
143                 // set the sql for retreiving the data
144                 $sql = "select users_tokendata from users where users_username='$username'";
145                 
146                 // run the query
147                 $result = $dbObject->query($sql);
148                 
149                 // check the result
150                 //echo "next1\n";
151                 if(!$result) return false;
152                 
153                 // now just retreieve all the data (there should only be one, but whatever)
154                 //echo "next2\n";
155                 $tokendata = false;
156                 foreach($result as $row) {
157                         $tokendata = $row["users_tokendata"];
158                 }
159
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
163                 return $tokendata;
164                 
165                 // and there you have it, simple eh?
166         }
167         
168         
169         function putData($username, $data) {
170                 // get our database connection
171                 $dbObject = getDB();
172                 
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) {
177                         // do update
178                         //error_log("doing userdata update");
179                         $sql = "update users set users_tokendata='$data' where users_username='$username'";
180                 } else {
181                         // do insert
182                         //error_log("doing user data create");
183                         $sql = "insert into users values (NULL, '$username', '', '', '$data', '')";
184                 }
185                 
186                 if($dbObject->query($sql)) {
187                         return true;
188                 } else {
189                         return false;
190                 }
191
192         }
193         
194         function getUsers() {
195                 // get our database connection
196                 $dbObject = getDB();
197                 
198                 // now the sql again
199                 $sql = "select users_username from users";
200                 
201                 // run the query
202                 $result = $dbObject->query($sql);
203                 
204                 // iterate over the results - we expect a simple array containing
205                 // a list of usernames
206                 $i = 0;
207                 $users = array();
208                 foreach($result as $row) {
209                         $users[$i] = $row["username"];
210                         $i++;
211                 }
212                 
213                 // now return the list
214                 return $users;
215         }
216 }
217 ?>