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