Initial coding of the gaasd new auth server.
[ga4php.git] / gaas / lib / gaasdLib.php
1 <?php 
2
3 // first include the ga4php.php file itself
4 $BASE_DIR = realpath(dirname(__FILE__)."/../../");
5 global $BASE_DIR;
6
7 // messy
8 require_once(dirname(__FILE__)."/../../lib/ga4php.php");
9
10
11
12 // first we check if our db exists, if not, we're not inited
13 $initState = false;
14 $backEnd = "";
15 global $initState, $backEnd;
16 if(file_exists($BASE_DIR."/gaas/gaasd/gaasd.sqlite")) {
17         // then we check if the config vars we need exist in the db
18         $backEndType = confGetVar("backend");
19         
20         if($backEndType == "AD") {
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         if($backEnd == "IN") {
53                 $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);';
54                 $dbobject->query($sql);
55         }
56         
57         $sql = 'CREATE TABLE "config" ("conf_id" INTEGER PRIMARY KEY AUTOINCREMENT,"conf_name" TEXT, "conf_value" TEXT);';
58         $dbobject->query($sql);
59         $sql = 'CREATE TABLE "radclients" ("rad_id" INTEGER PRIMARY KEY AUTOINCREMENT,"rad_name" TEXT, "rad_ip" TEXT, "rad_secret" TEXT, "rad_desc" TEXT);';
60         $dbobject->query($sql);
61         $sql = 'CREATE TABLE "hardwaretokens" ("tok_id" INTEGER PRIMARY KEY AUTOINCREMENT,"tok_name" TEXT, "tok_key" TEXT, "tok_type" TEXT);';
62         $dbobject->query($sql);
63         
64         return true;
65 }
66
67 // a function to get the database
68 function getDB()
69 {
70         $dbobject = false;
71         global $BASE_DIR, $DB_HANDLE;
72         if($DB_HANDLE != false) return $DB_HANDLE;
73         if(file_exists("$BASE_DIR/gaas/gaasd/gaasd.sqlite")) {
74                 try {
75                         $dbobject = new PDO("sqlite:$BASE_DIR/gaas/gaasd/gaasd.sqlite");
76                 } catch(PDOException $exep) {
77                         error_log("execpt on db open");
78                         return false;
79                 }
80         } else {
81                 return false;
82         }
83         
84         $DB_HANDLE = $dbobject;
85         return $dbobject;
86 }
87
88
89 function confDelVar($varname)
90 {
91         $db = getDB();
92         
93         $sql = "delete from config where conf_name='$varname'";
94         $db->query($sql);
95         
96         return true;
97 }
98
99 // a funciton to deal with Config Vars
100 function confGetVar($varname)
101 {
102         $db = getDB();
103         
104         $sql = "select conf_value from config where conf_name='$varname'";
105         
106         $result = $db->query($sql);
107         
108         if(!$result) return false;
109         
110         $val = "";
111         foreach($result as $row) {
112                 $val = $row["conf_value"];
113         }
114
115         // TOTALLY GUNNA WORK!
116         return $val;
117 }
118
119 // and a function to put vars
120 function confPutVar($varname, $value)
121 {
122         $db = getDB();
123         
124         $sql = "delete from config where conf_name='$varname'";
125         $db->query($sql);
126         
127         $sql = "insert into config values (NULL, '$varname','$value')";
128         $db->query($sql);
129         
130         // TODO: do all this better
131         return true;
132 }
133
134 // now we define our extended class
135 class gaasGA extends GoogleAuthenticator
136 {
137         
138         function getData($username)
139         {
140         }
141         
142         
143         function putData($username, $data)
144         {
145         }
146         
147         
148         function getUsers()
149         {
150         }
151 }
152 ?>