mysql.pm 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854
  1. package Rstat::mysql;
  2. #
  3. # Copyright (C) Roman Dmitiriev, rnd@rajven.ru
  4. #
  5. use strict;
  6. use English;
  7. use FindBin '$Bin';
  8. use lib "$Bin";
  9. use base 'Exporter';
  10. use vars qw(@EXPORT @ISA);
  11. use Rstat::config;
  12. use Rstat::main;
  13. use Net::Patricia;
  14. use Data::Dumper;
  15. use DBI;
  16. our @ISA = qw(Exporter);
  17. our @EXPORT = qw(
  18. batch_db_sql
  19. db_log_warning
  20. db_log_debug
  21. db_log_error
  22. db_log_info
  23. db_log_verbose
  24. delete_record
  25. do_sql
  26. get_count_records
  27. get_custom_record
  28. get_custom_records
  29. get_device_by_ip
  30. get_diff_rec
  31. get_id_record
  32. get_new_user_id
  33. GetNowTime
  34. get_option
  35. get_record
  36. get_records
  37. get_subnets_ref
  38. init_db
  39. init_option
  40. init_traf_db
  41. insert_record
  42. IpToStr
  43. refresh_add_rules
  44. resurrection_auth
  45. StrToIp
  46. update_record
  47. write_db_log
  48. set_changed
  49. $add_rules
  50. $L_WARNING
  51. $L_INFO
  52. $L_DEBUG
  53. $L_ERROR
  54. $L_VERBOSE
  55. );
  56. BEGIN
  57. {
  58. #---------------------------------------------------------------------------------------------------------------
  59. our $add_rules;
  60. our $L_ERROR = 0;
  61. our $L_WARNING = 1;
  62. our $L_INFO = 2;
  63. our $L_VERBOSE = 3;
  64. our $L_DEBUG = 255;
  65. our %acl_fields = (
  66. 'ip' => '1',
  67. 'ip_int' => '1',
  68. 'ip_int_end'=>'1',
  69. 'enabled'=>'1',
  70. 'dhcp'=>'1',
  71. 'filter_group_id'=>'1',
  72. 'deleted'=>'1',
  73. 'dhcp_acl'=>'1',
  74. 'queue_id'=>'1',
  75. 'mac'=>'1',
  76. 'blocked'=>'1'
  77. );
  78. #---------------------------------------------------------------------------------------------------------------
  79. sub StrToIp{
  80. return unpack('N',pack('C4',split(/\./,$_[0])));
  81. }
  82. #---------------------------------------------------------------------------------------------------------------
  83. sub IpToStr{
  84. my $nIP = shift;
  85. my $res = (($nIP>>24) & 255) .".". (($nIP>>16) & 255) .".". (($nIP>>8) & 255) .".". ($nIP & 255);
  86. return $res;
  87. }
  88. #---------------------------------------------------------------------------------------------------------------
  89. sub batch_db_sql {
  90. my $dbh=shift;
  91. my @batch_sql=shift;
  92. return if (!$dbh);
  93. return if (!@batch_sql or !scalar(@batch_sql));
  94. $dbh->{AutoCommit} = 0;
  95. my $sth;
  96. foreach my $sSQL(@batch_sql) {
  97. #log_debug($sSQL);
  98. $sth = $dbh->prepare($sSQL);
  99. $sth->execute;
  100. }
  101. $sth->finish;
  102. $dbh->{AutoCommit} = 1;
  103. }
  104. #---------------------------------------------------------------------------------------------------------------
  105. sub do_sql {
  106. my $dbh=shift;
  107. my $sql=shift;
  108. return if (!$dbh);
  109. return if (!$sql);
  110. if ($sql!~/^select /i) { db_log_debug($dbh,$sql); }
  111. my $sql_prep = $dbh->prepare($sql);
  112. my $sql_ref;
  113. if ( !defined $sql_prep ) { die "Cannot prepare statement: $DBI::errstr\n"; }
  114. $sql_prep->execute;
  115. if ($sql=~/^insert/i) { $sql_ref = $sql_prep->{mysql_insertid}; }
  116. if ($sql=~/^select /i) { $sql_ref = $sql_prep->fetchall_arrayref(); };
  117. $sql_prep->finish();
  118. return $sql_ref;
  119. }
  120. #---------------------------------------------------------------------------------------------------------------
  121. sub write_db_log {
  122. my $dbh=shift;
  123. my $msg=shift;
  124. my $level = shift || $L_VERBOSE;
  125. my $auth_id = shift || 0;
  126. return if (!$dbh);
  127. return if (!$msg);
  128. $msg=~s/[\'\"]//g;
  129. my $db_log = 0;
  130. my $history_sql="INSERT INTO syslog(customer,message,level,auth_id) VALUES(".$dbh->quote($MY_NAME).",".$dbh->quote($msg).",$level,$auth_id)";
  131. if ($level eq $L_ERROR and $log_level >= $L_ERROR) { log_error($msg); $db_log = 1; }
  132. if ($level eq $L_WARNING and $log_level >= $L_WARNING) { log_warning($msg); $db_log = 1; }
  133. if ($level eq $L_INFO and $log_level >= $L_INFO) { log_info($msg); $db_log = 1; }
  134. if ($level eq $L_DEBUG and $log_level >= $L_DEBUG) { log_debug($msg); $db_log = 1; }
  135. if ($db_log) {
  136. my $history_rf=$dbh->prepare($history_sql);
  137. $history_rf->execute;
  138. }
  139. }
  140. #---------------------------------------------------------------------------------------------------------------
  141. sub db_log_debug {
  142. my $dbh = shift;
  143. my $msg = shift;
  144. my $id = shift;
  145. if ($debug) { write_db_log($dbh,$msg,$L_DEBUG,$id); }
  146. }
  147. #---------------------------------------------------------------------------------------------------------------
  148. sub db_log_error {
  149. my $dbh = shift;
  150. my $msg = shift;
  151. if ($log_level >= $L_ERROR) {
  152. sendEmail("ERROR! ".substr($msg,0,30),$msg,1);
  153. write_db_log($dbh,$msg,$L_ERROR);
  154. }
  155. }
  156. #---------------------------------------------------------------------------------------------------------------
  157. sub db_log_info {
  158. my $dbh = shift;
  159. my $msg = shift;
  160. if ($log_level >= $L_INFO) { write_db_log($dbh,$msg,$L_INFO); }
  161. }
  162. #---------------------------------------------------------------------------------------------------------------
  163. sub db_log_verbose {
  164. my $dbh = shift;
  165. my $msg = shift;
  166. if ($log_level >= $L_VERBOSE) { write_db_log($dbh,$msg,$L_VERBOSE); }
  167. }
  168. #---------------------------------------------------------------------------------------------------------------
  169. sub db_log_warning {
  170. my $dbh = shift;
  171. my $msg = shift;
  172. if ($log_level >= $L_WARNING) {
  173. sendEmail("WARN! ".substr($msg,0,30),$msg,1);
  174. write_db_log($dbh,$msg,$L_WARNING);
  175. }
  176. }
  177. #---------------------------------------------------------------------------------------------------------------
  178. sub init_db {
  179. # Create new database handle. If we can't connect, die()
  180. my $db = DBI->connect("dbi:mysql:database=$DBNAME;host=$DBHOST","$DBUSER","$DBPASS");
  181. if ( !defined $db ) { die "Cannot connect to mySQL server: $DBI::errstr\n"; }
  182. $db->do('SET NAMES utf8');
  183. return $db;
  184. }
  185. #---------------------------------------------------------------------------------------------------------------
  186. sub init_traf_db {
  187. # Create new database handle. If we can't connect, die()
  188. my $db = DBI->connect("dbi:mysql:database=$DBNAME;host=$TRAF_HOST","$DBUSER","$DBPASS");
  189. if ( !defined $db ) { die "Cannot connect to mySQL server: $DBI::errstr\n"; }
  190. $db->do('SET NAMES utf8');
  191. return $db;
  192. }
  193. #---------------------------------------------------------------------------------------------------------------
  194. sub get_count_records {
  195. my $dbh = shift;
  196. my $table = shift;
  197. my $filter = shift;
  198. my $result = 0;
  199. return $result if (!$dbh);
  200. return $result if (!$table);
  201. my $sSQL='Select count(*) as rec_cnt from '.$table;
  202. if ($filter) { $sSQL=$sSQL." where ".$filter; }
  203. my $record = get_custom_record($dbh,$sSQL);
  204. if ($record->{rec_cnt}) { $result = $record->{rec_cnt}; }
  205. return $result;
  206. }
  207. #---------------------------------------------------------------------------------------------------------------
  208. sub get_id_record {
  209. my $dbh = shift;
  210. my $table = shift;
  211. my $filter = shift;
  212. my $result = 0;
  213. return $result if (!$dbh);
  214. return $result if (!$table);
  215. my %fields=('id'=>'1');
  216. my $record = get_record($dbh,$table,\%fields,$filter);
  217. if ($record->{id}) { $result = $record->{id}; }
  218. return $result;
  219. }
  220. #---------------------------------------------------------------------------------------------------------------
  221. sub get_record {
  222. my $dbh = shift;
  223. my $table = shift;
  224. my $field_list = shift;
  225. my $filter = shift;
  226. my $result;
  227. return $result if (!$dbh);
  228. return $result if (!$table);
  229. if ($filter) { $filter = 'where '.$filter; }
  230. my $fields='';
  231. foreach my $field (keys %$field_list) {
  232. next if (!$field);
  233. $fields=$fields.",`".$field."`";
  234. }
  235. $fields=~s/^,//;
  236. $fields=~s/,$//;
  237. if (!$fields) { $fields='*'; }
  238. my $sSQL="SELECT $fields FROM $table $filter LIMIT 1";
  239. my $list = $dbh->prepare( $sSQL );
  240. if ( !defined $list ) { die "Cannot prepare statement: $DBI::errstr\n"; }
  241. $list->execute;
  242. my $list_ref = $list->fetchrow_hashref();
  243. $list->finish();
  244. if (!$list_ref) { return $result; }
  245. return $list_ref;
  246. }
  247. #---------------------------------------------------------------------------------------------------------------
  248. sub get_records {
  249. my $dbh = shift;
  250. my $table = shift;
  251. my $field_list = shift;
  252. my $filter = shift;
  253. my @result;
  254. return @result if (!$dbh);
  255. return @result if (!$table);
  256. if ($filter) { $filter = 'where '.$filter; }
  257. my $fields='';
  258. my %field_order;
  259. my $order_index=0;
  260. foreach my $field (keys %$field_list) {
  261. next if (!$field);
  262. $fields=$fields.",`".$field."`";
  263. $field_order{$order_index}=$field;
  264. $order_index++;
  265. }
  266. $fields=~s/^,//;
  267. $fields=~s/,$//;
  268. if (!$fields) { $fields='*'; }
  269. my $sSQL="SELECT $fields FROM $table $filter";
  270. my $list = $dbh->prepare( $sSQL );
  271. if ( !defined $list ) { die "Cannot prepare statement: $DBI::errstr\n"; }
  272. $list->execute;
  273. my @list_ref = @{$list->fetchall_arrayref()};
  274. $list->finish();
  275. if (!@list_ref or !scalar @list_ref) { return @result; }
  276. foreach my $row (@list_ref) {
  277. my %record;
  278. foreach my $index (keys %field_order) {
  279. $record{$field_order{$index}}=@{$row}[$index];
  280. }
  281. push(@result,\%record);
  282. }
  283. return @result;
  284. }
  285. #---------------------------------------------------------------------------------------------------------------
  286. sub get_custom_records {
  287. my $dbh = shift;
  288. my $table = shift;
  289. my @result;
  290. return @result if (!$dbh);
  291. return @result if (!$table);
  292. my $list = $dbh->prepare( $table );
  293. if ( !defined $list ) { die "Cannot prepare statement: $DBI::errstr\n"; }
  294. $list->execute;
  295. while(my $row_ref = $list ->fetchrow_hashref) {
  296. push(@result,$row_ref);
  297. }
  298. $list->finish();
  299. return @result;
  300. }
  301. #---------------------------------------------------------------------------------------------------------------
  302. sub get_custom_record {
  303. my $dbh = shift;
  304. my $table = shift;
  305. my @result;
  306. return @result if (!$dbh);
  307. return @result if (!$table);
  308. my $list = $dbh->prepare( $table . ' LIMIT 1' );
  309. if ( !defined $list ) { die "Cannot prepare statement: $DBI::errstr\n"; }
  310. $list->execute;
  311. my $row_ref = $list ->fetchrow_hashref;
  312. $list->finish();
  313. return $row_ref;
  314. }
  315. #---------------------------------------------------------------------------------------------------------------
  316. sub get_diff_rec {
  317. my $dbh = shift;
  318. my $table = shift;
  319. my $value = shift;
  320. my $filter = shift;
  321. return if (!$dbh);
  322. return if (!$table);
  323. return if (!$filter);
  324. my $old_value = get_custom_record($dbh,"SELECT * FROM $table WHERE $filter");
  325. my $result='';
  326. foreach my $field (keys %$value) {
  327. if (!$value->{$field}) { $value->{$field}=''; }
  328. if (!$old_value->{$field}) { $old_value->{$field}=''; }
  329. if ($value->{$field}!~/^$old_value->{$field}$/) { $result = $result." $field => $value->{$field} (old: $old_value->{$field}),"; }
  330. }
  331. $result=~s/,$//;
  332. return $result;
  333. }
  334. #---------------------------------------------------------------------------------------------------------------
  335. sub update_record {
  336. my $dbh = shift;
  337. my $table = shift;
  338. my $record = shift;
  339. my $filter = shift;
  340. return if (!$dbh);
  341. return if (!$table);
  342. return if (!$filter);
  343. my $old_record = get_custom_record($dbh,"SELECT * FROM $table WHERE $filter");
  344. my $diff='';
  345. my $change_str='';
  346. my $found_changed=0;
  347. my $auth_id = 0;
  348. my $network_changed = 0;
  349. if ($table=~/User_auth/i) { $auth_id = $old_record->{'id'}; }
  350. foreach my $field (keys %$record) {
  351. if (!defined $record->{$field}) { $record->{$field}=''; }
  352. if (!defined $old_record->{$field}) { $old_record->{$field}=''; }
  353. my $old_value = $old_record->{$field};
  354. my $new_value = $record->{$field};
  355. $new_value=~s/\'//g;
  356. $new_value=~s/\"//g;
  357. if ($new_value!~/^$old_value$/) {
  358. if ($table eq 'User_auth' and exists $acl_fields{$field}) { $network_changed = 1; }
  359. $diff = $diff." $field => $record->{$field} (old: $old_record->{$field}),";
  360. $change_str = $change_str." `$field`=".$dbh->quote($record->{$field}).",";
  361. $found_changed++;
  362. }
  363. }
  364. if ($found_changed) {
  365. if ($network_changed) { $diff = $diff." `changed`='1',"; }
  366. $change_str=~s/\,$//;
  367. $diff=~s/\,$//;
  368. if ($table eq 'User_auth') { $change_str .= ", `changed_time`='".GetNowTime()."'"; }
  369. my $sSQL = "UPDATE $table SET $change_str WHERE $filter";
  370. db_log_debug($dbh,'Change table '.$table.' for '.$filter.' set: '.$diff,$auth_id);
  371. do_sql($dbh,$sSQL);
  372. } else {
  373. db_log_debug($dbh,'Request update:'.Dumper($record));
  374. db_log_debug($dbh,'Nothing change. Skip update.');
  375. }
  376. }
  377. #---------------------------------------------------------------------------------------------------------------
  378. sub insert_record {
  379. my $dbh = shift;
  380. my $table = shift;
  381. my $record = shift;
  382. return if (!$dbh);
  383. return if (!$table);
  384. my $change_str='';
  385. my $fields='';
  386. my $values='';
  387. my $new_str='';
  388. foreach my $field (keys %$record) {
  389. if (!defined $record->{$field}) { $record->{$field}=''; }
  390. my $new_value = $record->{$field};
  391. $new_value=~s/\'//g;
  392. $new_value=~s/\"//g;
  393. $fields = $fields."`$field`,";
  394. $values = $values." ".$dbh->quote($new_value).",";
  395. $new_str = $new_str." $field => $new_value,";
  396. }
  397. $fields=~s/,$//;
  398. $values=~s/,$//;
  399. $new_str=~s/,$//;
  400. my $sSQL = "INSERT INTO $table($fields) VALUES($values)";
  401. my $result = do_sql($dbh,$sSQL);
  402. if ($result) { $new_str='id: '.$result.' '.$new_str; }
  403. db_log_debug($dbh,'Add record to table '.$table.' '.$new_str);
  404. return $result;
  405. }
  406. #---------------------------------------------------------------------------------------------------------------
  407. sub delete_record {
  408. my $dbh = shift;
  409. my $table = shift;
  410. my $filter = shift;
  411. return if (!$dbh);
  412. return if (!$table);
  413. return if (!$filter);
  414. my $old_record = get_custom_record($dbh,"SELECT * FROM $table WHERE $filter");
  415. my $diff='';
  416. foreach my $field (keys %$old_record) {
  417. if (!$old_record->{$field}) { $old_record->{$field}=''; }
  418. $diff = $diff." $field => $old_record->{$field},";
  419. }
  420. $diff=~s/,$//;
  421. db_log_debug($dbh,'Delete record from table '.$table.' value: '.$diff);
  422. #never delete user ip record!
  423. if ($table eq 'User_auth') {
  424. my $sSQL = "UPDATE User_auth SET deleted=1, changed_time='".GetNowTime()."' WHERE ".$filter;
  425. do_sql($dbh,$sSQL);
  426. } else {
  427. my $sSQL = "DELETE FROM ".$table." WHERE ".$filter;
  428. do_sql($dbh,$sSQL);
  429. }
  430. }
  431. #---------------------------------------------------------------------------------------------------------------
  432. sub GetNowTime {
  433. my ($sec,$min,$hour,$day,$month,$year,$zone) = localtime(time());
  434. $month += 1;
  435. $year += 1900;
  436. my $now_str=sprintf "%04d-%02d-%02d %02d:%02d:%02d",$year,$month,$day,$hour,$min,$sec;
  437. return $now_str;
  438. }
  439. #---------------------------------------------------------------------------------------------------------------
  440. sub refresh_add_rules {
  441. my $dbh = shift;
  442. if (defined $add_rules) { undef $add_rules; }
  443. $add_rules = new Net::Patricia;
  444. #custom rules
  445. my @user_rules=get_custom_records($dbh,'select id,default_subnet from User_list where deleted=0 and LENGTH(default_subnet)>0');
  446. foreach my $subnet (@user_rules) {
  447. next if (!$subnet);
  448. next if (!$subnet->{default_subnet});
  449. next if (!$subnet->{id});
  450. eval {
  451. $add_rules->add_string($subnet->{default_subnet},$subnet->{id});
  452. };
  453. }
  454. #hotspot nets
  455. foreach my $subnet (@hotspot_network_list) {
  456. next if (!$subnet);
  457. $add_rules->add_string($subnet,$hotspot_user_id);
  458. }
  459. }
  460. #---------------------------------------------------------------------------------------------------------------
  461. sub get_new_user_id {
  462. my $dbh = shift;
  463. my $ip = shift;
  464. if (!defined $add_rules) { refresh_add_rules($dbh); }
  465. my $user_id=$add_rules->match_string($ip);
  466. if (!$user_id) { return $default_user_id; }
  467. return $user_id;
  468. }
  469. #---------------------------------------------------------------------------------------------------------------
  470. sub set_changed {
  471. my $db = shift;
  472. my $id = shift;
  473. return if (!$db or !$id);
  474. my $update_record;
  475. $update_record->{changed}=1;
  476. update_record($db,'User_auth',$update_record,"id=$id");
  477. }
  478. #---------------------------------------------------------------------------------------------------------------
  479. sub resurrection_auth {
  480. my $db = shift;
  481. my $ip = shift;
  482. my $mac = shift;
  483. my $action = shift;
  484. my $ip_aton=StrToIp($ip);
  485. my %fields=( 'user_id'=>'1', 'id'=>'2' );
  486. my $timestamp=GetNowTime();
  487. my $record=get_record($db,'User_auth',\%fields,"ip_int=$ip_aton and mac='".$mac."' and deleted=0");
  488. my $new_record;
  489. $new_record->{last_found}=$timestamp;
  490. if ($record->{user_id}) {
  491. if ($action!~/arp/i) {
  492. $new_record->{dhcp_action}=$action;
  493. $new_record->{dhcp_time}=$timestamp;
  494. update_record($db,'User_auth',$new_record,"id=$record->{id}");
  495. } else {
  496. update_record($db,'User_auth',$new_record,"id=$record->{id}");
  497. }
  498. return;
  499. }
  500. #default user
  501. my $new_user_id=get_new_user_id($db,$ip);
  502. #search changed mac
  503. %fields=( 'id'=>'1', 'mac'=>'2' );
  504. $record=get_record($db,'User_auth',\%fields,"ip_int=$ip_aton and deleted=0");
  505. if ($record->{id}) {
  506. if (!$record->{mac}) {
  507. db_log_verbose($db,"use empty auth record...");
  508. $new_record->{ip_int}=$ip_aton;
  509. $new_record->{ip_int_end}=$ip_aton;
  510. $new_record->{ip}=$ip;
  511. $new_record->{mac}=$mac;
  512. $new_record->{user_id}=$new_user_id;
  513. if ($action!~/arp/i) {
  514. $new_record->{dhcp_action}=$action;
  515. $new_record->{dhcp_time}=$timestamp;
  516. update_record($db,'User_auth',$new_record,"id=$record->{id}");
  517. } else {
  518. update_record($db,'User_auth',$new_record,"id=$record->{id}");
  519. }
  520. return;
  521. }
  522. if ($record->{mac}) {
  523. db_log_warning($db,"For ip: $ip mac change detected! Old mac: [".$record->{mac}."] New mac: [".$mac."]. Disable old auth_id: $record->{id}");
  524. my $disable_record;
  525. $disable_record->{deleted}="1";
  526. update_record($db,'User_auth',$disable_record,"id=".$record->{id});
  527. }
  528. }
  529. #seek old auth with same ip and mac
  530. my $auth_exists=get_count_records($db,'User_auth',"ip_int=".$ip_aton." and mac='".$mac."'");
  531. $new_record->{ip_int}=$ip_aton;
  532. $new_record->{ip_int_end}=$ip_aton;
  533. $new_record->{ip}=$ip;
  534. $new_record->{mac}=$mac;
  535. $new_record->{user_id}=$new_user_id;
  536. $new_record->{save_traf}="$save_detail";
  537. $new_record->{deleted}="0";
  538. $new_record->{dhcp_action}=$action;
  539. $new_record->{dhcp_time}=$timestamp;
  540. if ($auth_exists) {
  541. #found ->Resurrection old record
  542. my $resurrection_id = get_id_record($db,'User_auth',"ip_int=".$ip_aton." and mac='".$mac."'");
  543. db_log_info($db,"Resurrection auth_id: $resurrection_id with ip: $ip and mac: $mac");
  544. update_record($db,'User_auth',$new_record,"id=$resurrection_id");
  545. } else {
  546. #not found ->create new record
  547. db_log_info($db,"New ip created! ip: $ip mac: $mac");
  548. insert_record($db,'User_auth',$new_record);
  549. }
  550. #filter and status
  551. my $cur_auth_id=get_id_record($db,'User_auth',"ip='$ip' and mac='$mac' and deleted=0 ORDER BY last_found DESC");
  552. if ($cur_auth_id) {
  553. %fields=( 'enabled'=>'1', 'filter_group_id'=>'2', 'queue_id'=>'3' );
  554. $record=get_record($db,'User_list',\%fields,"id=".$new_user_id);
  555. if ($record) {
  556. $new_record->{filter_group_id}=$record->{filter_group_id};
  557. $new_record->{queue_id}=$record->{queue_id};
  558. $new_record->{enabled}="$record->{enabled}";
  559. update_record($db,'User_auth',$new_record,"id=$cur_auth_id");
  560. }
  561. } else { return; }
  562. return $cur_auth_id;
  563. }
  564. #---------------------------------------------------------------------------------------------------------------
  565. sub get_option {
  566. my $dbh=shift;
  567. my $option_id=shift;
  568. return if (!$option_id);
  569. return if (!$dbh);
  570. my $default_option = get_custom_record($dbh,'SELECT * FROM config_options WHERE id='.$option_id);
  571. my $config_options = get_custom_record($dbh,'SELECT * FROM config WHERE option_id='.$option_id);
  572. my $result;
  573. if (!$config_options) {
  574. if ($default_option->{type}=~/int/i or $default_option->{type}=~/bool/i) {
  575. $result = $default_option->{default_value}*1;
  576. } else {
  577. $result = $default_option->{default_value};
  578. }
  579. return $result;
  580. }
  581. $result = $config_options->{value};
  582. return $result;
  583. }
  584. #---------------------------------------------------------------------------------------------------------------
  585. sub init_option {
  586. my $dbh=shift;
  587. $last_refresh_config = time();
  588. $config_ref{dbh}=$dbh;
  589. $config_ref{save_detail}=get_option($dbh,23);
  590. $config_ref{add_unknown_user}=get_option($dbh,22);
  591. $config_ref{dns_server}=get_option($dbh,3);
  592. $config_ref{dhcp_server}=get_option($dbh,5);
  593. $config_ref{snmp_default_version}=get_option($dbh,9);
  594. $config_ref{snmp_default_community}=get_option($dbh,11);
  595. $config_ref{KB}=get_option($dbh,1);
  596. $config_ref{mac_discovery}=get_option($dbh,17);
  597. $config_ref{arp_discovery}=get_option($dbh,19);
  598. $config_ref{default_user_id}=get_option($dbh,20);
  599. $config_ref{admin_email}=get_option($dbh,21);
  600. $config_ref{sender_email}=get_option($dbh,52);
  601. $config_ref{send_email}=get_option($dbh,51);
  602. $config_ref{history}=get_option($dbh,26);
  603. $config_ref{history_dhcp}=get_option($dbh,27);
  604. $config_ref{router_login}=get_option($dbh,28);
  605. $config_ref{router_password}=get_option($dbh,29);
  606. $config_ref{router_port}=get_option($dbh,30);
  607. $config_ref{org_name}=get_option($dbh,32);
  608. $config_ref{domain_name}=get_option($dbh,33);
  609. $config_ref{connections_history}=get_option($dbh,35);
  610. $config_ref{auth_clear}=get_option($dbh,36);
  611. $config_ref{debug}=get_option($dbh,34);
  612. $config_ref{log_level} = get_option($dbh,53);
  613. if ($config_ref{debug}) { $config_ref{log_level} = 255; }
  614. $config_ref{urgent_sync}=get_option($dbh,50);
  615. $config_ref{ignore_hotspot_dhcp_log} = get_option($dbh,44);
  616. $config_ref{ignore_update_dhcp_event} = get_option($dbh,45);
  617. $config_ref{update_hostname_from_dhcp} = get_option($dbh,46);
  618. $config_ref{hotspot_user_id}=get_option($dbh,43);
  619. $config_ref{history_log_day}=get_option($dbh,47);
  620. $config_ref{history_syslog_day} = get_option($dbh,48);
  621. $config_ref{history_trafstat_day} = get_option($dbh,49);
  622. #$save_detail = 1; id=23
  623. $save_detail=get_option($dbh,23);
  624. #$add_unknown_user = 1; id=22
  625. $add_unknown_user=get_option($dbh,22);
  626. #$dns_server='192.168.2.12'; id=3
  627. $dns_server=get_option($dbh,3);
  628. #$dhcp_server='192.168.2.12'; id=5
  629. $dhcp_server=get_option($dbh,5);
  630. #$snmp_default_version='2'; id=9
  631. $snmp_default_version=get_option($dbh,9);
  632. #$snmp_default_community='public'; id=11
  633. $snmp_default_community=get_option($dbh,11);
  634. #$KB=1024; id=1
  635. $KB=get_option($dbh,1);
  636. #$mac_discovery; id=17
  637. $mac_discovery=get_option($dbh,17);
  638. #$arp_discovery; id=19
  639. $arp_discovery=get_option($dbh,19);
  640. #$default_user_id; id=20
  641. $default_user_id=get_option($dbh,20);
  642. #$admin_email; id=21
  643. $admin_email=get_option($dbh,21);
  644. #sender email
  645. $sender_email=get_option($dbh,52);
  646. #send email
  647. $send_email=get_option($dbh,51);
  648. #$history=15; id=26
  649. $history=get_option($dbh,26);
  650. #$history_dhcp=7; id=27
  651. $history_dhcp=get_option($dbh,27);
  652. #$router_login="admin"; id=28
  653. $router_login=get_option($dbh,28);
  654. #$router_password="admin"; id=29
  655. $router_password=get_option($dbh,29);
  656. #$router_port=23; id=30
  657. $router_port=get_option($dbh,30);
  658. #32
  659. $org_name=get_option($dbh,32);
  660. #33
  661. $domain_name=get_option($dbh,33);
  662. #35
  663. $connections_history=get_option($dbh,35);
  664. #36
  665. $auth_clear=get_option($dbh,36);
  666. #debug
  667. $debug=get_option($dbh,34);
  668. #log level
  669. $log_level = get_option($dbh,53);
  670. if ($debug) { $log_level = 255; }
  671. #urgent sync access
  672. $urgent_sync=get_option($dbh,50);
  673. $ignore_hotspot_dhcp_log = get_option($dbh,44);
  674. $ignore_update_dhcp_event = get_option($dbh,45);
  675. $update_hostname_from_dhcp = get_option($dbh,46);
  676. #$hotspot_user_id; id=43
  677. $hotspot_user_id=get_option($dbh,43);
  678. $history_log_day=get_option($dbh,47);
  679. $history_syslog_day = get_option($dbh,48);
  680. $history_trafstat_day = get_option($dbh,49);
  681. @subnets=get_custom_records($dbh,'SELECT * FROM subnets ORDER BY ip_int_start');
  682. if (defined $office_networks) { undef $office_networks; }
  683. if (defined $free_networks) { undef $free_networks; }
  684. if (defined $vpn_networks) { undef $vpn_networks; }
  685. if (defined $hotspot_networks) { undef $hotspot_networks; }
  686. if (defined $all_networks) { undef $all_networks; }
  687. $office_networks = new Net::Patricia;
  688. $free_networks = new Net::Patricia;
  689. $vpn_networks = new Net::Patricia;
  690. $hotspot_networks = new Net::Patricia;
  691. $all_networks = new Net::Patricia;
  692. @office_network_list=();
  693. @free_network_list=();
  694. @free_network_list=();
  695. @vpn_network_list=();
  696. @hotspot_network_list=();
  697. @all_network_list=();
  698. foreach my $net (@subnets) {
  699. next if (!$net->{subnet});
  700. $subnets_ref{$net->{subnet}}=$net;
  701. if ($net->{office}) {
  702. push(@office_network_list,$net->{subnet});
  703. $office_networks->add_string($net->{subnet});
  704. }
  705. if ($net->{free}) {
  706. push(@free_network_list,$net->{subnet});
  707. $free_networks->add_string($net->{subnet});
  708. }
  709. if ($net->{vpn}) {
  710. push(@vpn_network_list,$net->{subnet});
  711. $vpn_networks->add_string($net->{subnet});
  712. }
  713. if ($net->{hotspot}) {
  714. push(@hotspot_network_list,$net->{subnet});
  715. push(@all_network_list,$net->{subnet});
  716. $hotspot_networks->add_string($net->{subnet});
  717. }
  718. push(@all_network_list,$net->{subnet});
  719. $all_networks->add_string($net->{subnet});
  720. }
  721. }
  722. #---------------------------------------------------------------------------------------------------------------
  723. sub get_subnets_ref {
  724. my $dbh = shift;
  725. my @list=get_custom_records($dbh,'SELECT * FROM subnets ORDER BY ip_int_start');
  726. my $list_ref;
  727. foreach my $net (@list) {
  728. next if (!$net->{subnet});
  729. $list_ref->{$net->{subnet}}=$net;
  730. }
  731. return $list_ref;
  732. }
  733. #---------------------------------------------------------------------------------------------------------------
  734. sub get_device_by_ip {
  735. my $dbh = shift;
  736. my $ip = shift;
  737. my $netdev=get_custom_record($dbh,'SELECT * FROM devices WHERE ip="'.$ip.'"');
  738. if ($netdev and $netdev->{id}>0) { return $netdev; }
  739. my $auth_rec=get_custom_record($dbh,'SELECT user_id FROM User_auth WHERE ip="'.$ip.'" and deleted=0');
  740. if ($auth_rec and $auth_rec->{user_id}>0) {
  741. $netdev=get_custom_record($dbh,'SELECT * FROM devices WHERE user_id='.$auth_rec->{user_id});
  742. return $netdev;
  743. }
  744. return;
  745. }
  746. #---------------------------------------------------------------------------------------------------------------
  747. $dbh=init_db();
  748. init_option($dbh);
  749. 1;
  750. }