5ca73a0ffc10c06f58ceb2d57ce886d214995316
[ga4php.git] / gaas / lib / gaasdMessages.php
1 <?php
2
3 // this file defines all the messages used by gaaasd
4
5 // there are only really two status messages at this point - "init" meaning we have no been defined yet
6 // and "running" meaning we have been defined
7 function gaasStatus_server($messages)
8 {
9         global $initState, $backEnd;
10
11         $return = "init";
12         if($initState != false && $backEnd != "") {
13                 $return = "running";
14         }
15         
16         return $return;
17 }
18
19
20 function gaasInitServer_server($msg)
21 {
22         global $initState, $backEnd;
23         
24         error_log("Init server called\n");
25         // here we "init" the server, if we're ad, we attempt to connect to AD and if it all works
26         // we then create the db
27         // $m["backend"] = "AD|IN";
28         // AD expects:
29         // $m["domain"] = "somedomain.com";
30         // $m["user"] = "someuser";
31         // $m["pass"] = "somepassword";
32         // $m["userdef"] = "user definition paramaters";
33         // IN expects
34         // $m["user"] = "someuser";
35         // $m["pass"] = "somepass";
36         echo "initstate is $initState\n";
37         if($initState) {
38                 echo "true\n";
39         } else {
40                 echo "false\n";
41         }
42         if($initState) {
43                 error_log("init server called when server already init'd\n");
44                 return false;
45         }
46         
47         if($msg["backend"] == "AD") {
48                 echo "Backend is AD with params of\n";
49                 print_r($msg);
50                 echo "\n";
51                 $backEnd = "AD";
52                 // attempt connect to AD, verify creds
53                 $addom = $msg["domain"];
54                 $adlogin = $msg["user"];
55                 $adpass = $msg["pass"];
56                 $adclientdef = $msg["clientdef"];
57                 $adadmindef = $msg["admindef"];
58                 
59                 // now wee test our logins...
60                 // first look up the domain name stuff
61                 $servers = dns_get_record("_gc._tcp.$addom");
62                 if(count($servers)<1) {
63                         echo "AD servers cant be found, fail!\n";
64                 }
65                 
66                 // we should check all servers, but lets just go with 0 for now
67                 $res =  adTestLogin($addom, $adlogin, $adpass);
68                 if(!$res) {
69                         echo "AD login test failed\n";
70                         return false;
71                 } else {
72                         echo "AD login test succeeded\n";
73                 }
74                 
75                 
76                 // then
77                 createDB();
78                 confSetVal("ad.domain", $addom);
79                 confSetVal("ad.user", $adlogin);
80                 confSetVal("ad.pass", $adpass);
81                 confSetVal("ad.encryptionkey", generateHexString(32));
82                 confSetVal("ad.clientdef", $adclientdef);
83                 confSetVal("ad.admindef", $adadmindef);
84                 
85                 $initState = true;
86                 $backEnd = "AD";
87                 
88                 // and that should be it... i think cept im in a forked erg.. lets assume it works, need pain i do not.
89                 echo "its all good at the server\n";
90                 
91                 return true;
92         } else if($msg["backend"] == "IN") {
93                 // this ones simpler
94                 $backEnd = "IN";
95                 createDB();
96                 
97                 // create the user in the db
98                 $username = $msg["user"];
99                 $password = $msg["pass"];
100                 
101                 $myga = new gaasdGA();
102                 $myga->setUser($username);
103                 
104                 if($password == "") $pass = "";
105                 else $pass = hash('sha512', $password);
106                 
107                 $db = getDB();
108                 $db->query($sql = "update users set users_password='$pass' where users_username='$username'");
109                 
110                 $initState = "running";
111                 return true;
112         } else {
113                 return false;
114         }
115 }
116
117
118 function gaasSetADLogin_server($msg)
119 {
120         global $initState, $backEnd;
121         
122         if($initState != "running") {
123                 return "not in running init state";
124         }
125         
126         if($backEnd != "AD") {
127                 return "not setup as AD client";
128         }
129         
130         $addom = $msg["domain"];
131         $adlogin = $msg["user"];
132         $adpass = $msg["pass"];
133         $adclientdef = $msg["clientdef"];
134         $adadmindef = $msg["admindef"];
135         
136         $res = adTestLogin($addmo, $adlogin, $adpass);
137         if($res != 0) {
138                 return "not able to connect to AD with given cred's";
139         }
140         
141         confSetVal("ad.domain", $addom);
142         confSetVal("ad.user", $adlogin);
143         confSetVal("ad.pass", $adpass);
144         confSetVal("ad.clientdef", $adclientdef);
145         confSetVal("ad.admindef", $adadmindef);
146         
147         return true;
148         
149 }
150 ?>