18ce1bf94d0425c8d32e64b4ed53291165dcf016
[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, \
55                 "users_password" TEXT, "users_tokendata" TEXT, "users_qrcodeid" TEXT, "user_enabled" TEXT, "users_tokentype" TEXT);';
56         $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);
63         
64         return true;
65 }
66
67 // a function to get the database
68 function getDB()
69 {
70         $dbobject = false;
71         global $BASE_DIR, $DB_HANDLE;
72         if($DB_HANDLE != false) return $DB_HANDLE;
73         if(file_exists("$BASE_DIR/gaas/gaasd/gaasd.sqlite")) {
74                 try {
75                         $dbobject = new PDO("sqlite:$BASE_DIR/gaas/gaasd/gaasd.sqlite");
76                 } catch(PDOException $exep) {
77                         error_log("execpt on db open");
78                         return false;
79                 }
80         } else {
81                 return false;
82         }
83         
84         $DB_HANDLE = $dbobject;
85         return $dbobject;
86 }
87
88
89 function confDelVar($varname)
90 {
91         $db = getDB();
92         
93         $sql = "delete from config where conf_name='$varname'";
94         $db->query($sql);
95         
96         return true;
97 }
98
99
100 function hasToken($username)
101 {
102         $db = getDB();
103         
104         $sql = "select * from users where users_username='$username'";
105         $res = $db->query($sql);
106         if(!$res) return false;
107         foreach($res as $row) {
108                 print_r($row);
109         }
110         
111         return true;
112 }
113
114
115 // a funciton to deal with Config Vars
116 function confGetVal($varname)
117 {
118         $db = getDB();
119         
120         $sql = "select conf_value from config where conf_name='$varname'";
121         
122         $result = $db->query($sql);
123         
124         if(!$result) return false;
125         
126         $val = "";
127         foreach($result as $row) {
128                 $val = $row["conf_value"];
129         }
130
131         // TOTALLY GUNNA WORK!
132         return $val;
133 }
134
135 // and a function to put vars
136 function confSetVal($varname, $value)
137 {
138         $db = getDB();
139         
140         $sql = "delete from config where conf_name='$varname'";
141         $db->query($sql);
142         
143         $sql = "insert into config values (NULL, '$varname','$value')";
144         $db->query($sql);
145         
146         // TODO: do all this better
147         return true;
148 }
149
150 // now we define our extended class
151 class gaasdGA extends GoogleAuthenticator
152 {
153         
154         function getData($username) {
155                 //echo "called into getdata\n";
156                 
157                 // get our database connection
158                 $dbObject = getDB();
159                 
160                 // set the sql for retreiving the data
161                 $sql = "select users_tokendata from users where users_username='$username'";
162                 
163                 // run the query
164                 $result = $dbObject->query($sql);
165                 
166                 // check the result
167                 //echo "next1\n";
168                 if(!$result) return false;
169                 
170                 // now just retreieve all the data (there should only be one, but whatever)
171                 //echo "next2\n";
172                 $tokendata = false;
173                 foreach($result as $row) {
174                         $tokendata = $row["users_tokendata"];
175                 }
176
177                 //echo "next3, $username, $tokendata\n";
178                 // now we have our data, we just return it. If we got no data
179                 // we'll just return false by default
180                 return $tokendata;
181                 
182                 // and there you have it, simple eh?
183         }
184         
185         
186         function putData($username, $data) {
187                 // get our database connection
188                 $dbObject = getDB();
189                 
190                 // we need to check if the user exists, and if so put the data, if not create the data
191                 $sql = "select * from users where users_username='$username'";
192                 $res = $dbObject->query($sql);
193                 if($res->fetchColumn() > 0) {
194                         // do update
195                         //error_log("doing userdata update");
196                         $sql = "update users set users_tokendata='$data' where users_username='$username'";
197                 } else {
198                         // do insert
199                         //error_log("doing user data create");
200                         $sql = "insert into users values (NULL, '$username', '', '', '$data', '')";
201                 }
202                 
203                 if($dbObject->query($sql)) {
204                         return true;
205                 } else {
206                         return false;
207                 }
208
209         }
210         
211         function getUsers() {
212                 // get our database connection
213                 $dbObject = getDB();
214                 
215                 // now the sql again
216                 $sql = "select users_username from users";
217                 
218                 // run the query
219                 $result = $dbObject->query($sql);
220                 
221                 // iterate over the results - we expect a simple array containing
222                 // a list of usernames
223                 $i = 0;
224                 $users = array();
225                 foreach($result as $row) {
226                         $users[$i] = $row["username"];
227                         $i++;
228                 }
229                 
230                 // now return the list
231                 return $users;
232         }
233 }
234 ?>