3 if(!isset($TCP_PORT_NUMBER)) $TCP_PORT_NUMBER = 21446;
4 global $TCP_PORT_NUMBER;
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);
26 $BASE_DIR = realpath(dirname(__FILE__)."/../../");
30 require_once(dirname(__FILE__)."/../../lib/ga4php.php");
32 function generateRandomString()
35 $strpos = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
37 for($i=0; $i<128; $i++) {
38 $str .= $strpos[rand(0, strlen($strpos)-1)];
44 function updateRadius()
46 // this is hardcoded for now.
47 //$clientfile = "/etc/freeradius/clients.conf";
48 $clientfile = "/tmp/clients.conf";
49 $reloadinit = "/etc/init.d/freeradius restart";
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);
66 //system($reloadinit);
70 function getDatabase()
74 if(file_exists("$BASE_DIR/authserver/authd/gaasdata.sqlite")) {
76 $dbobject = new PDO("sqlite:$BASE_DIR/authserver/authd/gaasdata.sqlite");
77 } catch(PDOException $exep) {
78 error_log("execpt on db open");
82 $dbobject = new PDO("sqlite:$BASE_DIR/authserver/authd/gaasdata.sqlite");
83 } catch(PDOException $exep) {
84 error_log("execpt on db open");
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);
95 function closeDatabase($db) {
96 // doesnt do anything yet
99 class gaasGA extends GoogleAuthenticator {
100 function getData($username) {
101 //echo "called into getdata\n";
103 // get our database connection
104 $dbObject = getDatabase();
106 // set the sql for retreiving the data
107 $sql = "select users_tokendata from users where users_username='$username'";
110 $result = $dbObject->query($sql);
114 if(!$result) return false;
116 // now just retreieve all the data (there should only be one, but whatever)
119 foreach($result as $row) {
120 $tokendata = $row["users_tokendata"];
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
128 // and there you have it, simple eh?
132 function putData($username, $data) {
133 // get our database connection
134 $dbObject = getDatabase();
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) {
141 //error_log("doing userdata update");
142 $sql = "update users set users_tokendata='$data' where users_username='$username'";
145 //error_log("doing user data create");
146 $sql = "insert into users values (NULL, '$username', '', '', '$data', '')";
149 if($dbObject->query($sql)) {
157 function getUsers() {
158 // get our database connection
159 $dbObject = getDatabase();
162 $sql = "select users_username from users";
165 $result = $dbObject->query($sql);
167 // iterate over the results - we expect a simple array containing
168 // a list of usernames
171 foreach($result as $row) {
172 $users[$i] = $row["username"];
176 // now return the list