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