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