it actually works, fuck me
[ga4php.git] / authserver / lib / lib.php
1 <?php
2
3 if(!isset($TCP_PORT_NUMBER)) $TCP_PORT_NUMBER = 21446;
4 global $TCP_PORT_NUMBER;
5
6 define("MSG_AUTH_USER_TOKEN", 1);
7 define("MSG_ADD_USER_TOKEN", 2);
8 define("MSG_DELETE_USER", 3);
9 define("MSG_AUTH_USER_PASSWORD", 4);
10 define("MSG_SET_USER_PASSWORD", 5);
11 define("MSG_SET_USER_REALNAME", 6);
12 define("MSG_SET_USER_TOKEN", 7);
13 define("MSG_SET_USER_TOKEN_TYPE", 8);
14 define("MSG_GET_USERS", 9);
15 define("MSG_GET_OTK_PNG", 10);
16 define("MSG_GET_OTK_ID", 11);
17 define("MSG_DELETE_USER_TOKEN", 12);
18 define("MSG_SYNC_TOKEN", 13);
19 define("MSG_GET_TOKEN_TYPE", 14);
20 define("MSG_GET_RADIUS_CLIENTS", 15);
21 define("MSG_REMOVE_RADIUS_CLIENT", 16);
22 define("MSG_ADD_RADIUS_CLIENT", 17);
23
24 // BASE_DIR = 
25 // messy
26 $BASE_DIR = realpath(dirname(__FILE__)."/../../");
27 global $BASE_DIR;
28
29 // messy
30 require_once(dirname(__FILE__)."/../../lib/ga4php.php");
31
32 function generateRandomString()
33 {
34         $str = "";
35         $strpos = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
36         
37         for($i=0; $i<128; $i++) {
38                 $str .= $strpos[rand(0, strlen($strpos)-1)];
39         }
40         
41         return $str;
42 }
43
44 function updateRadius()
45 {
46         // this is hardcoded for now.
47         //$clientfile = "/etc/freeradius/clients.conf";
48         $clientfile = "/tmp/clients.conf";
49         $reloadinit = "/etc/init.d/freeradius restart";
50         
51         $db = getDatabase();
52         
53         echo "in updateradius\n";
54         $hand = fopen($clientfile, "w");
55         $sql = "select * from radclients";
56         $res = $db->query($sql);
57         foreach($res as $row) {
58                 $cname = $row["rad_name"];
59                 $cip = $row["rad_ip"];
60                 $csec = $row["rad_secret"];
61                 $lines = "client $cname {\nipaddr = $cip\nsecret = $csec\nrequire_message_authenticator = no\n}\n\n";
62                 fwrite($hand, $lines);
63         }
64         fclose($hand);
65         // not yet
66         //system($reloadinit);
67 }
68
69
70 function getDatabase()
71 {
72         $dbobject = false;
73         global $BASE_DIR;
74         if(file_exists("$BASE_DIR/authserver/authd/gaasdata.sqlite")) {
75                 try {
76                         $dbobject = new PDO("sqlite:$BASE_DIR/authserver/authd/gaasdata.sqlite");
77                 } catch(PDOException $exep) {
78                         error_log("execpt on db open");
79                 }
80         } else {
81                 try {
82                         $dbobject = new PDO("sqlite:$BASE_DIR/authserver/authd/gaasdata.sqlite");
83                 } catch(PDOException $exep) {
84                         error_log("execpt on db open");
85                 }
86                 $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);';
87                 $dbobject->query($sql);
88                 $sql = 'CREATE TABLE "radclients" ("rad_id" INTEGER PRIMARY KEY AUTOINCREMENT,"rad_name" TEXT, "rad_ip" TEXT, "rad_secret" TEXT, "rad_desc" TEXT);';
89                 $dbobject->query($sql);
90         }
91         
92         return $dbobject;
93 }
94
95 function closeDatabase($db) {
96         // doesnt do anything yet
97 }
98
99 class gaasGA extends GoogleAuthenticator {
100         function getData($username) {
101                 echo "called into getdata\n";
102                 
103                 // get our database connection
104                 $dbObject = getDatabase();
105                 
106                 // set the sql for retreiving the data
107                 $sql = "select users_tokendata from users where users_username='$username'";
108                 
109                 // run the query
110                 $result = $dbObject->query($sql);
111                 
112                 // check the result
113                 echo "next1\n";
114                 if(!$result) return false;
115                 
116                 // now just retreieve all the data (there should only be one, but whatever)
117                 echo "next2\n";
118                 $tokendata = false;
119                 foreach($result as $row) {
120                         $tokendata = $row["users_tokendata"];
121                 }
122
123                 echo "next3, $username, $tokendata\n";
124                 // now we have our data, we just return it. If we got no data
125                 // we'll just return false by default
126                 return $tokendata;
127                 
128                 // and there you have it, simple eh?
129         }
130         
131         
132         function putData($username, $data) {
133                 // get our database connection
134                 $dbObject = getDatabase();
135                 
136                 // we need to check if the user exists, and if so put the data, if not create the data
137                 $sql = "select * from users where users_username='$username'";
138                 $res = $dbObject->query($sql);
139                 if($res->fetchColumn() > 0) {
140                         // do update
141                         error_log("doing userdata update");
142                         $sql = "update users set users_tokendata='$data' where users_username='$username'";
143                 } else {
144                         // do insert
145                         error_log("doing user data create");
146                         $sql = "insert into users values (NULL, '$username', '', '', '$data', '')";
147                 }
148                 
149                 if($dbObject->query($sql)) {
150                         return true;
151                 } else {
152                         return false;
153                 }
154
155         }
156         
157         function getUsers() {
158                 // get our database connection
159                 $dbObject = getDatabase();
160                 
161                 // now the sql again
162                 $sql = "select users_username from users";
163                 
164                 // run the query
165                 $result = $dbObject->query($sql);
166                 
167                 // iterate over the results - we expect a simple array containing
168                 // a list of usernames
169                 $i = 0;
170                 $users = array();
171                 foreach($result as $row) {
172                         $users[$i] = $row["username"];
173                         $i++;
174                 }
175                 
176                 // now return the list
177                 return $users;
178         }       
179 }
180
181 ?>