cant quite figure out why the client messages for init server arent working correctly
[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                 $backEnd = "AD";
49                 // attempt connect to AD, verify creds
50                 $addom = $msg["domain"];
51                 $adlogin = $msg["user"];
52                 $adpass = $msg["pass"];
53                 $adclientdef = $msg["clientdef"];
54                 $adadmindef = $msg["admindef"];
55                 
56                 // now wee test our logins...
57                 // first look up the domain name stuff
58                 $servers = dns_get_record("_gc._tcp.$addom");
59                 if(count($servers)<1) {
60                         echo "AD servers cant be found, fail!\n";
61                 }
62                 
63                 // we should check all servers, but lets just go with 0 for now
64                 $res =  adTestLogin($addom, $adlogin, $adpass);
65                 if(!$res) {
66                         echo "AD login test failed\n";
67                         return false;
68                 } else {
69                         echo "AD login test succeeded\n";
70                 }
71                 
72                 
73                 // then
74                 createDB();
75                 confSetVal("ad.domain", $addom);
76                 confSetVal("ad.user", $adlogin);
77                 confSetVal("ad.pass", $adpass);
78                 confSetVal("ad.encryptionkey", generateHexString(32));
79                 confSetVal("ad.clientdef", $adclientdef);
80                 confSetVal("ad.admindef", $adadmindef);
81                 
82                 $initState = true;
83                 $backEnd = "AD";
84                 
85                 // and that should be it... i think cept im in a forked erg.. lets assume it works, need pain i do not.
86                 
87                 return true;
88         } else if($msg["backend"] == "IN") {
89                 // this ones simpler
90                 $backEnd = "IN";
91                 createDB();
92                 
93                 // create the user in the db
94                 $username = $msg["user"];
95                 $password = $msg["pass"];
96                 
97                 $myga = new gaasdGA();
98                 $myga->setUser($username);
99                 
100                 if($password == "") $pass = "";
101                 else $pass = hash('sha512', $password);
102                 
103                 $db = getDB();
104                 $db->query($sql = "update users set users_password='$pass' where users_username='$username'");
105                 
106                 $initState = "running";
107                 return true;
108         } else {
109                 return false;
110         }
111 }
112
113
114 function gaasSetADLogin_server($msg)
115 {
116         global $initState, $backEnd;
117         
118         if($initState != "running") {
119                 return "not in running init state";
120         }
121         
122         if($backEnd != "AD") {
123                 return "not setup as AD client";
124         }
125         
126         $addom = $msg["domain"];
127         $adlogin = $msg["user"];
128         $adpass = $msg["pass"];
129         $adclientdef = $msg["clientdef"];
130         $adadmindef = $msg["admindef"];
131         
132         $res = adTestLogin($addmo, $adlogin, $adpass);
133         if($res != 0) {
134                 return "not able to connect to AD with given cred's";
135         }
136         
137         confSetVal("ad.domain", $addom);
138         confSetVal("ad.user", $adlogin);
139         confSetVal("ad.pass", $adpass);
140         confSetVal("ad.clientdef", $adclientdef);
141         confSetVal("ad.admindef", $adadmindef);
142         
143         return true;
144         
145 }
146 ?>