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