trying to figure out how to store data in AD
[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";
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                         return false;
67                 }
68                 
69                 
70                 // then
71                 createDB();
72                 confSetVal("ad.domain", $addom);
73                 confSetVal("ad.user", $adlogin);
74                 confSetVal("ad.pass", $adpass);
75                 confSetVal("ad.encryptionkey", generateHexString(32));
76                 confSetVal("ad.clientdef", $adclientdef);
77                 confSetVal("ad.admindef", $adadmindef);
78                 confSetVal("backend", "AD");
79                 
80                 $initState = true;
81                 $backEnd = "AD";
82                 
83                 // and that should be it... i think cept im in a forked erg.. lets assume it works, need pain i do not.
84                 return true;
85         } else if($msg["backend"] == "IN") {
86                 // this ones simpler
87                 $backEnd = "IN";
88                 createDB();
89                 
90                 // create the user in the db
91                 $username = $msg["user"];
92                 $password = $msg["pass"];
93                 
94                 $myga = new gaasdGA();
95                 $myga->setUser($username);
96                 
97                 if($password == "") $pass = "";
98                 else $pass = hash('sha512', $password);
99                 
100                 $db = getDB();
101                 $db->query($sql = "update users set users_password='$pass' where users_username='$username'");
102                 
103                 $initState = "running";
104                 return true;
105         } else {
106                 return false;
107         }
108 }
109
110
111 function gaasSetADLogin_server($msg)
112 {
113         global $initState, $backEnd;
114         
115         if($initState != "running") {
116                 return "not in running init state";
117         }
118         
119         if($backEnd != "AD") {
120                 return "not setup as AD client";
121         }
122         
123         $addom = $msg["domain"];
124         $adlogin = $msg["user"];
125         $adpass = $msg["pass"];
126         $adclientdef = $msg["clientdef"];
127         $adadmindef = $msg["admindef"];
128         
129         $res = adTestLogin($addmo, $adlogin, $adpass);
130         if($res != 0) {
131                 return "not able to connect to AD with given cred's";
132         }
133         
134         confSetVal("ad.domain", $addom);
135         confSetVal("ad.user", $adlogin);
136         confSetVal("ad.pass", $adpass);
137         confSetVal("ad.clientdef", $adclientdef);
138         confSetVal("ad.admindef", $adadmindef);
139         
140         return true;
141         
142 }
143 ?>