mysql.pm 53 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591
  1. package eyelib::mysql;
  2. #
  3. # Copyright (C) Roman Dmitiriev, rnd@rajven.ru
  4. #
  5. use utf8;
  6. use strict;
  7. use English;
  8. use FindBin '$Bin';
  9. use lib "$Bin";
  10. use base 'Exporter';
  11. use vars qw(@EXPORT @ISA);
  12. use eyelib::config;
  13. use eyelib::main;
  14. use Net::Patricia;
  15. use eyelib::net_utils;
  16. use Data::Dumper;
  17. use DateTime;
  18. use POSIX;
  19. use DBI;
  20. our @ISA = qw(Exporter);
  21. our @EXPORT = qw(
  22. batch_db_sql
  23. batch_db_sql_cached
  24. db_log_warning
  25. db_log_debug
  26. db_log_error
  27. db_log_info
  28. db_log_verbose
  29. delete_record
  30. do_sql
  31. Get_Variable
  32. Set_Variable
  33. Del_Variable
  34. get_count_records
  35. get_record_sql
  36. get_records_sql
  37. get_device_by_ip
  38. get_diff_rec
  39. get_id_record
  40. get_new_user_id
  41. is_hotspot
  42. GetNowTime
  43. GetUnixTimeByStr
  44. GetTimeStrByUnixTime
  45. get_option
  46. get_subnets_ref
  47. init_db
  48. init_option
  49. insert_record
  50. apply_device_lock
  51. set_lock_discovery
  52. unset_lock_discovery
  53. IpToStr
  54. unbind_ports
  55. resurrection_auth
  56. new_auth
  57. StrToIp
  58. get_first_line
  59. update_dns_record
  60. update_dns_record_by_dhcp
  61. update_dns_hostname
  62. update_dns_ptr
  63. update_record
  64. write_db_log
  65. set_changed
  66. recalc_quotes
  67. clean_variables
  68. $add_rules
  69. $L_WARNING
  70. $L_INFO
  71. $L_DEBUG
  72. $L_ERROR
  73. $L_VERBOSE
  74. );
  75. BEGIN
  76. {
  77. #---------------------------------------------------------------------------------------------------------------
  78. our $add_rules;
  79. our $L_ERROR = 0;
  80. our $L_WARNING = 1;
  81. our $L_INFO = 2;
  82. our $L_VERBOSE = 3;
  83. our $L_DEBUG = 255;
  84. our %acl_fields = (
  85. 'ip' => '1',
  86. 'ip_int' => '1',
  87. 'enabled'=>'1',
  88. 'dhcp'=>'1',
  89. 'filter_group_id'=>'1',
  90. 'deleted'=>'1',
  91. 'dhcp_acl'=>'1',
  92. 'queue_id'=>'1',
  93. 'mac'=>'1',
  94. 'blocked'=>'1'
  95. );
  96. our %dhcp_fields = (
  97. 'ip' => '1',
  98. 'dhcp'=>'1',
  99. 'deleted'=>'1',
  100. 'mac'=>'1',
  101. );
  102. #---------------------------------------------------------------------------------------------------------------
  103. sub StrToIp{
  104. return unpack('N',pack('C4',split(/\./,$_[0])));
  105. }
  106. #---------------------------------------------------------------------------------------------------------------
  107. sub IpToStr{
  108. my $nIP = shift;
  109. my $res = (($nIP>>24) & 255) .".". (($nIP>>16) & 255) .".". (($nIP>>8) & 255) .".". ($nIP & 255);
  110. return $res;
  111. }
  112. #---------------------------------------------------------------------------------------------------------------
  113. sub batch_db_sql {
  114. my $db=shift;
  115. my $batch_sql=shift;
  116. return if (!$db);
  117. $db->{AutoCommit} = 0;
  118. my $apply = 0;
  119. my $sth;
  120. my @msg = ();
  121. if (ref($batch_sql) eq 'ARRAY') { @msg = @$batch_sql; } else { @msg = split("\n",$batch_sql); }
  122. foreach my $sSQL (@msg) {
  123. next if (!$sSQL);
  124. $sth = $db->prepare($sSQL) or die "Unable to prepare $sSQL" . $db->errstr;
  125. $sth->execute() or die "Unable to prepare $sSQL" . $db->errstr;
  126. $apply = 1;
  127. }
  128. if ($apply) { $sth->finish(); }
  129. $db->{AutoCommit} = 1;
  130. }
  131. #---------------------------------------------------------------------------------------------------------------
  132. sub batch_db_sql_cached {
  133. my $db = DBI->connect("dbi:mysql:database=$DBNAME;host=$DBHOST","$DBUSER","$DBPASS", { RaiseError => 0, AutoCommit => 0 });
  134. if ( !defined $db ) { die "Cannot connect to mySQL server: $DBI::errstr\n"; }
  135. $db->do('SET NAMES utf8mb4');
  136. $db->{'mysql_enable_utf8'} = 1;
  137. $db->{'mysql_auto_reconnect'} = 1;
  138. my $table= shift;
  139. my $batch_sql=shift;
  140. return if (!$db);
  141. my @msg = ();
  142. if (ref($batch_sql) eq 'ARRAY') { @msg = @$batch_sql; } else { @msg = split("\n",$batch_sql); }
  143. my $sth = $db->prepare_cached($table) or die "Unable to prepare:" . $db->errstr;
  144. foreach my $sSQL (@msg) {
  145. next if (!$sSQL);
  146. $sth->execute(@$sSQL) or die "Unable to execute:" . $db->errstr;
  147. }
  148. $db->commit();
  149. $db->disconnect();
  150. }
  151. #---------------------------------------------------------------------------------------------------------------
  152. sub do_sql {
  153. my $db=shift;
  154. my $sql=shift;
  155. return if (!$db);
  156. return if (!$sql);
  157. if ($sql!~/^select /i) { db_log_debug($db,$sql); }
  158. my $sql_prep = $db->prepare($sql) or die "Unable to prepare $sql: " . $db->errstr;
  159. my $sql_ref;
  160. $sql_prep->execute() or die "Unable to execute $sql: " . $db->errstr;
  161. if ($sql=~/^insert/i) { $sql_ref = $sql_prep->{mysql_insertid}; }
  162. if ($sql=~/^select /i) { $sql_ref = $sql_prep->fetchall_arrayref() or die "Unable to select $sql: " . $db->errstr; };
  163. $sql_prep->finish();
  164. return $sql_ref;
  165. }
  166. #---------------------------------------------------------------------------------------------------------------
  167. sub get_first_line {
  168. my $msg = shift;
  169. if (!$msg) { return; }
  170. if ($msg=~ /(.*)(\n|\<br\>)/) {
  171. $msg = $1 if ($1);
  172. chomp($msg);
  173. }
  174. return $msg;
  175. }
  176. #---------------------------------------------------------------------------------------------------------------
  177. sub write_db_log {
  178. my $db=shift;
  179. my $msg=shift;
  180. my $level = shift || $L_VERBOSE;
  181. my $auth_id = shift || 0;
  182. return if (!$db);
  183. return if (!$msg);
  184. $msg=~s/[\'\"]//g;
  185. my $db_log = 0;
  186. if (!$db) { $db_log = 0; }
  187. if ($level eq $L_ERROR and $log_level >= $L_ERROR) { log_error($msg); $db_log = 1; }
  188. if ($level eq $L_WARNING and $log_level >= $L_WARNING) { log_warning($msg); $db_log = 1; }
  189. if ($level eq $L_INFO and $log_level >= $L_INFO) { log_info($msg); $db_log = 1; }
  190. if ($level eq $L_DEBUG and $log_level >= $L_DEBUG) { log_debug($msg); $db_log = 1; }
  191. if ($db_log) {
  192. my $history_sql="INSERT INTO syslog(customer,message,level,auth_id) VALUES(".$db->quote($MY_NAME).",".$db->quote($msg).",$level,$auth_id)";
  193. my $history_rf=$db->prepare($history_sql) or die "Unable to prepare $history_sql:" . $db->errstr;
  194. $history_rf->execute() or die "Unable to execute $history_sql: " . $db->errstr;
  195. }
  196. }
  197. #---------------------------------------------------------------------------------------------------------------
  198. sub db_log_debug {
  199. my $db = shift;
  200. my $msg = shift;
  201. my $id = shift;
  202. if ($debug) { write_db_log($db,$msg,$L_DEBUG,$id); }
  203. }
  204. #---------------------------------------------------------------------------------------------------------------
  205. sub db_log_error {
  206. my $db = shift;
  207. my $msg = shift;
  208. if ($log_level >= $L_ERROR) {
  209. sendEmail("ERROR! ".get_first_line($msg),$msg,1);
  210. write_db_log($db,$msg,$L_ERROR);
  211. }
  212. }
  213. #---------------------------------------------------------------------------------------------------------------
  214. sub db_log_info {
  215. my $db = shift;
  216. my $msg = shift;
  217. my $id = shift;
  218. if ($log_level >= $L_INFO) { write_db_log($db,$msg,$L_INFO,$id); }
  219. }
  220. #---------------------------------------------------------------------------------------------------------------
  221. sub db_log_verbose {
  222. my $db = shift;
  223. my $msg = shift;
  224. my $id = shift;
  225. if ($log_level >= $L_VERBOSE) { write_db_log($db,$msg,$L_VERBOSE,$id); }
  226. }
  227. #---------------------------------------------------------------------------------------------------------------
  228. sub db_log_warning {
  229. my $db = shift;
  230. my $msg = shift;
  231. my $id = shift;
  232. if ($log_level >= $L_WARNING) {
  233. sendEmail("WARN! ".get_first_line($msg),$msg,1);
  234. write_db_log($db,$msg,$L_WARNING,$id);
  235. }
  236. }
  237. #---------------------------------------------------------------------------------------------------------------
  238. sub init_db {
  239. # Create new database handle. If we can't connect, die()
  240. my $db = DBI->connect("dbi:mysql:database=$DBNAME;host=$DBHOST","$DBUSER","$DBPASS", { RaiseError => 0, AutoCommit => 1 });
  241. if ( !defined $db ) { die "Cannot connect to mySQL server: $DBI::errstr\n"; }
  242. $db->do('SET NAMES utf8mb4');
  243. $db->{'mysql_enable_utf8'} = 1;
  244. $db->{'mysql_auto_reconnect'} = 1;
  245. return $db;
  246. }
  247. #---------------------------------------------------------------------------------------------------------------
  248. sub get_count_records {
  249. my $db = shift;
  250. my $table = shift;
  251. my $filter = shift;
  252. my $result = 0;
  253. return $result if (!$db);
  254. return $result if (!$table);
  255. my $sSQL='SELECT COUNT(*) as rec_cnt FROM '.$table;
  256. if ($filter) { $sSQL=$sSQL." WHERE ".$filter; }
  257. my $record = get_record_sql($db,$sSQL);
  258. if ($record->{rec_cnt}) { $result = $record->{rec_cnt}; }
  259. return $result;
  260. }
  261. #---------------------------------------------------------------------------------------------------------------
  262. sub get_id_record {
  263. my $db = shift;
  264. my $table = shift;
  265. my $filter = shift;
  266. my $result = 0;
  267. return $result if (!$db);
  268. return $result if (!$table);
  269. my $record = get_record_sql($db,"SELECT id FROM $table WHERE $filter");
  270. if ($record->{id}) { $result = $record->{id}; }
  271. return $result;
  272. }
  273. #---------------------------------------------------------------------------------------------------------------
  274. sub get_records_sql {
  275. my $db = shift;
  276. my $table = shift;
  277. my @result;
  278. return @result if (!$db);
  279. return @result if (!$table);
  280. my $list = $db->prepare( $table ) or die "Unable to prepare $table:" . $db->errstr;
  281. $list->execute() or die "Unable to execute $table: " . $db->errstr;
  282. while(my $row_ref = $list->fetchrow_hashref()) { push(@result,$row_ref); }
  283. $list->finish();
  284. return @result;
  285. }
  286. #---------------------------------------------------------------------------------------------------------------
  287. sub get_record_sql {
  288. my $db = shift;
  289. my $tsql = shift;
  290. my @result;
  291. return @result if (!$db);
  292. return @result if (!$tsql);
  293. $tsql.=' LIMIT 1';
  294. my $row_ref;
  295. eval {
  296. my $list = $db->prepare($tsql) or die "Unable to prepare $tsql: " . $db->errstr;
  297. $list->execute() or die "Unable to execute $tsql: " . $db->errstr;
  298. $row_ref = $list->fetchrow_hashref();
  299. $list->finish();
  300. };
  301. if ($@) {
  302. log_error("Error apply sql: $tsql err:".$@);
  303. die "Error apply sql: $tsql";
  304. }
  305. return $row_ref;
  306. }
  307. #---------------------------------------------------------------------------------------------------------------
  308. sub unbind_ports {
  309. my $db = shift;
  310. my $device_id = shift;
  311. return if (!$db);
  312. return if (!$device_id);
  313. my @target = get_records_sql($db, "SELECT U.target_port_id,U.id FROM device_ports U WHERE U.device_id=".$device_id);
  314. foreach my $row (@target) {
  315. do_sql($db, "UPDATE device_ports SET target_port_id=0 WHERE target_port_id=".$row->{id});
  316. do_sql($db, "UPDATE device_ports SET target_port_id=0 WHERE id=".$row->{id});
  317. }
  318. }
  319. #---------------------------------------------------------------------------------------------------------------
  320. sub get_diff_rec {
  321. my $db = shift;
  322. my $table = shift;
  323. my $value = shift;
  324. my $filter = shift;
  325. return if (!$db);
  326. return if (!$table);
  327. return if (!$filter);
  328. my $old_value = get_record_sql($db,"SELECT * FROM $table WHERE $filter");
  329. my $result='';
  330. foreach my $field (keys %$value) {
  331. if (!$value->{$field}) { $value->{$field}=''; }
  332. if (!$old_value->{$field}) { $old_value->{$field}=''; }
  333. if ($value->{$field}!~/^$old_value->{$field}$/) { $result = $result." $field => $value->{$field} (old: $old_value->{$field}),"; }
  334. }
  335. $result=~s/,$//;
  336. return $result;
  337. }
  338. #---------------------------------------------------------------------------------------------------------------
  339. sub update_record {
  340. my $db = shift;
  341. my $table = shift;
  342. my $record = shift;
  343. my $filter = shift;
  344. return if (!$db);
  345. return if (!$table);
  346. return if (!$filter);
  347. my $old_record = get_record_sql($db,"SELECT * FROM $table WHERE $filter");
  348. my $diff='';
  349. my $change_str='';
  350. my $found_changed=0;
  351. my $auth_id = 0;
  352. if ($table eq "User_auth") {
  353. $auth_id = $old_record->{'id'};
  354. foreach my $field (keys %$record) {
  355. if (exists $acl_fields{$field}) { $record->{changed}="1"; }
  356. if (exists $dhcp_fields{$field}) { $record->{dhcp_changed}="1"; }
  357. }
  358. }
  359. foreach my $field (keys %$record) {
  360. if (!defined $record->{$field}) { $record->{$field}=''; }
  361. if (!defined $old_record->{$field}) { $old_record->{$field}=''; }
  362. my $old_value = quotemeta($old_record->{$field});
  363. my $new_value = $record->{$field};
  364. $new_value=~s/\'//g;
  365. $new_value=~s/\"//g;
  366. if ($new_value!~/^$old_value$/) {
  367. $diff = $diff." $field => $record->{$field} (old: $old_record->{$field}),";
  368. $change_str = $change_str." `$field`=".$db->quote($record->{$field}).",";
  369. $found_changed++;
  370. }
  371. }
  372. if ($found_changed) {
  373. $change_str=~s/\,$//;
  374. $diff=~s/\,$//;
  375. if ($table eq 'User_auth') { $change_str .= ", `changed_time`='".GetNowTime()."'"; }
  376. my $sSQL = "UPDATE $table SET $change_str WHERE $filter";
  377. db_log_debug($db,'Change table '.$table.' for '.$filter.' set: '.$diff,$auth_id);
  378. do_sql($db,$sSQL);
  379. } else {
  380. db_log_debug($db,'Nothing change. Skip update.');
  381. }
  382. return 1;
  383. }
  384. #---------------------------------------------------------------------------------------------------------------
  385. sub insert_record {
  386. my $db = shift;
  387. my $table = shift;
  388. my $record = shift;
  389. return if (!$db);
  390. return if (!$table);
  391. my $change_str='';
  392. my $fields='';
  393. my $values='';
  394. my $new_str='';
  395. if ($table eq 'User_auth') {
  396. foreach my $field (keys %$record) {
  397. if (exists $acl_fields{$field}) { $record->{changed}="1"; }
  398. if (exists $dhcp_fields{$field}) { $record->{dhcp_changed}="1"; }
  399. }
  400. }
  401. foreach my $field (keys %$record) {
  402. if (!defined $record->{$field}) { $record->{$field}=''; }
  403. my $new_value = $record->{$field};
  404. $new_value=~s/\'//g;
  405. $new_value=~s/\"//g;
  406. $fields = $fields."`$field`,";
  407. $values = $values." ".$db->quote($new_value).",";
  408. $new_str = $new_str." $field => $new_value,";
  409. }
  410. $fields=~s/,$//;
  411. $values=~s/,$//;
  412. $new_str=~s/,$//;
  413. my $sSQL = "INSERT INTO $table($fields) VALUES($values)";
  414. my $result = do_sql($db,$sSQL);
  415. if ($result) { $new_str='id: '.$result.' '.$new_str; }
  416. db_log_debug($db,'Add record to table '.$table.' '.$new_str);
  417. return $result;
  418. }
  419. #---------------------------------------------------------------------------------------------------------------
  420. sub delete_record {
  421. my $db = shift;
  422. my $table = shift;
  423. my $filter = shift;
  424. return if (!$db);
  425. return if (!$table);
  426. return if (!$filter);
  427. my $old_record = get_record_sql($db,"SELECT * FROM $table WHERE $filter");
  428. my $diff='';
  429. foreach my $field (keys %$old_record) {
  430. if (!$old_record->{$field}) { $old_record->{$field}=''; }
  431. $diff = $diff." $field => $old_record->{$field},";
  432. }
  433. $diff=~s/,$//;
  434. db_log_debug($db,'Delete record from table '.$table.' value: '.$diff);
  435. #never delete user ip record!
  436. if ($table eq 'User_auth') {
  437. my $sSQL = "UPDATE User_auth SET changed=1, deleted=1, changed_time='".GetNowTime()."' WHERE ".$filter;
  438. do_sql($db,$sSQL);
  439. } else {
  440. my $sSQL = "DELETE FROM ".$table." WHERE ".$filter;
  441. do_sql($db,$sSQL);
  442. }
  443. }
  444. #---------------------------------------------------------------------------------------------------------------
  445. sub GetNowTime {
  446. my ($sec,$min,$hour,$day,$month,$year,$zone) = localtime(time());
  447. $month += 1;
  448. $year += 1900;
  449. my $now_str=sprintf "%04d-%02d-%02d %02d:%02d:%02d",$year,$month,$day,$hour,$min,$sec;
  450. return $now_str;
  451. }
  452. #---------------------------------------------------------------------------------------------------------------
  453. sub is_hotspot {
  454. my $db = shift;
  455. my $ip = shift;
  456. my $users = new Net::Patricia;
  457. #check hotspot
  458. my @ip_rules = get_records_sql($db,'SELECT * FROM subnets WHERE hotspot=1 AND LENGTH(subnet)>0');
  459. foreach my $row (@ip_rules) { $users->add_string($row->{subnet}); }
  460. if ($users->match_string($ip)) { return 1; }
  461. return 0;
  462. }
  463. #---------------------------------------------------------------------------------------------------------------
  464. sub get_new_user_id {
  465. my $db = shift;
  466. my $ip = shift;
  467. my $mac = shift;
  468. my $hostname = shift;
  469. my $result;
  470. #check user rules
  471. $mac = mac_simplify($mac);
  472. $result->{ip} = $ip;
  473. $result->{mac} = mac_splitted($mac);
  474. $result->{dhcp_hostname} = $hostname;
  475. $result->{ou_id}=undef;
  476. $result->{user_id}=undef;
  477. #check ip
  478. if (defined $ip and $ip) {
  479. my $users = new Net::Patricia;
  480. #check ip rules
  481. my @ip_rules = get_records_sql($db,'SELECT * FROM auth_rules WHERE type=1 and LENGTH(rule)>0 AND user_id IS NOT NULL');
  482. foreach my $row (@ip_rules) { eval { $users->add_string($row->{rule},$row->{user_id}); }; }
  483. if ($users->match_string($ip)) { $result->{user_id}=$users->match_string($ip); }
  484. }
  485. #check mac
  486. if (defined $mac and $mac) {
  487. my @user_rules=get_records_sql($db,'SELECT * FROM auth_rules WHERE type=2 AND LENGTH(rule)>0 AND user_id IS NOT NULL');
  488. foreach my $user (@user_rules) {
  489. my $rule = mac_simplify($user->{rule});
  490. if ($mac=~/$rule/i) { $result->{user_id}=$user->{user_id}; }
  491. }
  492. }
  493. #check hostname
  494. if (defined $hostname and $hostname) {
  495. my @user_rules=get_records_sql($db,'SELECT * FROM auth_rules WHERE type=3 AND LENGTH(rule)>0 AND user_id IS NOT NULL');
  496. foreach my $user (@user_rules) {
  497. if ($hostname=~/$user->{rule}/i) { $result->{user_id}=$user->{user_id}; }
  498. }
  499. }
  500. #
  501. if ($result->{user_id}) { return $result; }
  502. #check ou rules
  503. #check ip
  504. if (defined $ip and $ip) {
  505. my $users = new Net::Patricia;
  506. #check hotspot
  507. my @ip_rules = get_records_sql($db,'SELECT * FROM subnets WHERE hotspot=1 AND LENGTH(subnet)>0');
  508. foreach my $row (@ip_rules) { $users->add_string($row->{subnet},$default_hotspot_ou_id); }
  509. if ($users->match_string($ip)) { $result->{ou_id}=$users->match_string($ip); }
  510. #check ip rules
  511. @ip_rules = get_records_sql($db,'SELECT * FROM auth_rules WHERE type=1 and LENGTH(rule)>0 AND ou_id IS NOT NULL');
  512. foreach my $row (@ip_rules) { eval { $users->add_string($row->{rule},$row->{ou_id}); }; }
  513. if ($users->match_string($ip)) { $result->{ou_id}=$users->match_string($ip); }
  514. }
  515. #check mac
  516. if (defined $mac and $mac) {
  517. my @user_rules=get_records_sql($db,'SELECT * FROM auth_rules WHERE type=2 AND LENGTH(rule)>0 AND ou_id IS NOT NULL');
  518. foreach my $user (@user_rules) {
  519. my $rule = mac_simplify($user->{rule});
  520. if ($mac=~/$rule/i) { $result->{ou_id}=$user->{ou_id}; }
  521. }
  522. }
  523. #check hostname
  524. if (defined $hostname and $hostname) {
  525. my @user_rules=get_records_sql($db,'SELECT * FROM auth_rules WHERE type=3 AND LENGTH(rule)>0 AND ou_id IS NOT NULL');
  526. foreach my $user (@user_rules) {
  527. if ($hostname=~/$user->{rule}/i) { $result->{ou_id}=$user->{ou_id}; }
  528. }
  529. }
  530. if (!$result->{ou_id}) { $result->{ou_id}=$default_user_ou_id; }
  531. return $result;
  532. }
  533. #---------------------------------------------------------------------------------------------------------------
  534. sub set_changed {
  535. my $db = shift;
  536. my $id = shift;
  537. return if (!$db or !$id);
  538. my $update_record;
  539. $update_record->{changed}=1;
  540. update_record($db,'User_auth',$update_record,"id=$id");
  541. }
  542. #---------------------------------------------------------------------------------------------------------------
  543. sub update_dns_record {
  544. my $hdb = shift;
  545. my $auth_record = shift;
  546. if (!$auth_record->{dns_name}) { return 0; }
  547. my $ad_zone = get_option($hdb,33);
  548. #update dns block
  549. my $fqdn_static=lc($auth_record->{dns_name});
  550. $fqdn_static=~s/\.$ad_zone$//i;
  551. $fqdn_static=~s/\.$//;
  552. #skip update unknown domain
  553. if ($fqdn_static =~/\./) { return 0; }
  554. my $ad_dns = get_option($hdb,3);
  555. my $enable_ad_dns_update = ($ad_zone and $ad_dns and $config_ref{enable_dns_updates});
  556. log_debug("Auth record: ".Dumper($auth_record));
  557. log_debug("enable_ad_dns_update: ".$enable_ad_dns_update);
  558. log_debug("DNS update flags - zone: ".$ad_zone.", dns: ".$ad_dns.", enable_ad_dns_update: ".$enable_ad_dns_update);
  559. my $maybe_update_dns=( $enable_ad_dns_update and $office_networks->match_string($auth_record->{ip}) );
  560. if (!$maybe_update_dns) {
  561. db_log_debug($hdb,"FOUND Auth_id: $auth_record->{id}. DNS update don't needed.");
  562. return 0;
  563. }
  564. log_debug("DNS update enabled.");
  565. $fqdn_static=lc($fqdn_static.'.'.$ad_zone);
  566. db_log_info($hdb,"Update dns request for auth_id: $auth_record->{id} $fqdn_static => $auth_record->{ip}");
  567. #check exists static dns name
  568. my $static_exists = 0;
  569. my $static_ok = 0;
  570. my $static_ref = '';
  571. my @dns_record=ResolveNames($fqdn_static,$dns_server);
  572. $static_exists = (scalar @dns_record>0);
  573. if ($static_exists) {
  574. $static_ref = join(' ',@dns_record);
  575. foreach my $dns_a (@dns_record) {
  576. if ($dns_a=~/^$auth_record->{ip}$/) { $static_ok = 1; }
  577. }
  578. }
  579. db_log_debug($hdb,"Dns record for static record $fqdn_static: $static_ok");
  580. if (!$static_ok) {
  581. if (!$static_exists) {
  582. db_log_info($hdb,"Static dns hostname defined but not found. Create it ($fqdn_static => $auth_record->{ip})!");
  583. } else {
  584. db_log_warning($hdb,"Static record mismatch! Expected $fqdn_static => $auth_record->{ip}, recivied: $static_ref");
  585. }
  586. update_dns_hostname($fqdn_static,$auth_record->{ip},$ad_zone,$ad_dns,$hdb);
  587. update_dns_ptr($fqdn_static,$auth_record->{ip},$ad_zone,$ad_dns,$hdb);
  588. } else {
  589. db_log_debug($hdb,"Static record for $fqdn_static [$static_ok] correct.");
  590. }
  591. }
  592. #---------------------------------------------------------------------------------------------------------------
  593. sub update_dns_record_by_dhcp {
  594. my $hdb = shift;
  595. my $dhcp_record = shift;
  596. my $auth_record = shift;
  597. my $ad_zone = get_option($hdb,33);
  598. my $ad_dns = get_option($hdb,3);
  599. $update_hostname_from_dhcp = get_option($hdb,46) || 0;
  600. my $subnets_dhcp = get_subnets_ref($hdb);
  601. my $enable_ad_dns_update = ($ad_zone and $ad_dns and $update_hostname_from_dhcp);
  602. log_debug("Dhcp record: ".Dumper($dhcp_record));
  603. log_debug("Subnets: ".Dumper($subnets_dhcp->{$dhcp_record->{network}->{subnet}}));
  604. log_debug("enable_ad_dns_update: ".$enable_ad_dns_update);
  605. log_debug("DNS update flags - zone: ".$ad_zone.",dns: ".$ad_dns.", update_hostname_from_dhcp: ".$update_hostname_from_dhcp.", enable_ad_dns_update: ".$enable_ad_dns_update);
  606. my $maybe_update_dns=(($dhcp_record->{type}=~/add/i or $dhcp_record->{type}=~/old/i) and $dhcp_record->{hostname_utf8} and $dhcp_record->{hostname_utf8} !~/UNDEFINED/i and $enable_ad_dns_update and $subnets_dhcp->{$dhcp_record->{network}->{subnet}}->{dhcp_update_hostname});
  607. if (!$maybe_update_dns) {
  608. db_log_debug($hdb,"FOUND Auth_id: $auth_record->{id}. DNS update don't needed.");
  609. return 0;
  610. }
  611. log_debug("DNS update enabled.");
  612. #update dns block
  613. my $fqdn_static;
  614. if ($auth_record->{dns_name}) {
  615. $fqdn_static=lc($auth_record->{dns_name});
  616. if ($fqdn_static!~/\.$ad_zone$/i) {
  617. $fqdn_static=~s/\.$//;
  618. $fqdn_static=lc($fqdn_static.'.'.$ad_zone);
  619. }
  620. }
  621. my $fqdn=lc(trim($dhcp_record->{hostname_utf8}));
  622. if ($fqdn!~/\.$ad_zone$/i) {
  623. $fqdn=~s/\.$//;
  624. $fqdn=lc($fqdn.'.'.$ad_zone);
  625. }
  626. db_log_debug($hdb,"FOUND Auth_id: $auth_record->{id} dns_name: $fqdn_static dhcp_hostname: $fqdn");
  627. #check exists static dns name
  628. my $static_exists = 0;
  629. my $dynamic_exists = 0;
  630. my $static_ok = 0;
  631. my $dynamic_ok = 0;
  632. my $static_ref;
  633. my $dynamic_ref;
  634. if ($fqdn_static ne '') {
  635. my @dns_record=ResolveNames($fqdn_static,$dns_server);
  636. $static_exists = (scalar @dns_record>0);
  637. if ($static_exists) {
  638. $static_ref = join(' ',@dns_record);
  639. foreach my $dns_a (@dns_record) {
  640. if ($dns_a=~/^$dhcp_record->{ip}$/) { $static_ok = $dns_a; }
  641. }
  642. }
  643. } else { $static_ok = 1; }
  644. if ($fqdn ne '') {
  645. my @dns_record=ResolveNames($fqdn,$dns_server);
  646. $dynamic_exists = (scalar @dns_record>0);
  647. if ($dynamic_exists) {
  648. $dynamic_ref = join(' ',@dns_record);
  649. foreach my $dns_a (@dns_record) {
  650. if ($dns_a=~/^$dhcp_record->{ip}$/) { $dynamic_ok = $dns_a; }
  651. }
  652. }
  653. }
  654. db_log_debug($hdb,"Dns record for static record $fqdn_static: $static_ok");
  655. db_log_debug($hdb,"Dns record for dhcp-hostname $fqdn: $dynamic_ok");
  656. if ($fqdn_static ne '') {
  657. if (!$static_ok) {
  658. db_log_info($hdb,"Static record mismatch! Expected $fqdn_static => $dhcp_record->{ip}, recivied: $static_ref");
  659. if (!$static_exists) {
  660. db_log_info($hdb,"Static dns hostname defined but not found. Create it ($fqdn_static => $dhcp_record->{ip})!");
  661. update_dns_hostname($fqdn_static,$dhcp_record->{ip},$ad_zone,$ad_dns,$hdb);
  662. }
  663. } else {
  664. db_log_debug($hdb,"Static record for $fqdn_static [$static_ok] correct.");
  665. }
  666. }
  667. if ($fqdn ne '' and $dynamic_ok ne '') { db_log_debug($hdb,"Dynamic record for $fqdn [$dynamic_ok] correct. No changes required."); }
  668. if ($fqdn ne '' and !$dynamic_ok) {
  669. #log event without email alert
  670. log_error("Dynamic record mismatch! Expected: $fqdn => $dhcp_record->{ip}, recivied: $dynamic_ref. Checking the status.");
  671. #check exists hostname
  672. my $another_hostname_exists = 0;
  673. my $hostname_filter = ' LOWER(dns_name) REGEXP("^'.lc($dhcp_record->{hostname_utf8}).'\.*$")';
  674. if ($fqdn_static ne '' and $fqdn !~/$fqdn_static/) {
  675. $hostname_filter = $hostname_filter . ' or LOWER(dns_name) REGEXP("^'.lc($auth_record->{dns_name}).'\.*$")';
  676. }
  677. #check exists another records with some static hostname
  678. my $filter_sql = 'SELECT * FROM User_auth WHERE id<>'.$auth_record->{id}.' and deleted=0 and ('.$hostname_filter.') ORDER BY last_found DESC';
  679. db_log_debug($hdb,"Search dhcp hostname by: ".$filter_sql);
  680. my $name_record = get_record_sql($hdb,$filter_sql);
  681. if ($name_record->{dns_name} =~/^$fqdn$/i or $name_record->{dns_name} =~/^$dhcp_record->{hostname_utf8}$/i) {
  682. $another_hostname_exists = 1;
  683. }
  684. if (!$another_hostname_exists) {
  685. if ($fqdn_static and $fqdn_static ne '') {
  686. if ($fqdn_static!~/$fqdn/) {
  687. db_log_info($hdb,"Hostname from dhcp request $fqdn differs from static dns hostanme $fqdn_static. Ignore dynamic binding!");
  688. # update_dns_hostname($fqdn,$dhcp_record->{ip},$ad_zone,$ad_dns,$hdb);
  689. }
  690. } else {
  691. db_log_info($hdb,"Static dns hostname not defined. Create dns record by dhcp request. $fqdn => $dhcp_record->{ip}");
  692. update_dns_hostname($fqdn,$dhcp_record->{ip},$ad_zone,$ad_dns,$hdb);
  693. }
  694. } else {
  695. db_log_error($hdb,"Found another record with some hostname id: $name_record->{id} ip: $name_record->{ip} hostname: $name_record->{dns_name}. Skip update.");
  696. }
  697. }
  698. #end update dns block
  699. }
  700. #------------------------------------------------------------------------------------------------------------
  701. sub apply_device_lock {
  702. my $db = shift;
  703. my $device_id = shift;
  704. my $iteration = shift || 0;
  705. $iteration++;
  706. if ($iteration>2) { return 0; }
  707. my $dev = get_record_sql($db,"SELECT `discovery_locked`, `locked_timestamp`, UNIX_TIMESTAMP(`locked_timestamp`) as u_locked_timestamp FROM devices WHERE id=".$device_id);
  708. if (!$dev) { return 0; }
  709. if (!$dev->{'discovery_locked'}) { return set_lock_discovery($db,$device_id); }
  710. #if timestamp undefined, set and return
  711. if (!$dev->{'locked_timestamp'}) { return set_lock_discovery($db,$device_id); }
  712. #wait for discovery
  713. my $wait_time = $dev->{'locked_timestamp'} + 30 - time();
  714. if ($wait_time<0) { return set_lock_discovery($db,$device_id); }
  715. sleep($wait_time);
  716. return apply_device_lock($db,$device_id,$iteration);
  717. }
  718. #------------------------------------------------------------------------------------------------------------
  719. sub set_lock_discovery {
  720. my $db = shift;
  721. my $device_id = shift;
  722. my $new;
  723. $new->{'discovery_locked'} = 1;
  724. $new->{'locked_timestamp'} = GetNowTime();
  725. if (update_record($db,'devices',$new,'id='.$device_id)) { return 1; }
  726. return 0;
  727. }
  728. #------------------------------------------------------------------------------------------------------------
  729. sub unset_lock_discovery {
  730. my $db = shift;
  731. my $device_id = shift;
  732. my $new;
  733. $new->{'discovery_locked'} = 0;
  734. $new->{'locked_timestamp'} = GetNowTime();
  735. if (update_record($db,'devices',$new,'id='.$device_id)) { return 1; }
  736. return 0;
  737. }
  738. #------------------------------------------------------------------------------------------------------------
  739. sub update_dns_hostname {
  740. my $fqdn = shift;
  741. my $ip = shift;
  742. my $zone = shift;
  743. my $server = shift;
  744. my $db = shift;
  745. #skip update domain controllers
  746. if ($fqdn=~/^dc[0-9]{1,2}\./i) { return; }
  747. if (!$db) {
  748. log_info("DNS-UPDATE: Zone $zone Server: $server A: $fqdn IP: $ip");
  749. } else {
  750. db_log_info($db,"DNS-UPDATE: Zone $zone Server: $server A: $fqdn IP: $ip");
  751. }
  752. my $ad_zone = get_option($db,33);
  753. my $nsupdate_file = "/tmp/".$fqdn."-nsupdate";
  754. my @add_dns;
  755. if ($config_ref{dns_server_type}=~/windows/i) {
  756. push(@add_dns,"gsstsig");
  757. push(@add_dns,"server $server");
  758. push(@add_dns,"zone $zone");
  759. push(@add_dns,"update delete $fqdn A");
  760. push(@add_dns,"update add $fqdn 3600 A $ip");
  761. push(@add_dns,"send");
  762. write_to_file($nsupdate_file,\@add_dns);
  763. do_exec('/usr/bin/kinit -k -t /opt/Eye/scripts/cfg/dns_updater.keytab dns_updater@'.uc($ad_zone).' && /usr/bin/nsupdate "'.$nsupdate_file.'"');
  764. }
  765. if ($config_ref{dns_server_type}=~/bind/i) {
  766. push(@add_dns,"server $server");
  767. push(@add_dns,"zone $zone");
  768. push(@add_dns,"update delete $fqdn A");
  769. push(@add_dns,"update add $fqdn 3600 A $ip");
  770. push(@add_dns,"send");
  771. write_to_file($nsupdate_file,\@add_dns);
  772. do_exec('/usr/bin/nsupdate -k /etc/bind/rndc.key "'.$nsupdate_file.'"');
  773. }
  774. if (-e "$nsupdate_file") { unlink "$nsupdate_file"; }
  775. }
  776. #---------------------------------------------------------------------------------------------------------------
  777. sub update_dns_ptr {
  778. my $fqdn = shift;
  779. my $ip = shift;
  780. my $server = shift;
  781. my $db = shift;
  782. my $radr;
  783. my $zone;
  784. #skip update domain controllers
  785. if ($fqdn=~/^dc[0-9]{1,2}\./i) { return; }
  786. if ($ip =~ /([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})(\/[0-9]{1,2}){0,1}/) {
  787. return 0 if($1 > 255 || $2 > 255 || $3 > 255 || $4 > 255);
  788. $radr = "$4.$3.$2.$1.in-addr.arpa";
  789. $zone = "$3.$2.$1.in-addr.arpa";
  790. }
  791. if (!$radr or !$zone) { return 0; }
  792. if (!$db) {
  793. log_info("DNS-UPDATE: Zone $zone Server: $server A: $fqdn PTR: $ip");
  794. } else {
  795. db_log_info($db,"DNS-UPDATE: Zone $zone Server: $server A: $fqdn PTR: $ip");
  796. }
  797. my $ad_zone = get_option($db,33);
  798. my $nsupdate_file = "/tmp/".$radr."-nsupdate";
  799. my @add_dns;
  800. if ($config_ref{dns_server_type}=~/windows/i) {
  801. push(@add_dns,"gsstsig");
  802. push(@add_dns,"server $server");
  803. push(@add_dns,"zone $zone");
  804. push(@add_dns,"update delete $radr PTR");
  805. push(@add_dns,"update add $radr 3600 PTR $fqdn.");
  806. push(@add_dns,"send");
  807. write_to_file($nsupdate_file,\@add_dns);
  808. my $run_cmd = '/usr/bin/kinit -k -t /opt/Eye/scripts/cfg/dns_updater.keytab dns_updater@'.uc($ad_zone).' && /usr/bin/nsupdate "'.$nsupdate_file.'"';
  809. do_exec($run_cmd);
  810. }
  811. if ($config_ref{dns_server_type}=~/bind/i) {
  812. push(@add_dns,"server $server");
  813. push(@add_dns,"zone $zone");
  814. push(@add_dns,"update delete $radr PTR");
  815. push(@add_dns,"update add $radr 3600 PTR $fqdn.");
  816. push(@add_dns,"send");
  817. write_to_file($nsupdate_file,\@add_dns);
  818. my $run_cmd = '/usr/bin/nsupdate -k /etc/bind/rndc.key "'.$nsupdate_file.'"';
  819. do_exec($run_cmd);
  820. }
  821. if (-e "$nsupdate_file") { unlink "$nsupdate_file"; }
  822. }
  823. #---------------------------------------------------------------------------------------------------------------
  824. sub new_user {
  825. my $db = shift;
  826. my $user_info = shift;
  827. my $user;
  828. if ($user_info->{mac}) {
  829. $user->{login}=mac_splitted($user_info->{mac});
  830. } else {
  831. $user->{login}=$user_info->{ip};
  832. }
  833. if ($user_info->{dhcp_hostname}) {
  834. $user->{fio}=$user_info->{ip}. '['.$user_info->{dhcp_hostname} .']';
  835. } else {
  836. $user->{fio}=$user_info->{ip};
  837. }
  838. my $login_count = get_count_records($db,"User_list","(login LIKE '".$user->{login}."(%)') OR (login='".$user->{login}."')");
  839. if ($login_count) { $login_count++; $user->{login} .="(".$login_count.")"; }
  840. $user->{ou_id} = $user_info->{ou_id};
  841. my $ou_info = get_record_sql($db,"SELECT * FROM OU WHERE id=".$user_info->{'ou_id'});
  842. if ($ou_info) {
  843. $user->{'enabled'} = $ou_info->{'enabled'};
  844. $user->{'queue_id'} = $ou_info->{'queue_id'};
  845. $user->{'filter_group_id'} = $ou_info->{'filter_group_id'};
  846. }
  847. my $result = insert_record($db,"User_list",$user);
  848. if ($result and $config_ref{auto_mac_rule} and $user_info->{mac}) {
  849. my $auth_rule;
  850. $auth_rule->{user_id} = $result;
  851. $auth_rule->{type} = 2;
  852. $auth_rule->{rule} = mac_splitted($user_info->{mac});
  853. insert_record($db,"auth_rules",$auth_rule);
  854. }
  855. return $result;
  856. }
  857. #---------------------------------------------------------------------------------------------------------------
  858. sub get_ip_subnet {
  859. my $db = shift;
  860. my $ip = shift;
  861. if (!$ip) { return; }
  862. my $ip_aton = StrToIp($ip);
  863. my $user_subnet = get_record_sql($db, "SELECT * FROM `subnets` WHERE hotspot=1 or office=1 and ( ".$ip_aton." >= ip_int_start and ".$ip_aton." <= ip_int_stop)");
  864. return $user_subnet;
  865. }
  866. #---------------------------------------------------------------------------------------------------------------
  867. sub find_mac_in_subnet {
  868. my $db = shift;
  869. my $ip = shift;
  870. my $mac = shift;
  871. if (!$ip or !$mac) { return; }
  872. my $ip_subnet = get_ip_subnet($db, $ip);
  873. if (!$ip_subnet) { return; }
  874. my @t_auth = get_records_sql($db, "SELECT id,mac,user_id FROM User_auth WHERE ip_int>=" . $ip_subnet->{'ip_int_start'} . " and ip_int<=" . $ip_subnet->{'ip_int_stop'} . " and mac='" . $mac . "' and deleted=0 ORDER BY id");
  875. my $auth_count = 0;
  876. my $result;
  877. $result->{'count'} = 0;
  878. foreach my $row (@t_auth) {
  879. next if (!$row);
  880. $auth_count++;
  881. $result->{'count'} = $auth_count;
  882. $result->{$auth_count} = $row->{'id'};
  883. push(@{$result->{'users_id'}}, $row->{'user_id'});
  884. }
  885. return $result;
  886. }
  887. #---------------------------------------------------------------------------------------------------------------
  888. sub resurrection_auth {
  889. my $db = shift;
  890. my $ip_record = shift;
  891. my $ip = $ip_record->{'ip'};
  892. my $mac = $ip_record->{'mac'};
  893. my $action = $ip_record->{'type'};
  894. my $hostname = $ip_record->{'hostname_utf8'};
  895. my $client_id = $ip_record->{'client-id'};
  896. if (!exists $ip_record->{ip_aton}) { $ip_record->{ip_aton}=StrToIp($ip); }
  897. if (!exists $ip_record->{hotspot}) { $ip_record->{hotspot}=is_hotspot($db,$ip); }
  898. my $ip_aton=$ip_record->{ip_aton};
  899. my $timestamp=GetNowTime();
  900. my $record=get_record_sql($db,'SELECT * FROM User_auth WHERE `deleted`=0 AND `ip_int`='.$ip_aton.' AND `mac`="'.$mac.'"');
  901. my $new_record;
  902. $new_record->{last_found}=$timestamp;
  903. if ($client_id) { $new_record->{'client-id'} = $client_id; }
  904. #auth found?
  905. if ($record->{user_id}) {
  906. #update timestamp and return
  907. if ($action!~/arp/i) {
  908. $new_record->{dhcp_action}=$action;
  909. $new_record->{dhcp_time}=$timestamp;
  910. if ($hostname) { $new_record->{dhcp_hostname} = $hostname; }
  911. update_record($db,'User_auth',$new_record,"id=$record->{id}");
  912. } else {
  913. update_record($db,'User_auth',$new_record,"id=$record->{id}");
  914. }
  915. return $record->{id};
  916. }
  917. my $user_subnet=$office_networks->match_string($ip);
  918. if ($user_subnet->{static}) {
  919. db_log_warning($db,"Unknown ip+mac found in static subnet! Abort create record for ip: $ip mac: [".$mac."]");
  920. return 0;
  921. }
  922. #search changed mac
  923. $record=get_record_sql($db,'SELECT * FROM User_auth WHERE `ip_int`='.$ip_aton." and deleted=0");
  924. if ($record->{id}) {
  925. #if found record with same ip but another mac
  926. if (!$record->{mac}) {
  927. db_log_verbose($db,"use empty auth record...");
  928. $new_record->{mac}=$mac;
  929. if ($action!~/arp/i) {
  930. $new_record->{dhcp_action}=$action;
  931. $new_record->{dhcp_time}=$timestamp;
  932. if ($hostname) { $new_record->{dhcp_hostname} = $hostname; }
  933. update_record($db,'User_auth',$new_record,"id=$record->{id}");
  934. } else {
  935. update_record($db,'User_auth',$new_record,"id=$record->{id}");
  936. }
  937. return $record->{id};
  938. }
  939. if ($record->{mac}) {
  940. db_log_warning($db,"For ip: $ip mac change detected! Old mac: [".$record->{mac}."] New mac: [".$mac."]. Disable old auth_id: $record->{id}") if (!$ip_record->{hotspot});
  941. my $disable_record;
  942. $disable_record->{deleted}="1";
  943. update_record($db,'User_auth',$disable_record,"id=".$record->{id});
  944. }
  945. }
  946. #default user
  947. my $new_user_info=get_new_user_id($db,$ip,$mac,$hostname);
  948. my $new_user_id;
  949. if ($new_user_info->{user_id}) { $new_user_id = $new_user_info->{user_id}; }
  950. if (!$new_user_id) { $new_user_id = new_user($db,$new_user_info); }
  951. my $mac_exists=find_mac_in_subnet($db,$ip,$mac);
  952. #disable dhcp for same mac in one ip subnet
  953. if ($mac_exists and $mac_exists->{'count'}) { $new_record->{dhcp}=0; }
  954. #seek old auth with same ip and mac
  955. my $auth_exists=get_count_records($db,'User_auth',"ip_int=".$ip_aton." and mac='".$mac."'");
  956. $new_record->{ip_int}=$ip_aton;
  957. $new_record->{ip}=$ip;
  958. $new_record->{mac}=$mac;
  959. $new_record->{user_id}=$new_user_id;
  960. $new_record->{save_traf}="$save_detail";
  961. $new_record->{deleted}="0";
  962. $new_record->{dhcp_action}=$action;
  963. $new_record->{dhcp_time}=$timestamp;
  964. if ($auth_exists) {
  965. #found ->Resurrection old record
  966. my $resurrection_id = get_id_record($db,'User_auth',"ip_int=".$ip_aton." and mac='".$mac."'");
  967. if (!$ip_record->{hotspot}) { db_log_warning($db,"Resurrection auth_id: $resurrection_id with ip: $ip and mac: $mac"); }
  968. else { db_log_info($db,"Resurrection auth_id: $resurrection_id with ip: $ip and mac: $mac"); }
  969. update_record($db,'User_auth',$new_record,"id=$resurrection_id");
  970. } else {
  971. #not found ->create new record
  972. if (!$ip_record->{hotspot}) { db_log_warning($db,"New ip created! ip: $ip mac: $mac"); } else { db_log_info($db,"New ip created! ip: $ip mac: $mac"); }
  973. insert_record($db,'User_auth',$new_record);
  974. }
  975. #filter and status
  976. my $cur_auth_id=get_id_record($db,'User_auth',"ip='$ip' and mac='$mac' and deleted=0 ORDER BY last_found DESC");
  977. if ($cur_auth_id) {
  978. my $user_record=get_record_sql($db,"SELECT * FROM User_list WHERE id=".$new_user_id);
  979. if ($user_record) {
  980. $new_record->{ou_id}=$user_record->{ou_id};
  981. $new_record->{comments}=$user_record->{fio};
  982. $new_record->{filter_group_id}=$user_record->{filter_group_id};
  983. $new_record->{queue_id}=$user_record->{queue_id};
  984. $new_record->{enabled}="$user_record->{enabled}";
  985. update_record($db,'User_auth',$new_record,"id=$cur_auth_id");
  986. }
  987. } else { return; }
  988. return $cur_auth_id;
  989. }
  990. #---------------------------------------------------------------------------------------------------------------
  991. sub new_auth {
  992. my $db = shift;
  993. my $ip = shift;
  994. my $ip_aton=StrToIp($ip);
  995. my $record=get_record_sql($db,'SELECT id FROM User_auth WHERE `deleted`=0 AND `ip_int`='.$ip_aton);
  996. if ($record->{id}) { return $record->{id}; }
  997. #default user
  998. my $new_user_info=get_new_user_id($db,$ip,undef,undef);
  999. my $new_user_id;
  1000. if ($new_user_info->{user_id}) { $new_user_id = $new_user_info->{user_id}; }
  1001. if ($new_user_info->{ou_id}) { $new_user_id = new_user($db,$new_user_info); }
  1002. my $user_record=get_record_sql($db,"SELECT * FROM User_list WHERE id=".$new_user_id);
  1003. my $timestamp=GetNowTime();
  1004. my $new_record;
  1005. $new_record->{ip_int}=$ip_aton;
  1006. $new_record->{ip}=$ip;
  1007. $new_record->{user_id}=$new_user_id;
  1008. $new_record->{save_traf}="$save_detail";
  1009. $new_record->{deleted}="0";
  1010. $new_record->{dhcp_action}='netflow';
  1011. $new_record->{ou_id}=$user_record->{ou_id};
  1012. $new_record->{filter_group_id}=$user_record->{filter_group_id};
  1013. $new_record->{queue_id}=$user_record->{queue_id};
  1014. $new_record->{enabled}="$user_record->{enabled}";
  1015. $new_record->{comments}=$user_record->{fio};
  1016. my $cur_auth_id=insert_record($db,'User_auth',$new_record);
  1017. db_log_warning($db,"New ip created by netflow! ip: $ip") if ($cur_auth_id);
  1018. return $cur_auth_id;
  1019. }
  1020. #---------------------------------------------------------------------------------------------------------------
  1021. sub get_option {
  1022. my $db=shift;
  1023. my $option_id=shift;
  1024. return if (!$option_id);
  1025. return if (!$db);
  1026. my $default_option = get_record_sql($db,'SELECT * FROM config_options WHERE id='.$option_id);
  1027. my $config_options = get_record_sql($db,'SELECT * FROM config WHERE option_id='.$option_id);
  1028. my $result;
  1029. if (!$config_options) {
  1030. if ($default_option->{'type'}=~/^(int|bool)/i) { $result = $default_option->{'default_value'}*1; };
  1031. if ($default_option->{'type'}=~/^(string|text)/i) { $result = $default_option->{'default_value'}; }
  1032. if ($default_option->{'type'}=~/^list/i) { $result = $default_option->{'default_value'}; }
  1033. return $result;
  1034. }
  1035. $result = $config_options->{'value'};
  1036. return $result;
  1037. }
  1038. #---------------------------------------------------------------------------------------------------------------
  1039. sub init_option {
  1040. my $db=shift;
  1041. $last_refresh_config = time();
  1042. $config_ref{dbh}=$db;
  1043. $config_ref{save_detail}=get_option($db,23);
  1044. $config_ref{add_unknown_user}=get_option($db,22);
  1045. $config_ref{dhcp_server}=get_option($db,5);
  1046. $config_ref{snmp_default_version}=get_option($db,9);
  1047. $config_ref{snmp_default_community}=get_option($db,11);
  1048. $config_ref{KB}=get_option($db,1);
  1049. if ($config_ref{KB} ==0) { $config_ref{KB}=1000; }
  1050. if ($config_ref{KB} ==1) { $config_ref{KB}=1024; }
  1051. $config_ref{admin_email}=get_option($db,21);
  1052. $config_ref{sender_email}=get_option($db,52);
  1053. $config_ref{send_email}=get_option($db,51);
  1054. $config_ref{history}=get_option($db,26);
  1055. $config_ref{history_dhcp}=get_option($db,27);
  1056. $config_ref{router_login}=get_option($db,28);
  1057. $config_ref{router_password}=get_option($db,29);
  1058. $config_ref{router_port}=get_option($db,30);
  1059. $config_ref{org_name}=get_option($db,32);
  1060. $config_ref{domain_name}=get_option($db,33);
  1061. $config_ref{connections_history}=get_option($db,35);
  1062. $config_ref{debug}=get_option($db,34);
  1063. $config_ref{log_level} = get_option($db,53);
  1064. if ($config_ref{debug}) { $config_ref{log_level} = 255; }
  1065. $config_ref{urgent_sync}=get_option($db,50);
  1066. $config_ref{ignore_hotspot_dhcp_log} = get_option($db,44);
  1067. $config_ref{ignore_update_dhcp_event} = get_option($db,45);
  1068. $config_ref{update_hostname_from_dhcp} = get_option($db,46);
  1069. $config_ref{history_log_day}=get_option($db,47);
  1070. $config_ref{history_syslog_day} = get_option($db,48);
  1071. $config_ref{history_trafstat_day} = get_option($db,49);
  1072. $config_ref{enable_quotes} = get_option($db,54);
  1073. $config_ref{netflow_step} = get_option($db,55);
  1074. $config_ref{traffic_ipstat_history} = get_option($db,56);
  1075. $config_ref{nagios_url} = get_option($db,57);
  1076. $config_ref{cacti_url} = get_option($db,58);
  1077. $config_ref{torrus_url} = get_option($db,59);
  1078. $config_ref{wiki_url} = get_option($db,60);
  1079. $config_ref{stat_url} = get_option($db,62);
  1080. $config_ref{wiki_path} = get_option($db,61);
  1081. $config_ref{auto_mac_rule} = get_option($db,64);
  1082. #network configuration mode
  1083. $config_ref{config_mode}=get_option($db,68);
  1084. #auto clean old user record
  1085. $config_ref{clean_empty_user}=get_option($db,69);
  1086. #dns_server_type
  1087. $config_ref{dns_server}=get_option($db,3);
  1088. $config_ref{dns_server_type}=get_option($db,70);
  1089. $config_ref{enable_dns_updates}=get_option($db,71);
  1090. #$save_detail = 1; id=23
  1091. $save_detail=get_option($db,23);
  1092. #$add_unknown_user = 1; id=22
  1093. $add_unknown_user=get_option($db,22);
  1094. #$dns_server='192.168.2.12'; id=3
  1095. $dns_server=get_option($db,3);
  1096. #$dhcp_server='192.168.2.12'; id=5
  1097. $dhcp_server=get_option($db,5);
  1098. #$snmp_default_version='2'; id=9
  1099. $snmp_default_version=get_option($db,9);
  1100. #$snmp_default_community='public'; id=11
  1101. $snmp_default_community=get_option($db,11);
  1102. #$KB=1024; id=1
  1103. $KB=$config_ref{KB};
  1104. #$admin_email; id=21
  1105. $admin_email=get_option($db,21);
  1106. #sender email
  1107. $sender_email=get_option($db,52);
  1108. #send email
  1109. $send_email=get_option($db,51);
  1110. #$history=15; id=26
  1111. $history=get_option($db,26);
  1112. #$history_dhcp=7; id=27
  1113. $history_dhcp=get_option($db,27);
  1114. #$router_login="admin"; id=28
  1115. $router_login=get_option($db,28);
  1116. #$router_password="admin"; id=29
  1117. $router_password=get_option($db,29);
  1118. #$router_port=23; id=30
  1119. $router_port=get_option($db,30);
  1120. #32
  1121. $org_name=get_option($db,32);
  1122. #33
  1123. $domain_name=get_option($db,33);
  1124. #35
  1125. $connections_history=get_option($db,35);
  1126. #debug
  1127. $debug=get_option($db,34);
  1128. #log level
  1129. $log_level = get_option($db,53);
  1130. if ($debug) { $log_level = 255; }
  1131. #urgent sync access
  1132. $urgent_sync=get_option($db,50);
  1133. $ignore_hotspot_dhcp_log = get_option($db,44);
  1134. $ignore_update_dhcp_event = get_option($db,45);
  1135. $update_hostname_from_dhcp = get_option($db,46);
  1136. $history_log_day=get_option($db,47);
  1137. $history_syslog_day = get_option($db,48);
  1138. $history_trafstat_day = get_option($db,49);
  1139. my $ou = get_record_sql($db,"SELECT id FROM OU WHERE default_users = 1");
  1140. if (!$ou) { $default_user_ou_id = 0; } else { $default_user_ou_id = $ou->{'id'}; }
  1141. $ou = get_record_sql($db,"SELECT id FROM OU WHERE default_hotspot = 1");
  1142. if (!$ou) { $default_hotspot_ou_id = $default_user_ou_id; } else { $default_hotspot_ou_id = $ou->{'id'}; }
  1143. @subnets=get_records_sql($db,'SELECT * FROM subnets ORDER BY ip_int_start');
  1144. if (defined $office_networks) { undef $office_networks; }
  1145. if (defined $free_networks) { undef $free_networks; }
  1146. if (defined $vpn_networks) { undef $vpn_networks; }
  1147. if (defined $hotspot_networks) { undef $hotspot_networks; }
  1148. if (defined $all_networks) { undef $all_networks; }
  1149. $office_networks = new Net::Patricia;
  1150. $free_networks = new Net::Patricia;
  1151. $vpn_networks = new Net::Patricia;
  1152. $hotspot_networks = new Net::Patricia;
  1153. $all_networks = new Net::Patricia;
  1154. @office_network_list=();
  1155. @free_network_list=();
  1156. @free_network_list=();
  1157. @vpn_network_list=();
  1158. @hotspot_network_list=();
  1159. @all_network_list=();
  1160. foreach my $net (@subnets) {
  1161. next if (!$net->{subnet});
  1162. $subnets_ref{$net->{subnet}}=$net;
  1163. if ($net->{office}) {
  1164. push(@office_network_list,$net->{subnet});
  1165. $office_networks->add_string($net->{subnet},$net);
  1166. }
  1167. if ($net->{free}) {
  1168. push(@free_network_list,$net->{subnet});
  1169. $free_networks->add_string($net->{subnet},$net);
  1170. }
  1171. if ($net->{vpn}) {
  1172. push(@vpn_network_list,$net->{subnet});
  1173. $vpn_networks->add_string($net->{subnet},$net);
  1174. }
  1175. if ($net->{hotspot}) {
  1176. push(@hotspot_network_list,$net->{subnet});
  1177. push(@all_network_list,$net->{subnet});
  1178. $hotspot_networks->add_string($net->{subnet},$net);
  1179. }
  1180. push(@all_network_list,$net->{subnet});
  1181. $all_networks->add_string($net->{subnet},$net);
  1182. }
  1183. }
  1184. #---------------------------------------------------------------------------------------------------------------
  1185. sub get_subnets_ref {
  1186. my $db = shift;
  1187. my @list=get_records_sql($db,'SELECT * FROM subnets ORDER BY ip_int_start');
  1188. my $list_ref;
  1189. foreach my $net (@list) {
  1190. next if (!$net->{subnet});
  1191. $list_ref->{$net->{subnet}}=$net;
  1192. }
  1193. return $list_ref;
  1194. }
  1195. #---------------------------------------------------------------------------------------------------------------
  1196. sub get_device_by_ip {
  1197. my $db = shift;
  1198. my $ip = shift;
  1199. my $netdev=get_record_sql($db,'SELECT * FROM devices WHERE ip="'.$ip.'"');
  1200. if ($netdev and $netdev->{id}>0) { return $netdev; }
  1201. my $auth_rec=get_record_sql($db,'SELECT user_id FROM User_auth WHERE ip="'.$ip.'" and deleted=0');
  1202. if ($auth_rec and $auth_rec->{user_id}>0) {
  1203. $netdev=get_record_sql($db,'SELECT * FROM devices WHERE user_id='.$auth_rec->{user_id});
  1204. return $netdev;
  1205. }
  1206. return;
  1207. }
  1208. #---------------------------------------------------------------------------------------------------------------
  1209. sub GetUnixTimeByStr {
  1210. my $time_str = shift;
  1211. $time_str =~s/\//-/g;
  1212. $time_str = trim($time_str);
  1213. my ($sec,$min,$hour,$day,$mon,$year) = (localtime())[0,1,2,3,4,5];
  1214. $year+=1900;
  1215. $mon++;
  1216. if ($time_str =~/^([0-9]{2,4})\-([0-9]{1,2})-([0-9]{1,2})\s+/) {
  1217. $year = $1; $mon = $2; $day = $3;
  1218. }
  1219. if ($time_str =~/([0-9]{1,2})\:([0-9]{1,2})\:([0-9]{1,2})$/) {
  1220. $hour = $1; $min = $2; $sec = $3;
  1221. }
  1222. my $result = mktime($sec,$min,$hour,$day,$mon-1,$year-1900);
  1223. return $result;
  1224. }
  1225. #---------------------------------------------------------------------------------------------------------------
  1226. sub GetTimeStrByUnixTime {
  1227. my $time = shift || time();
  1228. my ($sec, $min, $hour, $mday, $mon, $year) = (localtime($time))[0,1,2,3,4,5];
  1229. my $result = strftime("%Y-%m-%d %H:%M:%S",$sec, $min, $hour, $mday, $mon, $year);
  1230. return $result;
  1231. }
  1232. #---------------------------------------------------------------------------------------------------------------
  1233. sub Set_Variable {
  1234. my $db = shift;
  1235. my $name = shift || $MY_NAME;
  1236. my $value = shift || $$;
  1237. my $timeshift = shift || 60;
  1238. Del_Variable($db,$name);
  1239. my $clean_variables = time() + $timeshift;
  1240. my ($sec,$min,$hour,$day,$month,$year,$zone) = localtime($clean_variables);
  1241. $month++;
  1242. $year += 1900;
  1243. my $clean_str=sprintf "%04d-%02d-%02d %02d:%02d:%02d",$year,$month,$day,$hour,$min,$sec;
  1244. my $clean_variables_date=$db->quote($clean_str);
  1245. do_sql($db,"INSERT INTO variables(name,value,clear_time) VALUES('".$name."','".$value."',".$clean_variables_date.");");
  1246. }
  1247. #---------------------------------------------------------------------------------------------------------------
  1248. sub Get_Variable {
  1249. my $db = shift;
  1250. my $name = shift || $MY_NAME;
  1251. my $variable=get_record_sql($db,'SELECT `value` FROM `variables` WHERE name="'.$name.'"');
  1252. if (!$variable and $variable->{'value'}) { return $variable->{'value'}; }
  1253. return;
  1254. }
  1255. #---------------------------------------------------------------------------------------------------------------
  1256. sub Del_Variable {
  1257. my $db = shift;
  1258. my $name = shift || $MY_NAME;
  1259. do_sql($db,"DELETE FROM `variables` WHERE name='".$name."';");
  1260. }
  1261. #---------------------------------------------------------------------------------------------------------------
  1262. sub recalc_quotes {
  1263. my $db = shift;
  1264. my $calc_id = shift || $$;
  1265. return if (!get_option($db,54));
  1266. clean_variables($db);
  1267. return if (Get_Variable($db,'RECALC'));
  1268. my $timeshift = get_option($db,55);
  1269. if ($timeshift >5 ) { $timeshift=$timeshift-1; }
  1270. Set_Variable($db,'RECALC',$calc_id,time()+$timeshift*60);
  1271. my $now = DateTime->now(time_zone=>'local');
  1272. my $day_start = $db->quote($now->ymd("-")." 00:00:00");
  1273. my $day_dur = DateTime::Duration->new( days => 1 );
  1274. my $tomorrow = $now+$day_dur;
  1275. my $day_stop = $db->quote($tomorrow->ymd("-")." 00:00:00");
  1276. $now->set(day=>1);
  1277. my $month_start=$db->quote($now->ymd("-")." 00:00:00");
  1278. my $month_dur = DateTime::Duration->new( months => 1 );
  1279. my $next_month = $now + $month_dur;
  1280. $next_month->set(day=>1);
  1281. my $month_stop = $db->quote($next_month->ymd("-")." 00:00:00");
  1282. #get user limits
  1283. my $user_auth_list_sql="SELECT A.id as auth_id, U.id, U.day_quota, U.month_quota, A.day_quota as auth_day, A.month_quota as auth_month FROM User_auth as A,User_list as U WHERE A.deleted=0 ORDER by user_id";
  1284. my @authlist_ref = get_records_sql($db,$user_auth_list_sql);
  1285. my %user_stats;
  1286. my %auth_info;
  1287. foreach my $row (@authlist_ref) {
  1288. $auth_info{$row->{auth_id}}{user_id}=$row->{id};
  1289. $auth_info{$row->{auth_id}}{day_limit}=$row->{auth_day};
  1290. $auth_info{$row->{auth_id}}{month_limit}=$row->{auth_month};
  1291. $auth_info{$row->{auth_id}}{day}=0;
  1292. $auth_info{$row->{auth_id}}{month}=0;
  1293. $user_stats{$row->{id}}{day_limit}=$row->{day_quota};
  1294. $user_stats{$row->{id}}{month_limit}=$row->{month_quota};
  1295. $user_stats{$row->{id}}{day}=0;
  1296. $user_stats{$row->{id}}{month}=0;
  1297. }
  1298. #recalc quotes - global
  1299. #day
  1300. my $day_sql="SELECT User_stats.auth_id, SUM( byte_in + byte_out ) AS traf_all FROM User_stats
  1301. WHERE User_stats.`timestamp`>= $day_start AND User_stats.`timestamp`< $day_stop GROUP BY User_stats.auth_id";
  1302. my @day_stats = get_records_sql($db,$day_sql);
  1303. foreach my $row (@day_stats) {
  1304. my $user_id=$auth_info{$row->{auth_id}}{user_id};
  1305. $auth_info{$row->{auth_id}}{day}=$row->{traf_all};
  1306. $user_stats{$user_id}{day}+=$row->{traf_all};
  1307. }
  1308. #month
  1309. my $month_sql="SELECT User_stats.auth_id, SUM( byte_in + byte_out ) AS traf_all FROM User_stats
  1310. WHERE User_stats.`timestamp`>= $month_start AND User_stats.`timestamp`< $month_stop GROUP BY User_stats.auth_id";
  1311. my @month_stats = get_records_sql($db,$month_sql);
  1312. foreach my $row (@month_stats) {
  1313. my $user_id=$auth_info{$row->{auth_id}}{user_id};
  1314. $auth_info{$row->{auth_id}}{month}=$row->{traf_all};
  1315. $user_stats{$user_id}{month}+=$row->{traf_all};
  1316. }
  1317. foreach my $auth_id (keys %auth_info) {
  1318. next if (!$auth_info{$auth_id}{day_limit});
  1319. next if (!$auth_info{$auth_id}{month_limit});
  1320. my $day_limit=$auth_info{$auth_id}{day_limit}*$KB*$KB;
  1321. my $month_limit=$auth_info{$auth_id}{month_limit}*$KB*$KB;
  1322. my $blocked_d=($auth_info{$auth_id}{day}>$day_limit);
  1323. my $blocked_m=($auth_info{$auth_id}{month}>$month_limit);
  1324. if ($blocked_d or $blocked_m) {
  1325. my $history_msg;
  1326. if ($blocked_d) { $history_msg=printf "Day quota limit found for auth_id: $auth_id - Current: %d Max: %d",$auth_info{$auth_id}{day},$day_limit; }
  1327. if ($blocked_m) { $history_msg=printf "Month quota limit found for auth_id: $auth_id - Current: %d Max: %d",$auth_info{$auth_id}{month},$month_limit; }
  1328. do_sql($db,"UPDATE User_auth set blocked=1, changed=1 where id=$auth_id");
  1329. db_log_verbose($db,$history_msg);
  1330. }
  1331. }
  1332. foreach my $user_id (keys %user_stats) {
  1333. next if (!$user_stats{$user_id}{day_limit});
  1334. next if (!$user_stats{$user_id}{month_limit});
  1335. my $day_limit=$user_stats{$user_id}{day_limit}*$KB*$KB;
  1336. my $month_limit=$user_stats{$user_id}{month_limit}*$KB*$KB;
  1337. my $blocked_d=($user_stats{$user_id}{day}>$day_limit);
  1338. my $blocked_m=($user_stats{$user_id}{month}>$month_limit);
  1339. if ($blocked_d or $blocked_m) {
  1340. my $history_msg;
  1341. if ($blocked_d) { $history_msg=printf "Day quota limit found for user_id: $user_id - Current: %d Max: %d",$user_stats{$user_id}{day},$day_limit; }
  1342. if ($blocked_m) { $history_msg=printf "Month quota limit found for user_id: $user_id - Current: %d Max: %d",$user_stats{$user_id}{month},$month_limit; }
  1343. do_sql($db,"UPDATE User_user set blocked=1 where id=$user_id");
  1344. do_sql($db,"UPDATE User_auth set blocked=1, changed=1 where user_id=$user_id");
  1345. db_log_verbose($db,$history_msg);
  1346. }
  1347. }
  1348. Del_Variable($db,'RECALC');
  1349. }
  1350. #---------------------------------------------------------------------------------------------------------------
  1351. sub clean_variables {
  1352. my $db = shift;
  1353. #clean temporary variables
  1354. my $clean_variables = time();
  1355. my ($sec,$min,$hour,$day,$month,$year,$zone) = localtime($clean_variables);
  1356. $month++;
  1357. $year += 1900;
  1358. my $now_str=sprintf "%04d-%02d-%02d %02d:%02d:%02d",$year,$month,$day,$hour,$min,$sec;
  1359. my $clean_variables_date=$db->quote($now_str);
  1360. do_sql($db,"DELETE FROM `variables` WHERE clear_time<=$clean_variables_date");
  1361. }
  1362. #---------------------------------------------------------------------------------------------------------------
  1363. $dbh=init_db();
  1364. init_option($dbh);
  1365. clean_variables($dbh);
  1366. Set_Variable($dbh);
  1367. 1;
  1368. }