add_dhcp.php 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. <?php
  2. require_once ($_SERVER['DOCUMENT_ROOT']."/inc/qauth.php");
  3. global $default_user_ou_id;
  4. global $default_hotspot_user_id;
  5. if (!empty($_GET["ip"]) and !empty($_GET["mac"])) {
  6. $ip = $_GET["ip"];
  7. $mac = mac_dotted(trim($_GET["mac"]));
  8. $dhcp_hostname = NULL;
  9. if (!empty($_GET["hostname"])) { $dhcp_hostname = trim($_GET["hostname"]); }
  10. $faction = $_GET["action"] * 1;
  11. $action = 'add';
  12. if ($faction == 1) { $action = 'add'; }
  13. if ($faction == 0) { $action = 'del'; }
  14. LOG_VERBOSE($db_link, "external dhcp request for $ip [$mac] $action");
  15. if (checkValidIp($ip) and is_our_network($db_link, $ip)) {
  16. $log_dhcp = 1;
  17. $ip_aton = ip2long($ip);
  18. //check hotspot
  19. $hotspot_user = is_hotspot($db_link,$ip);
  20. if ($hotspot_user) {
  21. LOG_DEBUG($db_link,"Hotspot user found!");
  22. $log_dhcp_hotspot = get_option($db_link,44);
  23. if (!isset($log_dhcp_hotspot)) { $log_dhcp_hotspot = 0; }
  24. $log_dhcp = !$log_dhcp_hotspot;
  25. }
  26. $auth = get_record_sql($db_link,"SELECT * FROM User_auth WHERE ip_int=" . $ip_aton . " AND deleted=0");
  27. $aid = NULL;
  28. if (!empty($auth)) {
  29. $aid = $auth['id'];
  30. LOG_VERBOSE($db_link,"Found auth for dhcp id: $aid with ip: $ip mac: $mac");
  31. }
  32. if ($action ==='add' and empty($auth)) {
  33. LOG_VERBOSE($db_link,"Add user by dhcp request ip: $ip mac: $mac");
  34. $aid = resurrection_auth($db_link, $ip, $mac, $action, $dhcp_hostname);
  35. }
  36. if ($action ==='del' and !empty($auth)) {
  37. $last_time = strtotime($auth['dhcp_time']);
  38. LOG_VERBOSE($db_link,"Delete action found for ip $ip (id: $aid, userid: ".$auth['user_id']."). Last timestamp = ".strftime('%Y-%m-%d %H-%M-%S',$last_time)." Now = ".strftime('%Y-%m-%d %H-%M-%S',time()));
  39. if ((time() - $last_time>60) and ($auth['ou_id'] == $default_user_ou_id or $auth['ou_id'] == $default_hotspot_ou_id)) {
  40. LOG_VERBOSE($db_link,"Remove dynamic user ip (id: $aid) by dhcp request for ip: $ip mac: $mac");
  41. delete_record($db_link,"User_auth","id=".$aid);
  42. $u_count=get_count_records($db_link,'User_auth','deleted=0 and user_id='.$auth['user_id']);
  43. if ($u_count == 0) {
  44. delete_record($db_link,"User_list","id=".$auth['user_id']);
  45. LOG_VERBOSE($db_link,"Remove dynamic user id: $auth['user_id'] by dhcp request");
  46. }
  47. }
  48. }
  49. if ($log_dhcp) {
  50. $dhcp_log['auth_id'] = $aid;
  51. $dhcp_log['ip'] = $ip;
  52. $dhcp_log['ip_int'] = $ip_aton;
  53. $dhcp_log['mac'] = $mac;
  54. $dhcp_log['action'] = $action;
  55. insert_record($db_link, "dhcp_log", $dhcp_log);
  56. }
  57. } else {
  58. LOG_ERROR($db_link, "$ip - wrong network!");
  59. }
  60. }
  61. unset($_GET);
  62. ?>