added hardware token add/list methods.
[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);';
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 hasToken($username)
103 {
104         $db = getDB();
105         
106         $sql = "select * from users where users_username='$username'";
107         $res = $db->query($sql);
108         if(!$res) return false;
109         foreach($res as $row) {
110                 print_r($row);
111         }
112         
113         return true;
114 }
115
116 function createUserInDB($username, $realname)
117 {
118         $db = getDB();
119         
120         $sql = "insert into users values (NULL, '$username', '$realname', '', '$data', '', '1', '')";           
121 }
122
123 // a funciton to deal with Config Vars
124 function confGetVal($varname)
125 {
126         $db = getDB();
127         
128         $sql = "select conf_value from config where conf_name='$varname'";
129         
130         $result = $db->query($sql);
131         
132         if(!$result) return false;
133         
134         $val = "";
135         foreach($result as $row) {
136                 $val = $row["conf_value"];
137         }
138
139         // TOTALLY GUNNA WORK!
140         return $val;
141 }
142
143 // and a function to put vars
144 function confSetVal($varname, $value)
145 {
146         $db = getDB();
147         
148         $sql = "delete from config where conf_name='$varname'";
149         $db->query($sql);
150         
151         $sql = "insert into config values (NULL, '$varname','$value')";
152         $db->query($sql);
153         
154         // TODO: do all this better
155         return true;
156 }
157
158 // now we define our extended class
159 class gaasdGA extends GoogleAuthenticator
160 {
161         
162         function getData($username) {
163                 //echo "called into getdata\n";
164                 
165                 // get our database connection
166                 $dbObject = getDB();
167                 
168                 // set the sql for retreiving the data
169                 $sql = "select users_tokendata from users where users_username='$username'";
170                 
171                 // run the query
172                 $result = $dbObject->query($sql);
173                 
174                 // check the result
175                 //echo "next1\n";
176                 if(!$result) return false;
177                 
178                 // now just retreieve all the data (there should only be one, but whatever)
179                 //echo "next2\n";
180                 $tokendata = false;
181                 foreach($result as $row) {
182                         $tokendata = $row["users_tokendata"];
183                 }
184
185                 //echo "next3, $username, $tokendata\n";
186                 // now we have our data, we just return it. If we got no data
187                 // we'll just return false by default
188                 return $tokendata;
189                 
190                 // and there you have it, simple eh?
191         }
192         
193         
194         function putData($username, $data) {
195                 // get our database connection
196                 $dbObject = getDB();
197                 
198                 // we need to check if the user exists, and if so put the data, if not create the data
199                 $sql = "select * from users where users_username='$username'";
200                 echo "sql was: $sql\n";
201                 $res = $dbObject->query($sql);
202                 if($res->fetchColumn() > 0) {
203                         // do update
204                         //error_log("doing userdata update");
205                         //"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)
206                         $sql = "update users set users_tokendata='$data' where users_username='$username'";
207                 } else {
208                         // do insert
209                         //error_log("doing user data create");
210                         $sql = "insert into users values (NULL, '$username', '', '', '$data', '', '1', 'software')";
211                 }
212                 
213                 if($dbObject->query($sql)) {
214                         return true;
215                 } else {
216                         return false;
217                 }
218
219         }
220         
221         function getUsers() {
222                 // get our database connection
223                 $dbObject = getDB();
224                 
225                 // now the sql again
226                 $sql = "select users_username from users";
227                 
228                 // run the query
229                 $result = $dbObject->query($sql);
230                 
231                 // iterate over the results - we expect a simple array containing
232                 // a list of usernames
233                 $i = 0;
234                 $users = array();
235                 foreach($result as $row) {
236                         $users[$i] = $row["username"];
237                         $i++;
238                 }
239                 
240                 // now return the list
241                 return $users;
242         }
243 }
244 ?>