editauth.php 19 KB

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