editauth.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. <?php
  2. require_once($_SERVER['DOCUMENT_ROOT'] . "/inc/auth.php");
  3. require_once($_SERVER['DOCUMENT_ROOT'] . "/inc/languages/" . HTML_LANG . ".php");
  4. require_once($_SERVER['DOCUMENT_ROOT'] . "/inc/idfilter.php");
  5. $msg_error = "";
  6. $old_auth_info = get_record_sql($db_link, "SELECT * FROM User_auth WHERE id=" . $id);
  7. $parent_id = $old_auth_info['user_id'];
  8. $user_info = get_record_sql($db_link, "SELECT * FROM User_list WHERE id=" . $parent_id);
  9. $parent_ou_id = $user_info['ou_id'];
  10. $user_enabled = $user_info['enabled'];
  11. if (isset($_POST["editauth"]) and !$old_auth_info['deleted']) {
  12. $ip = trim($_POST["f_ip"]);
  13. if (checkValidIp($ip)) {
  14. $ip_aton = ip2long($ip);
  15. $mac = mac_dotted($_POST["f_mac"]);
  16. //search mac
  17. $mac_exists = find_mac_in_subnet($db_link, $ip, $mac);
  18. if (isset($mac_exists) and $mac_exists['count'] >= 1 and !in_array($parent_id, $mac_exists['users_id'])) {
  19. $dup_sql = "SELECT * FROM User_list WHERE id=" . $mac_exists['users_id']['0'];
  20. $dup_info = get_record_sql($db_link, $dup_sql);
  21. $msg_error = "Mac already exists at another user in this subnet! Skip creating $ip [$mac].<br>Old user id: " . $dup_info['id'] . " login: " . $dup_info['login'];
  22. $_SESSION[$page_url]['msg'] = $msg_error;
  23. LOG_ERROR($db_link, $msg_error);
  24. header("Location: " . $_SERVER["REQUEST_URI"]);
  25. exit;
  26. }
  27. //disable dhcp for secondary ip
  28. $f_dhcp = $_POST["f_dhcp"] * 1;
  29. if (!empty($mac_exists) and in_array($parent_id, $mac_exists['users_id'])) {
  30. if ($parent_id != $mac_exists['users_id'][0]) {
  31. $f_dhcp = 0;
  32. }
  33. }
  34. //search ip
  35. $dup_ip_record = get_record_sql($db_link, "SELECT * FROM User_auth WHERE `ip_int`=$ip_aton AND id<>$id AND deleted=0");
  36. if (!empty($dup_ip_record)) {
  37. $dup_info = get_record_sql($db_link, "SELECT * FROM User_list WHERE id=" . $dup_ip_record['user_id']);
  38. $msg_error = "$ip already exists. Skip creating $ip [$mac].<br>Old user id: " . $dup_info['id'] . " login: " . $dup_info['login'];
  39. $_SESSION[$page_url]['msg'] = $msg_error;
  40. LOG_ERROR($db_link, $msg_error);
  41. header("Location: " . $_SERVER["REQUEST_URI"]);
  42. exit;
  43. }
  44. $new['ip'] = $ip;
  45. $new['ou_id'] = $parent_ou_id;
  46. $new['ip_int'] = $ip_aton;
  47. $new['mac'] = mac_dotted($_POST["f_mac"]);
  48. $new['comments'] = $_POST["f_comments"];
  49. // $new['firmware'] = $_POST["f_firmware"];
  50. $new['WikiName'] = $_POST["f_wiki"];
  51. $f_dnsname = trim($_POST["f_dns_name"]);
  52. // if (!empty($f_dnsname) and checkValidHostname($f_dnsname) and checkUniqHostname($db_link,$id,$f_dnsname)) { $new['dns_name'] = $f_dnsname; }
  53. if (!empty($f_dnsname) and checkValidHostname($f_dnsname)) {
  54. $new['dns_name'] = $f_dnsname;
  55. }
  56. if (empty($f_dnsname)) {
  57. $new['dns_name'] = '';
  58. }
  59. $new['save_traf'] = $_POST["f_save_traf"] * 1;
  60. $new['dhcp_acl'] = trim($_POST["f_acl"]);
  61. if (get_const('default_user_ou_id') == $parent_ou_id or get_const('default_hotspot_ou_id') == $parent_ou_id) {
  62. $new['nagios_handler'] = '';
  63. $new['enabled'] = 0;
  64. $new['link_check'] = 0;
  65. $new['nagios'] = 0;
  66. $new['blocked'] = 0;
  67. $new['day_quota'] = 0;
  68. $new['month_quota'] = 0;
  69. $new['queue_id'] = 0;
  70. $new['filter_group_id'] = 0;
  71. } else {
  72. $new['nagios_handler'] = $_POST["f_handler"];
  73. $new['enabled'] = get_int($_POST["f_enabled"]);
  74. $new['link_check'] = get_int($_POST["f_link"]);
  75. $new['nagios'] = get_int($_POST["f_nagios"]);
  76. $new['dhcp'] = $f_dhcp;
  77. $new['blocked'] = get_int($_POST["f_blocked"]);
  78. $new['day_quota'] = get_int($_POST["f_day_q"]);
  79. $new['month_quota'] = get_int($_POST["f_month_q"]);
  80. $new['queue_id'] = get_int($_POST["f_queue_id"]);
  81. $new['filter_group_id'] = get_int($_POST["f_group_id"]);
  82. }
  83. if ($new['nagios'] == 0) {
  84. $new['nagios_status'] = 'UP';
  85. }
  86. if (!$user_enabled) { $new['enabled']=0; }
  87. $changes = get_diff_rec($db_link, "User_auth", "id='$id'", $new, 0);
  88. if (!empty($changes)) {
  89. LOG_WARNING($db_link, "Changed record for $ip! Log: " . $changes, $id);
  90. }
  91. if (is_auth_bind_changed($db_link, $id, $ip, $mac)) {
  92. $new_id = copy_auth($db_link, $id, $new);
  93. header("Location: /admin/users/editauth.php?id=" . $new_id, TRUE, 302);
  94. exit;
  95. } else {
  96. update_record($db_link, "User_auth", "id='$id'", $new);
  97. }
  98. } else {
  99. $msg_error = "$msg_ip_error xxx.xxx.xxx.xxx";
  100. $_SESSION[$page_url]['msg'] = $msg_error;
  101. }
  102. header("Location: " . $_SERVER["REQUEST_URI"]);
  103. exit;
  104. }
  105. if (isset($_POST["moveauth"]) and !$old_auth_info['deleted']) {
  106. $new_parent_id = $_POST["f_new_parent"] * 1;
  107. $moved_auth = get_record_sql($db_link,"SELECT comments FROM User_auth WHERE id=".$id);
  108. $changes = apply_auth_rule($db_link, $moved_auth, $new_parent_id);
  109. update_record($db_link, "User_auth", "id='$id'", $changes);
  110. LOG_WARNING($db_link, "IP-address moved to another user! Applyed: " . get_rec_str($changes), $id);
  111. run_sql($db_link,"DELETE FROM auth_rules WHERE user_id=".$old_auth_info["user_id"]." AND rule='".$old_auth_info["mac"]."' AND type=2");
  112. run_sql($db_link,"DELETE FROM auth_rules WHERE user_id=".$old_auth_info["user_id"]." AND rule='".$old_auth_info["ip"]."' AND type=1");
  113. LOG_INFO($db_link,"Autorules removed for user_id: ".$old_auth_info["user_id"]." login: ".$user_info["login"]." by mac and ip");
  114. header("Location: " . $_SERVER["REQUEST_URI"]);
  115. exit;
  116. }
  117. if (isset($_POST["recovery"]) and $old_auth_info['deleted']) {
  118. $ip = trim($_POST["f_ip"]);
  119. if (checkValidIp($ip)) {
  120. $ip_aton = ip2long($ip);
  121. $mac = mac_dotted($_POST["f_mac"]);
  122. //search mac
  123. $mac_exists = find_mac_in_subnet($db_link, $ip, $mac);
  124. if (isset($mac_exists) and $mac_exists['count'] >= 1 and !in_array($parent_id, $mac_exists['users_id'])) {
  125. $dup_sql = "SELECT * FROM User_list WHERE id=" . $mac_exists['users_id']['0'];
  126. $dup_info = get_record_sql($db_link, $dup_sql);
  127. $msg_error = "Mac already exists at another user in this subnet! Skip creating $ip [$mac].<br>Old user id: " . $dup_info['id'] . " login: " . $dup_info['login'];
  128. $_SESSION[$page_url]['msg'] = $msg_error;
  129. LOG_ERROR($db_link, $msg_error);
  130. header("Location: " . $_SERVER["REQUEST_URI"]);
  131. exit;
  132. }
  133. //disable dhcp for secondary ip
  134. $f_dhcp = $_POST["f_dhcp"] * 1;
  135. if (in_array($parent_id, $mac_exists['users_id'])) {
  136. if ($parent_id != $mac_exists['users_id'][0]) {
  137. $f_dhcp = 0;
  138. }
  139. }
  140. //search ip
  141. $dup_ip_record = get_record_sql($db_link, "SELECT * FROM User_auth WHERE `ip_int`=$ip_aton AND id<>$id AND deleted=0");
  142. if (!empty($dup_ip_record)) {
  143. $dup_info = get_record_sql($db_link, "SELECT * FROM User_list WHERE id=" . $dup_ip_record['user_id']);
  144. $msg_error = "$ip already exists. Skip creating $ip [$mac].<br>Old user id: " . $dup_info['id'] . " login: " . $dup_info['login'];
  145. $_SESSION[$page_url]['msg'] = $msg_error;
  146. LOG_ERROR($db_link, $msg_error);
  147. header("Location: " . $_SERVER["REQUEST_URI"]);
  148. exit;
  149. }
  150. $new['deleted'] = 0;
  151. $parent_id = $old_auth_info['user_id'];
  152. $old_parent = get_record_sql($db_link, "SELECT * FROM User_list WHERE id=".$parent_id);
  153. if (empty($old_parent)) {
  154. $new_user_info = get_new_user_id($db_link, $ip, $mac, NULL);
  155. if ($new_user_info['user_id']) { $new_user_id = $new_user_info['user_id']; }
  156. if (empty($new_user_id)) { $new_user_id = new_user($db_link, $new_user_info); }
  157. $new['user_id'] = $new_user_id;
  158. }
  159. //save comments
  160. $new['comments']=$old_parent['comments'];
  161. if (get_const('default_user_ou_id') == $parent_ou_id or get_const('default_hotspot_ou_id') == $parent_ou_id) {
  162. $new['nagios_handler'] = '';
  163. $new['enabled'] = 0;
  164. $new['link_check'] = 0;
  165. $new['nagios'] = 0;
  166. $new['blocked'] = 0;
  167. $new['day_quota'] = 0;
  168. $new['month_quota'] = 0;
  169. $new['queue_id'] = 0;
  170. $new['filter_group_id'] = 0;
  171. } else {
  172. $new['nagios_handler'] = $_POST["f_handler"];
  173. $new['enabled'] = get_int($_POST["f_enabled"]);
  174. $new['link_check'] = get_int($_POST["f_link"]);
  175. $new['nagios'] = get_int($_POST["f_nagios"]);
  176. $new['dhcp'] = get_int($_POST["f_dhcp"]);
  177. $new['blocked'] = get_int($_POST["f_blocked"]);
  178. $new['day_quota'] = get_int($_POST["f_day_q"]);
  179. $new['month_quota'] = get_int($_POST["f_month_q"]);
  180. $new['queue_id'] = get_int($_POST["f_queue_id"]);
  181. $new['filter_group_id'] = get_int($_POST["f_group_id"]);
  182. }
  183. $changes = get_diff_rec($db_link, "User_auth", "id='$id'", $new, 0);
  184. if (!empty($changes)) {
  185. LOG_WARNING($db_link, "Recovered ip-address. Applyed: $changes", $id);
  186. }
  187. $new = apply_auth_rule($db_link, $new, $new['user_id']);
  188. update_record($db_link, "User_auth", "id='$id'", $new);
  189. } else {
  190. $msg_error = "$msg_ip_error xxx.xxx.xxx.xxx/xx";
  191. $_SESSION[$page_url]['msg'] = $msg_error;
  192. }
  193. header("Location: " . $_SERVER["REQUEST_URI"]);
  194. exit;
  195. }
  196. unset($_POST);
  197. require_once($_SERVER['DOCUMENT_ROOT'] . "/inc/header.php");
  198. $sSQL = "SELECT * FROM User_auth WHERE id=$id";
  199. $auth_info = get_record_sql($db_link, $sSQL);
  200. $device = get_record_sql($db_link, "SELECT * FROM devices WHERE user_id=" . $auth_info['user_id']);
  201. $parent_name = get_login($db_link, $auth_info['user_id']);
  202. if ($auth_info['dhcp_time'] == '0000-00-00 00:00:00') {
  203. $dhcp_str = '';
  204. } else {
  205. $dhcp_str = $auth_info['dhcp_time'] . " (" . $auth_info['dhcp_action'] . ")";
  206. }
  207. if ($auth_info['last_found'] == '0000-00-00 00:00:00') {
  208. $auth_info['last_found'] = '';
  209. }
  210. ?>
  211. <div id="cont">
  212. <?php
  213. if (!empty($_SESSION[$page_url]['msg'])) {
  214. print '<div id="msg">' . $_SESSION[$page_url]['msg'] . '</div>';
  215. unset($_SESSION[$page_url]['msg']);
  216. }
  217. print "<b>" . WEB_user_title . "&nbsp<a href=/admin/users/edituser.php?id=" . $auth_info['user_id'] . ">" . $parent_name . "</a> </b>";
  218. ?>
  219. <form name="def" action="editauth.php?id=<?php echo $id; ?>" method="post">
  220. <input type="hidden" name="id" value=<?php echo $id; ?>>
  221. <table class="data">
  222. <tr>
  223. <td width=200><?php print WEB_cell_dns_name . " &nbsp | &nbsp ";
  224. print_url("Альясы", "/admin/users/edit_alias.php?id=$id"); ?></td>
  225. <td width=200><?php print WEB_cell_comment; ?></td>
  226. <td width=70><?php print WEB_cell_enabled; ?></td>
  227. <td><?php print WEB_cell_traf; ?></td>
  228. <td></td>
  229. </tr>
  230. <tr>
  231. <td><input type="text" name="f_dns_name" value="<?php echo $auth_info['dns_name']; ?>" pattern="^(([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]*[a-zA-Z0-9])\.)*([A-Za-z0-9]|[A-Za-z0-9][A-Za-z0-9\-]*[A-Za-z0-9])$"></td>
  232. <td><input type="text" name="f_comments" value="<?php echo $auth_info['comments']; ?>"></td>
  233. <td><?php print_qa_select('f_enabled', $auth_info['enabled']); ?></td>
  234. <td><?php print_qa_select('f_save_traf', $auth_info['save_traf']); ?></td>
  235. <td></td>
  236. </tr>
  237. <tr>
  238. <td><?php print WEB_cell_ip; ?></td>
  239. <td><?php print WEB_cell_mac; ?></td>
  240. <td><?php print WEB_cell_dhcp; ?></td>
  241. <td><?php print WEB_cell_acl; ?></td>
  242. <td></td>
  243. <tr>
  244. <td><input type="text" name="f_ip" value="<?php echo $auth_info['ip']; ?>" pattern="^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$"></td>
  245. <td><input type="text" name="f_mac" value="<?php echo $auth_info['mac']; ?>" pattern="^([0-9A-Fa-f]{2}[:-]){5}[0-9A-Fa-f]{2}|([0-9a-fA-F]{4}[\\.-][0-9a-fA-F]{4}[\\.-][0-9a-fA-F]{4})|[0-9A-Fa-f]{12}$"></td>
  246. <td><?php print_qa_select('f_dhcp', $auth_info['dhcp']); ?></td>
  247. <td colspan=2><input type="text" name="f_acl" value="<?php echo $auth_info['dhcp_acl']; ?>"></td>
  248. </tr>
  249. <tr>
  250. <td><?php print WEB_cell_filter; ?></td>
  251. <td><?php print WEB_cell_shaper; ?></td>
  252. <td><?php print WEB_cell_blocked; ?></td>
  253. <td><?php print WEB_cell_perday; ?></td>
  254. <td><?php print WEB_cell_permonth; ?></td>
  255. </tr>
  256. <tr>
  257. <td><?php print_group_select($db_link, 'f_group_id', $auth_info['filter_group_id']); ?> </td>
  258. <td><?php print_queue_select($db_link, 'f_queue_id', $auth_info['queue_id']); ?> </td>
  259. <td><?php print_qa_select('f_blocked', $auth_info['blocked']); ?></td>
  260. <td><input type="text" name="f_day_q" value="<?php echo $auth_info['day_quota']; ?>" size=5></td>
  261. <td><input type="text" name="f_month_q" value="<?php echo $auth_info['month_quota']; ?>" size=5></td>
  262. </tr>
  263. <tr>
  264. <td><?php print WEB_cell_nagios_handler; ?></td>
  265. <td width=200>
  266. <?php
  267. if (!empty($auth_info['WikiName'])) {
  268. $wiki_url = rtrim(get_option($db_link, 60), '/');
  269. if (preg_match('/127.0.0.1/', $wiki_url)) {
  270. print WEB_cell_wikiname;
  271. } else {
  272. $wiki_web = rtrim(get_option($db_link, 63), '/');
  273. $wiki_web = ltrim($wiki_web, '/');
  274. $wiki_link = $wiki_url . '/' . $wiki_web . '/' . $auth_info['WikiName'];
  275. print_url(WEB_cell_wikiname, $wiki_link);
  276. }
  277. } else {
  278. print WEB_cell_wikiname;
  279. }
  280. $dev_id = get_device_by_auth($db_link, $auth_info['user_id']);
  281. if (isset($dev_id)) {
  282. print "&nbsp|&nbsp";
  283. print_url('Device', '/admin/devices/editdevice.php?id=' . $dev_id);
  284. }
  285. ?>
  286. </td>
  287. <td><?php if (empty($device) or (!empty($device) and $device['device_type'] > 2)) {
  288. print WEB_cell_nagios;
  289. } ?></td>
  290. <td><?php if (empty($device) or (!empty($device) and $device['device_type'] > 2)) {
  291. print WEB_cell_link;
  292. } ?></td>
  293. <tr>
  294. <td><input type="text" name="f_handler" value="<?php echo $auth_info['nagios_handler']; ?>"></td>
  295. <td><input type="text" name="f_wiki" value="<?php echo $auth_info['WikiName']; ?>"></td>
  296. <td><?php if (empty($device) or (!empty($device) and $device['device_type'] > 2)) {
  297. print_qa_select('f_nagios', $auth_info['nagios']);
  298. } ?></td>
  299. <td><?php if (empty($device) or (!empty($device) and $device['device_type'] > 2)) {
  300. print_qa_select('f_link', $auth_info['link_check']);
  301. } ?></td>
  302. <td></td>
  303. </tr>
  304. <tr>
  305. <td colspan=2><input type="submit" name="moveauth" value=<?php print WEB_btn_move; ?>><?php print_login_select($db_link, 'f_new_parent', $auth_info['user_id']); ?></td>
  306. <td><a href=/admin/logs/authlog.php?auth_id=<?php print $id; ?>><?php print WEB_log; ?></a></td>
  307. <?php
  308. if ($auth_info['deleted']) {
  309. print "<td >" . WEB_deleted . ": " . $auth_info['changed_time'] . "</td>";
  310. print "<td align=right><input type=\"submit\" name=\"recovery\" value='" . WEB_btn_recover . "'></td>";
  311. } else {
  312. print "<td ></td>";
  313. print "<td align=right><input type=\"submit\" name=\"editauth\" value='" . WEB_btn_save . "'></td>";
  314. }
  315. ?>
  316. </tr>
  317. </table>
  318. <table class="data">
  319. <tr>
  320. <td class="data" colspan=5><?php echo WEB_status . ":"; ?></td>
  321. </tr>
  322. <tr>
  323. <td colspan=2><?php print WEB_cell_dhcp_hostname . ": " . $auth_info['dhcp_hostname']; ?></td>
  324. <td width=100>&nbsp</td>
  325. <td align=right><?php print "Dhcp event: " . $dhcp_str; ?></td>
  326. </tr>
  327. <tr>
  328. <td><?php print WEB_cell_created . ": "; ?></td>
  329. <td><?php print $auth_info['timestamp']; ?></td>
  330. <td align=right colspan=2><?php print_url(WEB_report_by_day, "/admin/reports/authday.php?id=$id"); ?></td>
  331. </tr>
  332. <tr>
  333. <td><?php print WEB_cell_last_found . ": "; ?></td>
  334. <td><?php print $auth_info['last_found'] . "<br>"; ?></td>
  335. <td align=right><?php print WEB_cell_connection . ": "; ?></td>
  336. <td align=right><?php print get_connection($db_link, $id) . "<br>"; ?></td>
  337. </tr>
  338. </table>
  339. <?php
  340. if ($msg_error) {
  341. print "<div id='msg'><b>$msg_error</b></div><br>\n";
  342. }
  343. ?>
  344. </form>
  345. <br>
  346. <?php require_once($_SERVER['DOCUMENT_ROOT'] . "/inc/footer.php"); ?>