Radius clients
[ga4php.git] / authserver / www / admin_actions.php
1 <?php 
2 require_once("../lib/authClient.php");
3
4 $myAC = new GAAuthClient();
5
6 session_start();
7
8 if(isset($_SESSION["loggedin"])) if($_SESSION["loggedin"]) $loggedin = true;
9 else $loggedin = false;
10
11 if(isset($_REQUEST["action"])) {
12         switch($_REQUEST["action"]) {
13                 case "deleteradclient":
14                         $clientname = $_REQUEST["clientname"];
15                         $ret = $myAC->deleteRadiusClient($clientname);
16                         header("Location: ?message=".urlencode("Client Deleted"));
17                         exit(0);
18                         break;
19                 case "addradclient":
20                         $clientname = $_REQUEST["clientname"];
21                         $clientip = $_REQUEST["clientip"];
22                         $clientsecret = $_REQUEST["clientsecret"];
23                         $clientdesc = $_REQUEST["clientdesc"];
24                         $ret = $myAC->addRadiusClient($clientname, $clientip, $clientsecret, $clientdesc);
25                         if($ret === true) {
26                                 header("Location: ?message=".urlencode("Client Added"));
27                         } else if ($ret == "name") {
28                                 header("Location: ?error=".urlencode("Client Name Already Existed"));
29                         } else if ($ret == "ip") {
30                                 header("Location: ?error=".urlencode("Client IP Already Existed"));
31                         }
32                         exit(0);
33                         break;
34                 case "customtoken":
35                         $ttype = $_REQUEST["tokentype"];
36                         $tkey = $_REQUEST["tokenkey"];
37                         $username = $_REQUEST["username"];
38                         $ret1 = $myAC->setUserTokenType($username, $ttype);
39                         $ret2 = $myAC->setUserToken($username, $tkey);
40                         error_log("got, $ret1, $ret2...");
41                         break;
42                 case "synctoken":
43                         $username = $_REQUEST["username"];
44                         $tokenone = $_REQUEST["tokenone"];
45                         $tokentwo = $_REQUEST["tokentwo"];
46                         $retval = $myAC->syncUserToken($username, $tokenone, $tokentwo);
47                         error_log("retval: $retval");
48                         if($retval) {
49                                 header("Location: ?message=".urlencode("token synced"));
50                                 exit(0);
51                         } else {
52                                 header("Location: ?error=".urlencode("token not synced"));
53                                 exit(0);
54                         }
55                         
56                         break;
57                 case "recreatehotptoken":
58                         $username = $_REQUEST["username"];
59                         $myAC->addUser($username, "HOTP");
60                         header("Location: ?message=".urlencode("seemed to work?"));
61                         break;
62                 case "recreatetotptoken":
63                         $username = $_REQUEST["username"];
64                         $myAC->addUser($username, "TOTP");
65                         header("Location: ?message=".urlencode("seemed to work?"));
66                         break;
67                 case "deletetoken":
68                         $username = $_REQUEST["username"];
69                         $myAC->deleteUserToken($username);
70                         header("Location: ?message=".urlencode("seemed to work?"));
71                         break;
72                 case "edituser":
73                         $username = $_REQUEST["username"];
74                         if($_REQUEST["original_real"] != $_REQUEST["realname"]) {
75                                 $myAC->setUserRealName($username, $_REQUEST["realname"]);
76                         }
77                         if($_REQUEST["password"] != "") {
78                                 if($_REQUEST["password"]!=$_REQUEST["password_conf"]) {
79                                         header("Location: ?message=confirmfalse");
80                                 } else {
81                                         $myAC->setUserPass($username, $_REQUEST["password"]);
82                                 }
83                         }
84                         break;
85                 case "login":
86                         $username = $_REQUEST["username"];
87                         $password = $_REQUEST["password"];
88                         
89                         if($myAC->authUserPass($username, $password)) {
90                                 $_SESSION["loggedin"] = true;
91                                 $_SESSION["username"] = $username;
92                                 header("Location: admin.php");
93                         } else {
94                                 header("Location: admin.php?error=".urlencode("Login Failed"));
95                         }
96                         
97                         exit(0);
98                         break;
99                 case "logout":
100                         $_SESSION["loggedin"] = false;
101                         $_SESSION["username"] = "";
102                         header("Location: admin.php");
103                         exit(0);
104                         break;
105                 case "createuser":
106                         $username = $_REQUEST["username"];
107                         $users = explode(",",$username);
108                         foreach($users as $user) {
109                                 $user = trim($user);
110                                 error_log("createing, $user\n");
111                                 if($user != "" && strlen($user)>2) $myAC->addUser($user);
112                         }
113                         header("Location: admin.php");
114                         exit(0);
115                         break;
116                 case "update":
117                         error_log("would update");
118                         $err = print_r($_REQUEST, true);
119                         error_log("req: $err\n");
120                         $username = $_REQUEST["username"];
121                         if($_REQUEST["realname"]!="") {
122                                 $myAC->setUserRealName($username, $_REQUEST["realname"]);
123                         }
124                         if($_REQUEST["password"]!= "") {
125                                 $myAC->setUserPass($username, $_REQUEST["password"]);
126                         }
127                         break;
128                 case "delete":
129                         $username = $_REQUEST["username"];
130                         $myAC->deleteUser($username);
131                         break;
132                 case "deletepass":
133                         $username = $_REQUEST["username"];
134                         $myAC->setUserPass($username, "");
135                         break;
136                 case "getotkimg":
137                         $otk = $_REQUEST["otk"];
138                         $username = $_REQUEST["username"];
139                         error_log("requesting otk, $otk");
140                         $otk_img = $myAC->getOtkPng($username,$otk);
141                         header("Content-type: image/png");
142                         echo $otk_img;
143                         exit(0);
144                         break;
145         }
146 }
147 ?>