editauth.php 17 KB

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