database.pm 69 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076
  1. package eyelib::database;
  2. #
  3. # Copyright (C) Roman Dmitiriev, rnd@rajven.ru
  4. #
  5. use utf8;
  6. use open ":encoding(utf8)";
  7. use strict;
  8. use English;
  9. use FindBin '$Bin';
  10. use lib "/opt/Eye/scripts";
  11. use base 'Exporter';
  12. use vars qw(@EXPORT @ISA);
  13. use eyelib::config;
  14. use eyelib::main;
  15. use Net::Patricia;
  16. use eyelib::net_utils;
  17. use Data::Dumper;
  18. use DateTime;
  19. use File::Temp qw(tempfile);
  20. use DBI;
  21. our @ISA = qw(Exporter);
  22. our @EXPORT = qw(
  23. batch_db_sql
  24. batch_db_sql_cached
  25. batch_db_sql_csv
  26. db_log_warning
  27. db_log_debug
  28. db_log_error
  29. db_log_info
  30. db_log_verbose
  31. delete_record
  32. do_sql
  33. Get_Variable
  34. Set_Variable
  35. Del_Variable
  36. get_count_records
  37. get_record_sql
  38. get_records_sql
  39. get_device_by_ip
  40. get_diff_rec
  41. get_id_record
  42. get_new_user_id
  43. is_hotspot
  44. GetNowTime
  45. GetUnixTimeByStr
  46. GetTimeStrByUnixTime
  47. get_option
  48. get_subnets_ref
  49. init_db
  50. init_option
  51. insert_record
  52. apply_device_lock
  53. set_lock_discovery
  54. unset_lock_discovery
  55. IpToStr
  56. unbind_ports
  57. resurrection_auth
  58. new_auth
  59. StrToIp
  60. get_first_line
  61. is_ad_computer
  62. update_dns_record
  63. update_dns_record_by_dhcp
  64. create_dns_cname
  65. delete_dns_cname
  66. create_dns_hostname
  67. delete_dns_hostname
  68. create_dns_ptr
  69. delete_dns_ptr
  70. update_record
  71. write_db_log
  72. set_changed
  73. recalc_quotes
  74. clean_variables
  75. $add_rules
  76. $L_WARNING
  77. $L_INFO
  78. $L_DEBUG
  79. $L_ERROR
  80. $L_VERBOSE
  81. );
  82. BEGIN
  83. {
  84. #---------------------------------------------------------------------------------------------------------------
  85. our $add_rules;
  86. our $L_ERROR = 0;
  87. our $L_WARNING = 1;
  88. our $L_INFO = 2;
  89. our $L_VERBOSE = 3;
  90. our $L_DEBUG = 255;
  91. our %acl_fields = (
  92. 'ip' => '1',
  93. 'ip_int' => '1',
  94. 'enabled'=>'1',
  95. 'dhcp'=>'1',
  96. 'filter_group_id'=>'1',
  97. 'deleted'=>'1',
  98. 'dhcp_acl'=>'1',
  99. 'queue_id'=>'1',
  100. 'mac'=>'1',
  101. 'blocked'=>'1'
  102. );
  103. our %dhcp_fields = (
  104. 'ip' => '1',
  105. 'dhcp'=>'1',
  106. 'deleted'=>'1',
  107. 'mac'=>'1',
  108. );
  109. our %dns_fields = (
  110. 'ip' => '1',
  111. 'dns_name'=>'1',
  112. 'alias'=>'1',
  113. );
  114. #---------------------------------------------------------------------------------------------------------------
  115. sub StrToIp{
  116. return unpack('N',pack('C4',split(/\./,$_[0])));
  117. }
  118. #---------------------------------------------------------------------------------------------------------------
  119. sub IpToStr{
  120. my $nIP = shift;
  121. my $res = (($nIP>>24) & 255) .".". (($nIP>>16) & 255) .".". (($nIP>>8) & 255) .".". ($nIP & 255);
  122. return $res;
  123. }
  124. #---------------------------------------------------------------------------------------------------------------
  125. sub batch_db_sql {
  126. my $db=shift;
  127. my $batch_sql=shift;
  128. return if (!$db);
  129. $db->{AutoCommit} = 0;
  130. my $apply = 0;
  131. my $sth;
  132. my @msg = ();
  133. if (ref($batch_sql) eq 'ARRAY') { @msg = @$batch_sql; } else { @msg = split("\n",$batch_sql); }
  134. foreach my $sSQL (@msg) {
  135. next if (!$sSQL);
  136. $sth = $db->prepare($sSQL) or die "Unable to prepare $sSQL" . $db->errstr;
  137. $sth->execute() or die "Unable to prepare $sSQL" . $db->errstr;
  138. $apply = 1;
  139. }
  140. if ($apply) { $sth->finish(); }
  141. $db->{AutoCommit} = 1;
  142. }
  143. #---------------------------------------------------------------------------------------------------------------
  144. sub batch_db_sql_cached {
  145. my $db = DBI->connect("dbi:$config_ref{DBTYPE}:database=$DBNAME;host=$DBHOST","$DBUSER","$DBPASS", { RaiseError => 0, AutoCommit => 0 });
  146. if ( !defined $db ) { die "Cannot connect to $config_ref{DBTYPE} server: $DBI::errstr\n"; }
  147. if ($config_ref{DBTYPE} eq 'mysql') {
  148. $db->do('SET NAMES utf8mb4');
  149. $db->{'mysql_enable_utf8'} = 1;
  150. $db->{'mysql_auto_reconnect'} = 1;
  151. }
  152. my $table= shift;
  153. my $batch_sql=shift;
  154. return if (!$db);
  155. my @msg = ();
  156. if (ref($batch_sql) eq 'ARRAY') { @msg = @$batch_sql; } else { @msg = split("\n",$batch_sql); }
  157. my $sth = $db->prepare_cached($table) or die "Unable to prepare:" . $db->errstr;
  158. foreach my $sSQL (@msg) {
  159. next if (!$sSQL);
  160. $sth->execute(@$sSQL) or die "Unable to execute:" . $db->errstr;
  161. }
  162. $db->commit();
  163. $db->disconnect();
  164. }
  165. #---------------------------------------------------------------------------------------------------------------
  166. sub batch_db_sql_csv {
  167. my $db;
  168. if ($config_ref{DBTYPE} eq 'mysql') {
  169. $db = DBI->connect("dbi:$config_ref{DBTYPE}:database=$DBNAME;host=$DBHOST","$DBUSER","$DBPASS", { RaiseError => 1, mysql_local_infile=> 1 });
  170. } else {
  171. $db = DBI->connect("dbi:$config_ref{DBTYPE}:database=$DBNAME;host=$DBHOST","$DBUSER","$DBPASS", { RaiseError => 1 });
  172. }
  173. if ( !defined $db ) { die "Cannot connect to $config_ref{DBTYPE} server: $DBI::errstr\n"; }
  174. return if (!$db);
  175. if ($config_ref{DBTYPE} eq 'mysql') {
  176. $db->do('SET NAMES utf8mb4');
  177. $db->{'mysql_enable_utf8'} = 1;
  178. $db->{'mysql_auto_reconnect'} = 1;
  179. }
  180. my $table= shift;
  181. my $data = shift;
  182. my $fh = File::Temp->new(UNLINK=>0);
  183. my $fname = $fh->filename;
  184. binmode($fh,':utf8');
  185. foreach my $row (@$data) {
  186. next if (!$row);
  187. my @tmp = @$row;
  188. my $values = 'NULL';
  189. for (my $i = 0; $i <@tmp ; $i++) {
  190. $values.=',"'.$tmp[$i].'"';
  191. }
  192. $values =~s/,$//;
  193. print $fh $values."\r\n";
  194. }
  195. close $fh;
  196. my $query = qq{ LOAD DATA LOCAL INFILE '$fname' INTO TABLE $table FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"' LINES TERMINATED BY '\r\n'; };
  197. $db->do($query);
  198. $db->disconnect;
  199. #File::Temp::cleanup();
  200. }
  201. #---------------------------------------------------------------------------------------------------------------
  202. sub do_sql {
  203. my $db=shift;
  204. my $sql=shift;
  205. return if (!$db);
  206. return if (!$sql);
  207. if ($sql!~/^select /i) { log_debug($sql); }
  208. my $sql_prep = $db->prepare($sql) or die "Unable to prepare $sql: " . $db->errstr;
  209. my $sql_ref;
  210. my $rv = $sql_prep->execute() or die "Unable to execute $sql: " . $db->errstr;
  211. if ($sql=~/^insert/i) {
  212. if ($config_ref{DBTYPE} eq 'mysql') {
  213. $sql_ref = $sql_prep->{mysql_insertid};
  214. } else {
  215. ($sql_ref) = $db->selectrow_array("SELECT lastval()");
  216. }
  217. }
  218. if ($sql=~/^select /i) { $sql_ref = $sql_prep->fetchall_arrayref() or die "Unable to select $sql: " . $db->errstr; };
  219. $sql_prep->finish();
  220. return $sql_ref;
  221. }
  222. #---------------------------------------------------------------------------------------------------------------
  223. sub get_first_line {
  224. my $msg = shift;
  225. if (!$msg) { return; }
  226. if ($msg=~ /(.*)(\n|\<br\>)/) {
  227. $msg = $1 if ($1);
  228. chomp($msg);
  229. }
  230. return $msg;
  231. }
  232. #---------------------------------------------------------------------------------------------------------------
  233. sub write_db_log {
  234. my $db=shift;
  235. my $msg=shift;
  236. my $level = shift || $L_VERBOSE;
  237. my $auth_id = shift || 0;
  238. return if (!$db);
  239. return if (!$msg);
  240. $msg=~s/[\'\"]//g;
  241. my $db_log = 0;
  242. if (!$db) { $db_log = 0; }
  243. if ($level eq $L_ERROR and $log_level >= $L_ERROR) { log_error($msg); $db_log = 1; }
  244. if ($level eq $L_WARNING and $log_level >= $L_WARNING) { log_warning($msg); $db_log = 1; }
  245. if ($level eq $L_INFO and $log_level >= $L_INFO) { log_info($msg); $db_log = 1; }
  246. if ($level eq $L_VERBOSE and $log_level >= $L_VERBOSE) { log_verbose($msg); $db_log = 1; }
  247. if ($level eq $L_DEBUG and $log_level >= $L_DEBUG) { log_debug($msg); $db_log = 1; }
  248. if ($db_log) {
  249. my $history_sql="INSERT INTO worklog(customer,message,level,auth_id) VALUES(".$db->quote($MY_NAME).",".$db->quote($msg).",$level,$auth_id)";
  250. my $history_rf=$db->prepare($history_sql) or die "Unable to prepare $history_sql:" . $db->errstr;
  251. $history_rf->execute() or die "Unable to execute $history_sql: " . $db->errstr;
  252. }
  253. }
  254. #---------------------------------------------------------------------------------------------------------------
  255. sub db_log_debug {
  256. my $db = shift;
  257. my $msg = shift;
  258. my $id = shift;
  259. if ($debug) { write_db_log($db,$msg,$L_DEBUG,$id); }
  260. }
  261. #---------------------------------------------------------------------------------------------------------------
  262. sub db_log_error {
  263. my $db = shift;
  264. my $msg = shift;
  265. if ($log_level >= $L_ERROR) {
  266. sendEmail("ERROR! ".get_first_line($msg),$msg,1);
  267. write_db_log($db,$msg,$L_ERROR);
  268. }
  269. }
  270. #---------------------------------------------------------------------------------------------------------------
  271. sub db_log_info {
  272. my $db = shift;
  273. my $msg = shift;
  274. my $id = shift;
  275. if ($log_level >= $L_INFO) { write_db_log($db,$msg,$L_INFO,$id); }
  276. }
  277. #---------------------------------------------------------------------------------------------------------------
  278. sub db_log_verbose {
  279. my $db = shift;
  280. my $msg = shift;
  281. my $id = shift;
  282. if ($log_level >= $L_VERBOSE) { write_db_log($db,$msg,$L_VERBOSE,$id); }
  283. }
  284. #---------------------------------------------------------------------------------------------------------------
  285. sub db_log_warning {
  286. my $db = shift;
  287. my $msg = shift;
  288. my $id = shift;
  289. if ($log_level >= $L_WARNING) {
  290. sendEmail("WARN! ".get_first_line($msg),$msg,1);
  291. write_db_log($db,$msg,$L_WARNING,$id);
  292. }
  293. }
  294. #---------------------------------------------------------------------------------------------------------------
  295. sub init_db {
  296. # Create new database handle. If we can't connect, die()
  297. my $db = DBI->connect("dbi:$config_ref{DBTYPE}:database=$DBNAME;host=$DBHOST","$DBUSER","$DBPASS", { RaiseError => 0, AutoCommit => 1 });
  298. if ( !defined $db ) { die "Cannot connect to mySQL server: $DBI::errstr\n"; }
  299. if ($config_ref{DBTYPE} eq 'mysql') {
  300. $db->do('SET NAMES utf8mb4');
  301. $db->{'mysql_enable_utf8'} = 1;
  302. $db->{'mysql_auto_reconnect'} = 1;
  303. }
  304. return $db;
  305. }
  306. #---------------------------------------------------------------------------------------------------------------
  307. sub get_count_records {
  308. my $db = shift;
  309. my $table = shift;
  310. my $filter = shift;
  311. my $result = 0;
  312. return $result if (!$db);
  313. return $result if (!$table);
  314. my $sSQL='SELECT COUNT(*) as rec_cnt FROM '.$table;
  315. if ($filter) { $sSQL=$sSQL." WHERE ".$filter; }
  316. my $record = get_record_sql($db,$sSQL);
  317. if ($record->{rec_cnt}) { $result = $record->{rec_cnt}; }
  318. return $result;
  319. }
  320. #---------------------------------------------------------------------------------------------------------------
  321. sub get_id_record {
  322. my $db = shift;
  323. my $table = shift;
  324. my $filter = shift;
  325. my $result = 0;
  326. return $result if (!$db);
  327. return $result if (!$table);
  328. my $record = get_record_sql($db,"SELECT id FROM $table WHERE $filter");
  329. if ($record->{id}) { $result = $record->{id}; }
  330. return $result;
  331. }
  332. #---------------------------------------------------------------------------------------------------------------
  333. sub get_records_sql {
  334. my $db = shift;
  335. my $table = shift;
  336. my @result;
  337. return @result if (!$db);
  338. return @result if (!$table);
  339. my $list = $db->prepare( $table ) or die "Unable to prepare $table:" . $db->errstr;
  340. $list->execute() or die "Unable to execute $table: " . $db->errstr;
  341. while(my $row_ref = $list->fetchrow_hashref()) { push(@result,$row_ref); }
  342. $list->finish();
  343. return @result;
  344. }
  345. #---------------------------------------------------------------------------------------------------------------
  346. sub get_record_sql {
  347. my $db = shift;
  348. my $tsql = shift;
  349. my @result;
  350. return @result if (!$db);
  351. return @result if (!$tsql);
  352. $tsql.=' LIMIT 1';
  353. my $row_ref;
  354. eval {
  355. my $list = $db->prepare($tsql) or die "Unable to prepare $tsql: " . $db->errstr;
  356. $list->execute() or die "Unable to execute $tsql: " . $db->errstr;
  357. $row_ref = $list->fetchrow_hashref();
  358. $list->finish();
  359. };
  360. if ($@) {
  361. log_error("Error apply sql: $tsql err:".$@);
  362. die "Error apply sql: $tsql";
  363. }
  364. return $row_ref;
  365. }
  366. #---------------------------------------------------------------------------------------------------------------
  367. sub unbind_ports {
  368. my $db = shift;
  369. my $device_id = shift;
  370. return if (!$db);
  371. return if (!$device_id);
  372. my @target = get_records_sql($db, "SELECT U.target_port_id,U.id FROM device_ports U WHERE U.device_id=".$device_id);
  373. foreach my $row (@target) {
  374. do_sql($db, "UPDATE device_ports SET target_port_id=0 WHERE target_port_id=".$row->{id});
  375. do_sql($db, "UPDATE device_ports SET target_port_id=0 WHERE id=".$row->{id});
  376. }
  377. }
  378. #---------------------------------------------------------------------------------------------------------------
  379. sub get_diff_rec {
  380. my $db = shift;
  381. my $table = shift;
  382. my $value = shift;
  383. my $filter = shift;
  384. return if (!$db);
  385. return if (!$table);
  386. return if (!$filter);
  387. my $old_value = get_record_sql($db,"SELECT * FROM $table WHERE $filter");
  388. my $result='';
  389. foreach my $field (keys %$value) {
  390. if (!$value->{$field}) { $value->{$field}=''; }
  391. if (!$old_value->{$field}) { $old_value->{$field}=''; }
  392. if ($value->{$field}!~/^$old_value->{$field}$/) { $result = $result." $field => $value->{$field} (old: $old_value->{$field}),"; }
  393. }
  394. $result=~s/,$//;
  395. return $result;
  396. }
  397. #---------------------------------------------------------------------------------------------------------------
  398. sub get_dns_name {
  399. my $db = shift;
  400. my $id = shift;
  401. my $auth_record = get_record_sql($db,"SELECT dns_name FROM User_auth WHERE deleted=0 AND id=".$id);
  402. if ($auth_record and $auth_record->{'dns_name'}) { return $auth_record->{'dns_name'}; }
  403. return;
  404. }
  405. #---------------------------------------------------------------------------------------------------------------
  406. sub update_record {
  407. my $db = shift;
  408. my $table = shift;
  409. my $record = shift;
  410. my $filter = shift;
  411. return if (!$db);
  412. return if (!$table);
  413. return if (!$filter);
  414. my $old_record = get_record_sql($db,"SELECT * FROM $table WHERE $filter");
  415. my $diff='';
  416. my $change_str='';
  417. my $found_changed=0;
  418. my $rec_id = 0;
  419. my $dns_changed = 0;
  420. $rec_id = $old_record->{'id'} if ($old_record->{'id'});
  421. if ($table eq "User_auth") {
  422. foreach my $field (keys %$record) {
  423. if (exists $acl_fields{$field}) { $record->{changed}="1"; }
  424. if (exists $dhcp_fields{$field}) { $record->{dhcp_changed}="1"; }
  425. if (exists $dns_fields{$field}) { $dns_changed=1; }
  426. }
  427. }
  428. foreach my $field (keys %$record) {
  429. if (!defined $record->{$field}) { $record->{$field}=''; }
  430. if (!defined $old_record->{$field}) { $old_record->{$field}=''; }
  431. my $old_value = quotemeta($old_record->{$field});
  432. my $new_value = $record->{$field};
  433. $new_value=~s/\'//g;
  434. $new_value=~s/\"//g;
  435. if ($new_value!~/^$old_value$/) {
  436. $diff = $diff." $field => $record->{$field} (old: $old_record->{$field}),";
  437. $change_str = $change_str." `$field`=".$db->quote($record->{$field}).",";
  438. $found_changed++;
  439. }
  440. }
  441. if ($found_changed) {
  442. $change_str=~s/\,$//;
  443. $diff=~s/\,$//;
  444. if ($table eq 'User_auth') {
  445. $change_str .= ", `changed_time`='".GetNowTime()."'";
  446. if ($dns_changed) {
  447. my $del_dns;
  448. if ($old_record->{'dns_name'} and $old_record->{'ip'}) {
  449. $del_dns->{'name_type'}='A';
  450. $del_dns->{'name'}=$old_record->{'dns_name'};
  451. $del_dns->{'value'}=$old_record->{'ip'};
  452. $del_dns->{'type'}='del';
  453. if ($rec_id) { $del_dns->{'auth_id'}=$rec_id; }
  454. insert_record($db,'dns_queue',$del_dns);
  455. }
  456. my $new_dns;
  457. my $dns_rec_ip = $old_record->{ip};
  458. my $dns_rec_name = $old_record->{dns_name};
  459. if ($record->{'dns_name'}) { $dns_rec_name = $record->{'dns_name'}; }
  460. if ($record->{'ip'}) { $dns_rec_ip = $record->{'ip'}; }
  461. if ($dns_rec_name and $dns_rec_ip) {
  462. $new_dns->{'name_type'}='A';
  463. $new_dns->{'name'}=$dns_rec_name;
  464. $new_dns->{'value'}=$dns_rec_ip;
  465. $new_dns->{'type'}='add';
  466. if ($rec_id) { $new_dns->{'auth_id'}=$rec_id; }
  467. insert_record($db,'dns_queue',$new_dns);
  468. }
  469. }
  470. }
  471. if ($table eq 'User_auth_alias') {
  472. if ($dns_changed) {
  473. my $del_dns;
  474. if ($old_record->{'alias'}) {
  475. $del_dns->{'name_type'}='CNAME';
  476. $del_dns->{'name'}=$old_record->{'alias'};
  477. $del_dns->{'type'}='del';
  478. $del_dns->{'value'}=get_dns_name($db,$old_record->{auth_id});
  479. $del_dns->{'auth_id'}=$old_record->{auth_id};
  480. insert_record($db,'dns_queue',$del_dns);
  481. }
  482. my $new_dns;
  483. my $dns_rec_name = $old_record->{alias};
  484. if ($record->{'alias'}) { $dns_rec_name = $record->{'alias'}; }
  485. if ($dns_rec_name) {
  486. $new_dns->{'name_type'}='CNAME';
  487. $new_dns->{'name'}=$dns_rec_name;
  488. $new_dns->{'type'}='add';
  489. $new_dns->{'value'}=get_dns_name($db,$old_record->{auth_id});
  490. $new_dns->{'auth_id'}=$rec_id;
  491. insert_record($db,'dns_queue',$new_dns);
  492. }
  493. }
  494. }
  495. my $sSQL = "UPDATE $table SET $change_str WHERE $filter";
  496. db_log_debug($db,'Change table '.$table.' for '.$filter.' set: '.$diff, $rec_id);
  497. do_sql($db,$sSQL);
  498. } else {
  499. db_log_debug($db,'Nothing change. Skip update.');
  500. }
  501. return 1;
  502. }
  503. #---------------------------------------------------------------------------------------------------------------
  504. sub insert_record {
  505. my $db = shift;
  506. my $table = shift;
  507. my $record = shift;
  508. return if (!$db);
  509. return if (!$table);
  510. my $change_str='';
  511. my $fields='';
  512. my $values='';
  513. my $new_str='';
  514. my $dns_changed = 0;
  515. if ($table eq "User_auth") {
  516. foreach my $field (keys %$record) {
  517. if (exists $acl_fields{$field}) { $record->{changed}="1"; }
  518. if (exists $dhcp_fields{$field}) { $record->{dhcp_changed}="1"; }
  519. if (exists $dns_fields{$field}) { $dns_changed=1; }
  520. }
  521. }
  522. foreach my $field (keys %$record) {
  523. if (!defined $record->{$field}) { $record->{$field}=''; }
  524. my $new_value = $record->{$field};
  525. $new_value=~s/\'//g;
  526. $new_value=~s/\"//g;
  527. $record->{$field} = $new_value;
  528. $fields = $fields."`$field`,";
  529. $values = $values." ".$db->quote($record->{$field}).",";
  530. $new_str = $new_str." $field => $record->{$field},";
  531. }
  532. $fields=~s/,$//;
  533. $values=~s/,$//;
  534. $new_str=~s/,$//;
  535. my $sSQL = "INSERT INTO $table($fields) VALUES($values)";
  536. my $result = do_sql($db,$sSQL);
  537. if ($result) {
  538. $new_str='id: '.$result.' '.$new_str;
  539. if ($table eq 'User_auth_alias' and $dns_changed) {
  540. if ($record->{'alias'}) {
  541. my $add_dns;
  542. $add_dns->{'name_type'}='CNAME';
  543. $add_dns->{'name'}=$record->{'alias'};
  544. $add_dns->{'value'}=get_dns_name($db,$record->{'auth_id'});
  545. $add_dns->{'type'}='add';
  546. $add_dns->{'auth_id'}=$record->{'auth_id'};
  547. insert_record($db,'dns_queue',$add_dns);
  548. }
  549. }
  550. if ($table eq 'User_auth' and $dns_changed) {
  551. if ($record->{'dns_name'} and $record->{'ip'} and $dns_changed) {
  552. my $add_dns;
  553. $add_dns->{'name_type'}='A';
  554. $add_dns->{'name'}=$record->{'dns_name'};
  555. $add_dns->{'value'}=$record->{'ip'};
  556. $add_dns->{'type'}='add';
  557. $add_dns->{'auth_id'}=$result;
  558. insert_record($db,'dns_queue',$add_dns);
  559. }
  560. }
  561. }
  562. db_log_debug($db,'Add record to table '.$table.' '.$new_str);
  563. return $result;
  564. }
  565. #---------------------------------------------------------------------------------------------------------------
  566. sub delete_record {
  567. my $db = shift;
  568. my $table = shift;
  569. my $filter = shift;
  570. return if (!$db);
  571. return if (!$table);
  572. return if (!$filter);
  573. my $old_record = get_record_sql($db,"SELECT * FROM $table WHERE $filter");
  574. my $diff='';
  575. foreach my $field (keys %$old_record) {
  576. if (!$old_record->{$field}) { $old_record->{$field}=''; }
  577. $diff = $diff." $field => $old_record->{$field},";
  578. }
  579. $diff=~s/,$//;
  580. db_log_debug($db,'Delete record from table '.$table.' value: '.$diff);
  581. #never delete user ip record!
  582. if ($table eq 'User_auth') {
  583. my $sSQL = "UPDATE User_auth SET changed=1, deleted=1, changed_time='".GetNowTime()."' WHERE ".$filter;
  584. do_sql($db,$sSQL);
  585. if ($old_record->{'dns_name'} and $old_record->{'ip'}) {
  586. my $del_dns;
  587. $del_dns->{'name_type'}='A';
  588. $del_dns->{'name'}=$old_record->{'dns_name'};
  589. $del_dns->{'value'}=$old_record->{'ip'};
  590. $del_dns->{'type'}='del';
  591. $del_dns->{'auth_id'}=$old_record->{'id'};
  592. insert_record($db,'dns_queue',$del_dns);
  593. }
  594. }
  595. if ($table eq 'User_auth_alias') {
  596. if ($old_record->{'alias'} and $old_record->{'auth_id'}) {
  597. my $del_dns;
  598. $del_dns->{'name_type'}='CNAME';
  599. $del_dns->{'name'}=$old_record->{'alias'};
  600. $del_dns->{'value'}=get_dns_name($db,$old_record->{'auth_id'});
  601. $del_dns->{'type'}='del';
  602. $del_dns->{'auth_id'}=$old_record->{'auth_id'};
  603. insert_record($db,'dns_queue',$del_dns);
  604. }
  605. }
  606. my $sSQL = "DELETE FROM ".$table." WHERE ".$filter;
  607. return do_sql($db,$sSQL);
  608. }
  609. #---------------------------------------------------------------------------------------------------------------
  610. sub GetNowTime {
  611. my ($sec,$min,$hour,$day,$month,$year,$zone) = localtime(time());
  612. $month += 1;
  613. $year += 1900;
  614. my $now_str=sprintf "%04d-%02d-%02d %02d:%02d:%02d",$year,$month,$day,$hour,$min,$sec;
  615. return $now_str;
  616. }
  617. #---------------------------------------------------------------------------------------------------------------
  618. sub is_hotspot {
  619. my $db = shift;
  620. my $ip = shift;
  621. my $users = new Net::Patricia;
  622. #check hotspot
  623. my @ip_rules = get_records_sql($db,'SELECT * FROM subnets WHERE hotspot=1 AND LENGTH(subnet)>0');
  624. foreach my $row (@ip_rules) { $users->add_string($row->{subnet}); }
  625. if ($users->match_string($ip)) { return 1; }
  626. return 0;
  627. }
  628. #---------------------------------------------------------------------------------------------------------------
  629. sub get_new_user_id {
  630. my $db = shift;
  631. my $ip = shift;
  632. my $mac = shift;
  633. my $hostname = shift;
  634. my $result;
  635. #check user rules
  636. $mac = mac_simplify($mac);
  637. $result->{ip} = $ip;
  638. $result->{mac} = mac_splitted($mac);
  639. $result->{dhcp_hostname} = $hostname;
  640. $result->{ou_id}=undef;
  641. $result->{user_id}=undef;
  642. #check ip
  643. if (defined $ip and $ip) {
  644. my $users = new Net::Patricia;
  645. #check ip rules
  646. my @ip_rules = get_records_sql($db,'SELECT * FROM auth_rules WHERE type=1 and LENGTH(rule)>0 AND user_id IS NOT NULL');
  647. foreach my $row (@ip_rules) { eval { $users->add_string($row->{rule},$row->{user_id}); }; }
  648. if ($users->match_string($ip)) { $result->{user_id}=$users->match_string($ip); }
  649. }
  650. #check mac
  651. if (defined $mac and $mac) {
  652. my @user_rules=get_records_sql($db,'SELECT * FROM auth_rules WHERE type=2 AND LENGTH(rule)>0 AND user_id IS NOT NULL');
  653. foreach my $user (@user_rules) {
  654. my $rule = mac_simplify($user->{rule});
  655. if ($mac=~/$rule/i) { $result->{user_id}=$user->{user_id}; }
  656. }
  657. }
  658. #check hostname
  659. if (defined $hostname and $hostname) {
  660. my @user_rules=get_records_sql($db,'SELECT * FROM auth_rules WHERE type=3 AND LENGTH(rule)>0 AND user_id IS NOT NULL');
  661. foreach my $user (@user_rules) {
  662. if ($hostname=~/$user->{rule}/i) { $result->{user_id}=$user->{user_id}; }
  663. }
  664. }
  665. #
  666. if ($result->{user_id}) { return $result; }
  667. #check ou rules
  668. #check ip
  669. if (defined $ip and $ip) {
  670. my $users = new Net::Patricia;
  671. #check hotspot
  672. my @ip_rules = get_records_sql($db,'SELECT * FROM subnets WHERE hotspot=1 AND LENGTH(subnet)>0');
  673. foreach my $row (@ip_rules) { $users->add_string($row->{subnet},$default_hotspot_ou_id); }
  674. if ($users->match_string($ip)) { $result->{ou_id}=$users->match_string($ip); }
  675. #check ip rules
  676. @ip_rules = get_records_sql($db,'SELECT * FROM auth_rules WHERE type=1 and LENGTH(rule)>0 AND ou_id IS NOT NULL');
  677. foreach my $row (@ip_rules) { eval { $users->add_string($row->{rule},$row->{ou_id}); }; }
  678. if ($users->match_string($ip)) { $result->{ou_id}=$users->match_string($ip); }
  679. }
  680. #check mac
  681. if (defined $mac and $mac) {
  682. my @user_rules=get_records_sql($db,'SELECT * FROM auth_rules WHERE type=2 AND LENGTH(rule)>0 AND ou_id IS NOT NULL');
  683. foreach my $user (@user_rules) {
  684. my $rule = mac_simplify($user->{rule});
  685. if ($mac=~/$rule/i) { $result->{ou_id}=$user->{ou_id}; }
  686. }
  687. }
  688. #check hostname
  689. if (defined $hostname and $hostname) {
  690. my @user_rules=get_records_sql($db,'SELECT * FROM auth_rules WHERE type=3 AND LENGTH(rule)>0 AND ou_id IS NOT NULL');
  691. foreach my $user (@user_rules) {
  692. if ($hostname=~/$user->{rule}/i) { $result->{ou_id}=$user->{ou_id}; }
  693. }
  694. }
  695. if (!$result->{ou_id}) { $result->{ou_id}=$default_user_ou_id; }
  696. return $result;
  697. }
  698. #---------------------------------------------------------------------------------------------------------------
  699. sub set_changed {
  700. my $db = shift;
  701. my $id = shift;
  702. return if (!$db or !$id);
  703. my $update_record;
  704. $update_record->{changed}=1;
  705. update_record($db,'User_auth',$update_record,"id=$id");
  706. }
  707. #---------------------------------------------------------------------------------------------------------------
  708. sub update_dns_record {
  709. my $hdb = shift;
  710. my $auth_id = shift;
  711. return if (!$config_ref{enable_dns_updates});
  712. #get domain
  713. my $ad_zone = get_option($hdb,33);
  714. #get dns server
  715. my $ad_dns = get_option($hdb,3);
  716. my $enable_ad_dns_update = ($ad_zone and $ad_dns and $config_ref{enable_dns_updates});
  717. log_debug("Auth id: ".$auth_id);
  718. log_debug("enable_ad_dns_update: ".$enable_ad_dns_update);
  719. log_debug("DNS update flags - zone: ".$ad_zone.", dns: ".$ad_dns.", enable_ad_dns_update: ".$enable_ad_dns_update);
  720. my @dns_queue = get_records_sql($hdb,"SELECT * FROM dns_queue WHERE auth_id=".$auth_id." ORDER BY id ASC");
  721. if (!@dns_queue or !scalar @dns_queue) { return; }
  722. foreach my $dns_cmd (@dns_queue) {
  723. my $fqdn = '';
  724. my $fqdn_ip = '';
  725. my $fqdn_parent = '';
  726. my $static_exists = 0;
  727. my $static_ref = '';
  728. my $static_ok = 0;
  729. eval {
  730. if ($dns_cmd->{name_type}=~/^cname$/i) {
  731. $fqdn=lc($dns_cmd->{name});
  732. $fqdn=~s/\.$ad_zone$//i;
  733. $fqdn=~s/\.$//;
  734. if ($dns_cmd->{value}) {
  735. $fqdn_parent=lc($dns_cmd->{value});
  736. $fqdn_parent=~s/\.$ad_zone$//i;
  737. $fqdn_parent=~s/\.$//;
  738. }
  739. #skip update unknown domain
  740. if ($fqdn =~/\./ or $fqdn_parent =~/\./) { next; }
  741. $fqdn = $fqdn.".".$ad_zone;
  742. $fqdn_parent = $fqdn_parent.".".$ad_zone;
  743. #remove cname
  744. if ($dns_cmd->{type} eq 'del') {
  745. delete_dns_cname($fqdn_parent,$fqdn,$ad_zone,$ad_dns,$hdb);
  746. }
  747. #create cname
  748. if ($dns_cmd->{type} eq 'add') {
  749. create_dns_cname($fqdn_parent,$fqdn,$ad_zone,$ad_dns,$hdb);
  750. }
  751. }
  752. if ($dns_cmd->{name_type}=~/^a$/i) {
  753. $fqdn=lc($dns_cmd->{name});
  754. $fqdn=~s/\.$ad_zone$//i;
  755. $fqdn=~s/\.$//;
  756. if (!$dns_cmd->{value}) { next; }
  757. $fqdn_ip=lc($dns_cmd->{value});
  758. #skip update unknown domain
  759. if ($fqdn =~/\./) { next; }
  760. $fqdn = $fqdn.".".$ad_zone;
  761. #dns update disabled?
  762. my $maybe_update_dns=( $enable_ad_dns_update and $office_networks->match_string($fqdn_ip) );
  763. if (!$maybe_update_dns) {
  764. db_log_info($hdb,"FOUND Auth_id: $auth_id. DNS update disabled.");
  765. next;
  766. }
  767. #get aliases
  768. my @aliases = get_records_sql($hdb,"SELECT * FROM User_auth_alias WHERE auth_id=".$auth_id);
  769. #remove A & PTR
  770. if ($dns_cmd->{type} eq 'del') {
  771. #remove aliases
  772. if (@aliases and scalar @aliases) {
  773. foreach my $alias (@aliases) {
  774. delete_dns_cname($fqdn,$alias->{alias},$ad_zone,$ad_dns,$hdb) if ($alias->{alias});
  775. delete_dns_hostname($fqdn,$alias->{alias},$ad_zone,$ad_dns,$hdb) if ($alias->{alias});
  776. }
  777. }
  778. #remove main record
  779. delete_dns_hostname($fqdn,$fqdn_ip,$ad_zone,$ad_dns,$hdb);
  780. delete_dns_ptr($fqdn,$fqdn_ip,$ad_zone,$ad_dns,$hdb);
  781. }
  782. #create A & PTR
  783. if ($dns_cmd->{type} eq 'add') {
  784. my @dns_record=ResolveNames($fqdn,$dns_server);
  785. $static_exists = (scalar @dns_record>0);
  786. if ($static_exists) {
  787. $static_ref = join(' ',@dns_record);
  788. foreach my $dns_a (@dns_record) {
  789. if ($dns_a=~/^$fqdn_ip$/) { $static_ok = 1; }
  790. }
  791. db_log_debug($hdb,"Dns record for static record $fqdn: $static_ref");
  792. }
  793. #skip update if already exists
  794. if ($static_ok) {
  795. db_log_debug($hdb,"Static record for $fqdn [$static_ok] correct.");
  796. next;
  797. }
  798. #create record
  799. create_dns_hostname($fqdn,$fqdn_ip,$ad_zone,$ad_dns,$hdb);
  800. create_dns_ptr($fqdn,$fqdn_ip,$ad_zone,$ad_dns,$hdb);
  801. #create aliases
  802. if (@aliases and scalar @aliases) {
  803. foreach my $alias (@aliases) {
  804. create_dns_cname($fqdn,$alias->{alias},$ad_zone,$ad_dns,$hdb) if ($alias->{alias});
  805. }
  806. }
  807. }
  808. }
  809. };
  810. if ($@) { log_error("Error dns commands: $@"); }
  811. }
  812. }
  813. #---------------------------------------------------------------------------------------------------------------
  814. sub is_ad_computer {
  815. my $hdb = shift;
  816. my $computer_name = shift;
  817. if (!$computer_name or $computer_name =~/UNDEFINED/i) { return 0; }
  818. my $ad_check = get_option($hdb,73);
  819. if (!$ad_check) { return 1; }
  820. my $ad_zone = get_option($hdb,33);
  821. if ($computer_name =~/\./) {
  822. if ($computer_name!~/\.$ad_zone$/i) {
  823. db_log_verbose($hdb,"The domain of the computer $computer_name does not match the domain of the organization $ad_zone. Skip update.");
  824. return 0;
  825. }
  826. }
  827. if ($computer_name =~/^(.+)\./) {
  828. $computer_name = $1;
  829. }
  830. my $ad_computer_name = trim($computer_name).'$';
  831. my $name_in_cache = get_record_sql($hdb,"SELECT * FROM ad_comp_cache WHERE name='".$computer_name."'");
  832. if ($name_in_cache) { return 1; }
  833. my %name_found=do_exec_ref('/usr/bin/getent passwd '.$ad_computer_name);
  834. if (!$name_found{output} or $name_found{status} ne 0) {
  835. db_log_verbose($hdb,"The computer ".uc($ad_computer_name)." was not found in the domain $ad_zone. Skip update.");
  836. return 0;
  837. }
  838. do_sql($hdb,"INSERT INTO ad_comp_cache(name) VALUES('".$computer_name."') ON DUPLICATE KEY UPDATE name='".$computer_name."';");
  839. return 1;
  840. }
  841. #---------------------------------------------------------------------------------------------------------------
  842. sub update_dns_record_by_dhcp {
  843. my $hdb = shift;
  844. my $dhcp_record = shift;
  845. my $auth_record = shift;
  846. return if (!$config_ref{enable_dns_updates});
  847. my $ad_zone = get_option($hdb,33);
  848. my $ad_dns = get_option($hdb,3);
  849. $update_hostname_from_dhcp = get_option($hdb,46) || 0;
  850. my $subnets_dhcp = get_subnets_ref($hdb);
  851. my $enable_ad_dns_update = ($ad_zone and $ad_dns and $update_hostname_from_dhcp);
  852. log_debug("Dhcp record: ".Dumper($dhcp_record));
  853. log_debug("Subnets: ".Dumper($subnets_dhcp->{$dhcp_record->{network}->{subnet}}));
  854. log_debug("enable_ad_dns_update: ".$enable_ad_dns_update);
  855. 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);
  856. my $maybe_update_dns=(is_ad_computer($hdb,$dhcp_record->{hostname_utf8}) and ($dhcp_record->{type}=~/add/i or $dhcp_record->{type}=~/old/i) and $enable_ad_dns_update and $subnets_dhcp->{$dhcp_record->{network}->{subnet}}->{dhcp_update_hostname});
  857. if (!$maybe_update_dns) {
  858. db_log_debug($hdb,"FOUND Auth_id: $auth_record->{id}. DNS update don't needed.");
  859. return 0;
  860. }
  861. log_debug("DNS update enabled.");
  862. #update dns block
  863. my $fqdn_static;
  864. if ($auth_record->{dns_name}) {
  865. $fqdn_static=lc($auth_record->{dns_name});
  866. if ($fqdn_static!~/\.$ad_zone$/i) {
  867. $fqdn_static=~s/\.$//;
  868. $fqdn_static=lc($fqdn_static.'.'.$ad_zone);
  869. }
  870. }
  871. my $fqdn=lc(trim($dhcp_record->{hostname_utf8}));
  872. if ($fqdn!~/\.$ad_zone$/i) {
  873. $fqdn=~s/\.$//;
  874. $fqdn=lc($fqdn.'.'.$ad_zone);
  875. }
  876. db_log_debug($hdb,"FOUND Auth_id: $auth_record->{id} dns_name: $fqdn_static dhcp_hostname: $fqdn");
  877. #check exists static dns name
  878. my $static_exists = 0;
  879. my $dynamic_exists = 0;
  880. my $static_ok = 0;
  881. my $dynamic_ok = 0;
  882. my $static_ref;
  883. my $dynamic_ref;
  884. if ($fqdn_static ne '') {
  885. my @dns_record=ResolveNames($fqdn_static,$dns_server);
  886. $static_exists = (scalar @dns_record>0);
  887. if ($static_exists) {
  888. $static_ref = join(' ',@dns_record);
  889. foreach my $dns_a (@dns_record) {
  890. if ($dns_a=~/^$dhcp_record->{ip}$/) { $static_ok = $dns_a; }
  891. }
  892. }
  893. } else { $static_ok = 1; }
  894. if ($fqdn ne '') {
  895. my @dns_record=ResolveNames($fqdn,$dns_server);
  896. $dynamic_exists = (scalar @dns_record>0);
  897. if ($dynamic_exists) {
  898. $dynamic_ref = join(' ',@dns_record);
  899. foreach my $dns_a (@dns_record) {
  900. if ($dns_a=~/^$dhcp_record->{ip}$/) { $dynamic_ok = $dns_a; }
  901. }
  902. }
  903. }
  904. db_log_debug($hdb,"Dns record for static record $fqdn_static: $static_ok");
  905. db_log_debug($hdb,"Dns record for dhcp-hostname $fqdn: $dynamic_ok");
  906. if ($fqdn_static ne '') {
  907. if (!$static_ok) {
  908. db_log_info($hdb,"Static record mismatch! Expected $fqdn_static => $dhcp_record->{ip}, recivied: $static_ref");
  909. if (!$static_exists) {
  910. db_log_info($hdb,"Static dns hostname defined but not found. Create it ($fqdn_static => $dhcp_record->{ip})!");
  911. create_dns_hostname($fqdn_static,$dhcp_record->{ip},$ad_zone,$ad_dns,$hdb);
  912. }
  913. } else {
  914. db_log_debug($hdb,"Static record for $fqdn_static [$static_ok] correct.");
  915. }
  916. }
  917. if ($fqdn ne '' and $dynamic_ok ne '') { db_log_debug($hdb,"Dynamic record for $fqdn [$dynamic_ok] correct. No changes required."); }
  918. if ($fqdn ne '' and !$dynamic_ok) {
  919. #log event without email alert
  920. log_error("Dynamic record mismatch! Expected: $fqdn => $dhcp_record->{ip}, recivied: $dynamic_ref. Checking the status.");
  921. #check exists hostname
  922. my $another_hostname_exists = 0;
  923. my $hostname_filter = ' LOWER(dns_name) REGEXP("^'.lc($dhcp_record->{hostname_utf8}).'\.*$")';
  924. if ($fqdn_static ne '' and $fqdn !~/$fqdn_static/) {
  925. $hostname_filter = $hostname_filter . ' or LOWER(dns_name) REGEXP("^'.lc($auth_record->{dns_name}).'\.*$")';
  926. }
  927. #check exists another records with some static hostname
  928. my $filter_sql = 'SELECT * FROM User_auth WHERE id<>'.$auth_record->{id}.' and deleted=0 and ('.$hostname_filter.') ORDER BY last_found DESC';
  929. db_log_debug($hdb,"Search dhcp hostname by: ".$filter_sql);
  930. my $name_record = get_record_sql($hdb,$filter_sql);
  931. if ($name_record->{dns_name} =~/^$fqdn$/i or $name_record->{dns_name} =~/^$dhcp_record->{hostname_utf8}$/i) {
  932. $another_hostname_exists = 1;
  933. }
  934. if (!$another_hostname_exists) {
  935. if ($fqdn_static and $fqdn_static ne '') {
  936. if ($fqdn_static!~/$fqdn/) {
  937. db_log_info($hdb,"Hostname from dhcp request $fqdn differs from static dns hostname $fqdn_static. Ignore dynamic binding!");
  938. # delete_dns_hostname($fqdn,$dhcp_record->{ip},$ad_zone,$ad_dns,$hdb);
  939. # create_dns_hostname($fqdn,$dhcp_record->{ip},$ad_zone,$ad_dns,$hdb);
  940. }
  941. } else {
  942. db_log_info($hdb,"Rewrite aliases if exists for $fqdn => $dhcp_record->{ip}");
  943. #get and remove aliases
  944. my @aliases = get_records_sql($hdb,"SELECT * FROM User_auth_alias WHERE auth_id=".$auth_record->{id});
  945. if (@aliases and scalar @aliases) {
  946. foreach my $alias (@aliases) {
  947. delete_dns_cname($fqdn_static,$alias->{alias},$ad_zone,$ad_dns,$hdb) if ($alias->{alias});
  948. }
  949. }
  950. db_log_info($hdb,"Static dns hostname not defined. Create dns record by dhcp request. $fqdn => $dhcp_record->{ip}");
  951. create_dns_hostname($fqdn,$dhcp_record->{ip},$ad_zone,$ad_dns,$hdb);
  952. if (@aliases and scalar @aliases) {
  953. foreach my $alias (@aliases) {
  954. create_dns_cname($fqdn_static,$alias->{alias},$ad_zone,$ad_dns,$hdb) if ($alias->{alias});
  955. }
  956. }
  957. }
  958. } else {
  959. 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.");
  960. }
  961. }
  962. #end update dns block
  963. }
  964. #------------------------------------------------------------------------------------------------------------
  965. sub apply_device_lock {
  966. my $db = shift;
  967. my $device_id = shift;
  968. my $iteration = shift || 0;
  969. $iteration++;
  970. if ($iteration>2) { return 0; }
  971. 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);
  972. if (!$dev) { return 0; }
  973. if (!$dev->{'discovery_locked'}) { return set_lock_discovery($db,$device_id); }
  974. #if timestamp undefined, set and return
  975. if (!$dev->{'locked_timestamp'}) { return set_lock_discovery($db,$device_id); }
  976. #wait for discovery
  977. my $wait_time = $dev->{'locked_timestamp'} + 30 - time();
  978. if ($wait_time<0) { return set_lock_discovery($db,$device_id); }
  979. sleep($wait_time);
  980. return apply_device_lock($db,$device_id,$iteration);
  981. }
  982. #------------------------------------------------------------------------------------------------------------
  983. sub set_lock_discovery {
  984. my $db = shift;
  985. my $device_id = shift;
  986. my $new;
  987. $new->{'discovery_locked'} = 1;
  988. $new->{'locked_timestamp'} = GetNowTime();
  989. if (update_record($db,'devices',$new,'id='.$device_id)) { return 1; }
  990. return 0;
  991. }
  992. #------------------------------------------------------------------------------------------------------------
  993. sub unset_lock_discovery {
  994. my $db = shift;
  995. my $device_id = shift;
  996. my $new;
  997. $new->{'discovery_locked'} = 0;
  998. $new->{'locked_timestamp'} = GetNowTime();
  999. if (update_record($db,'devices',$new,'id='.$device_id)) { return 1; }
  1000. return 0;
  1001. }
  1002. #------------------------------------------------------------------------------------------------------------
  1003. sub create_dns_cname {
  1004. my $fqdn = shift;
  1005. my $alias = shift;
  1006. my $zone = shift;
  1007. my $server = shift;
  1008. my $db = shift;
  1009. #skip update domain controllers
  1010. if (!$db) {
  1011. log_info("DNS-UPDATE: Zone $zone Server: $server CNAME: $alias for $fqdn");
  1012. } else {
  1013. db_log_info($db,"DNS-UPDATE: Zone $zone Server: $server CNAME: $alias for $fqdn ");
  1014. }
  1015. my $ad_zone = get_option($db,33);
  1016. my $nsupdate_file = "/tmp/".$fqdn."-nsupdate";
  1017. my @add_dns;
  1018. if ($config_ref{dns_server_type}=~/windows/i) {
  1019. push(@add_dns,"gsstsig");
  1020. push(@add_dns,"server $server");
  1021. push(@add_dns,"zone $zone");
  1022. push(@add_dns,"update add $alias 3600 cname $fqdn.");
  1023. push(@add_dns,"send");
  1024. write_to_file($nsupdate_file,\@add_dns);
  1025. do_exec('/usr/bin/kinit -k -t /opt/Eye/scripts/cfg/dns_updater.keytab dns_updater@'.uc($ad_zone).' && /usr/bin/nsupdate "'.$nsupdate_file.'"');
  1026. }
  1027. if ($config_ref{dns_server_type}=~/bind/i) {
  1028. push(@add_dns,"server $server");
  1029. push(@add_dns,"zone $zone");
  1030. push(@add_dns,"update add $alias 3600 cname $fqdn.");
  1031. push(@add_dns,"send");
  1032. write_to_file($nsupdate_file,\@add_dns);
  1033. do_exec('/usr/bin/nsupdate -k /etc/bind/rndc.key "'.$nsupdate_file.'"');
  1034. }
  1035. if (-e "$nsupdate_file") { unlink "$nsupdate_file"; }
  1036. }
  1037. #---------------------------------------------------------------------------------------------------------------
  1038. sub delete_dns_cname {
  1039. my $fqdn = shift;
  1040. my $alias = shift;
  1041. my $zone = shift;
  1042. my $server = shift;
  1043. my $db = shift;
  1044. if (!$db) {
  1045. log_info("DNS-UPDATE: Delete => Zone $zone Server: $server CNAME: $alias for $fqdn ");
  1046. } else {
  1047. db_log_info($db,"DNS-UPDATE: Delete => Zone $zone Server: $server CNAME: $alias for $fqdn");
  1048. }
  1049. my $ad_zone = get_option($db,33);
  1050. my $nsupdate_file = "/tmp/".$fqdn."-nsupdate";
  1051. my @add_dns;
  1052. if ($config_ref{dns_server_type}=~/windows/i) {
  1053. push(@add_dns,"gsstsig");
  1054. push(@add_dns,"server $server");
  1055. push(@add_dns,"zone $zone");
  1056. push(@add_dns,"update delete $alias cname ");
  1057. push(@add_dns,"send");
  1058. write_to_file($nsupdate_file,\@add_dns);
  1059. do_exec('/usr/bin/kinit -k -t /opt/Eye/scripts/cfg/dns_updater.keytab dns_updater@'.uc($ad_zone).' && /usr/bin/nsupdate "'.$nsupdate_file.'"');
  1060. }
  1061. if ($config_ref{dns_server_type}=~/bind/i) {
  1062. push(@add_dns,"server $server");
  1063. push(@add_dns,"zone $zone");
  1064. push(@add_dns,"update delete $alias cname");
  1065. push(@add_dns,"send");
  1066. write_to_file($nsupdate_file,\@add_dns);
  1067. do_exec('/usr/bin/nsupdate -k /etc/bind/rndc.key "'.$nsupdate_file.'"');
  1068. }
  1069. if (-e "$nsupdate_file") { unlink "$nsupdate_file"; }
  1070. }
  1071. #------------------------------------------------------------------------------------------------------------
  1072. sub create_dns_hostname {
  1073. my $fqdn = shift;
  1074. my $ip = shift;
  1075. my $zone = shift;
  1076. my $server = shift;
  1077. my $db = shift;
  1078. #skip update domain controllers
  1079. if ($fqdn=~/^dc[0-9]{1,2}\./i) { return; }
  1080. if (!$db) {
  1081. log_info("DNS-UPDATE: Zone $zone Server: $server A: $fqdn IP: $ip");
  1082. } else {
  1083. db_log_info($db,"DNS-UPDATE: Zone $zone Server: $server A: $fqdn IP: $ip");
  1084. }
  1085. my $ad_zone = get_option($db,33);
  1086. my $nsupdate_file = "/tmp/".$fqdn."-nsupdate";
  1087. my @add_dns;
  1088. if ($config_ref{dns_server_type}=~/windows/i) {
  1089. push(@add_dns,"gsstsig");
  1090. push(@add_dns,"server $server");
  1091. push(@add_dns,"zone $zone");
  1092. push(@add_dns,"update add $fqdn 3600 A $ip");
  1093. push(@add_dns,"send");
  1094. write_to_file($nsupdate_file,\@add_dns);
  1095. do_exec('/usr/bin/kinit -k -t /opt/Eye/scripts/cfg/dns_updater.keytab dns_updater@'.uc($ad_zone).' && /usr/bin/nsupdate "'.$nsupdate_file.'"');
  1096. }
  1097. if ($config_ref{dns_server_type}=~/bind/i) {
  1098. push(@add_dns,"server $server");
  1099. push(@add_dns,"zone $zone");
  1100. push(@add_dns,"update add $fqdn 3600 A $ip");
  1101. push(@add_dns,"send");
  1102. write_to_file($nsupdate_file,\@add_dns);
  1103. do_exec('/usr/bin/nsupdate -k /etc/bind/rndc.key "'.$nsupdate_file.'"');
  1104. }
  1105. if (-e "$nsupdate_file") { unlink "$nsupdate_file"; }
  1106. }
  1107. #---------------------------------------------------------------------------------------------------------------
  1108. sub delete_dns_hostname {
  1109. my $fqdn = shift;
  1110. my $ip = shift;
  1111. my $zone = shift;
  1112. my $server = shift;
  1113. my $db = shift;
  1114. #skip update domain controllers
  1115. if ($fqdn=~/^dc[0-9]{1,2}\./i) { return; }
  1116. if (!$db) {
  1117. log_info("DNS-UPDATE: Delete => Zone $zone Server: $server A: $fqdn IP: $ip");
  1118. } else {
  1119. db_log_info($db,"DNS-UPDATE: Delete => Zone $zone Server: $server A: $fqdn IP: $ip");
  1120. }
  1121. my $ad_zone = get_option($db,33);
  1122. my $nsupdate_file = "/tmp/".$fqdn."-nsupdate";
  1123. my @add_dns;
  1124. if ($config_ref{dns_server_type}=~/windows/i) {
  1125. push(@add_dns,"gsstsig");
  1126. push(@add_dns,"server $server");
  1127. push(@add_dns,"zone $zone");
  1128. push(@add_dns,"update delete $fqdn A");
  1129. push(@add_dns,"send");
  1130. write_to_file($nsupdate_file,\@add_dns);
  1131. do_exec('/usr/bin/kinit -k -t /opt/Eye/scripts/cfg/dns_updater.keytab dns_updater@'.uc($ad_zone).' && /usr/bin/nsupdate "'.$nsupdate_file.'"');
  1132. }
  1133. if ($config_ref{dns_server_type}=~/bind/i) {
  1134. push(@add_dns,"server $server");
  1135. push(@add_dns,"zone $zone");
  1136. push(@add_dns,"update delete $fqdn A");
  1137. push(@add_dns,"send");
  1138. write_to_file($nsupdate_file,\@add_dns);
  1139. do_exec('/usr/bin/nsupdate -k /etc/bind/rndc.key "'.$nsupdate_file.'"');
  1140. }
  1141. if (-e "$nsupdate_file") { unlink "$nsupdate_file"; }
  1142. }
  1143. #---------------------------------------------------------------------------------------------------------------
  1144. sub create_dns_ptr {
  1145. my $fqdn = shift;
  1146. my $ip = shift;
  1147. my $ad_zone = shift;
  1148. my $server = shift;
  1149. my $db = shift;
  1150. my $radr;
  1151. my $zone;
  1152. #skip update domain controllers
  1153. if ($fqdn=~/^dc[0-9]{1,2}\./i) { return; }
  1154. if ($ip =~ /([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})(\/[0-9]{1,2}){0,1}/) {
  1155. return 0 if($1 > 255 || $2 > 255 || $3 > 255 || $4 > 255);
  1156. $radr = "$4.$3.$2.$1.in-addr.arpa";
  1157. $zone = "$3.$2.$1.in-addr.arpa";
  1158. }
  1159. if (!$radr or !$zone) { return 0; }
  1160. if (!$db) { return 0; }
  1161. db_log_info($db,"DNS-UPDATE: Zone $zone Server: $server A: $fqdn PTR: $ip");
  1162. my $nsupdate_file = "/tmp/".$radr."-nsupdate";
  1163. my @add_dns;
  1164. if ($config_ref{dns_server_type}=~/windows/i) {
  1165. push(@add_dns,"gsstsig");
  1166. push(@add_dns,"server $server");
  1167. push(@add_dns,"zone $zone");
  1168. push(@add_dns,"update add $radr 3600 PTR $fqdn.");
  1169. push(@add_dns,"send");
  1170. write_to_file($nsupdate_file,\@add_dns);
  1171. 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.'"';
  1172. do_exec($run_cmd);
  1173. }
  1174. if ($config_ref{dns_server_type}=~/bind/i) {
  1175. push(@add_dns,"server $server");
  1176. push(@add_dns,"zone $zone");
  1177. push(@add_dns,"update add $radr 3600 PTR $fqdn.");
  1178. push(@add_dns,"send");
  1179. write_to_file($nsupdate_file,\@add_dns);
  1180. my $run_cmd = '/usr/bin/nsupdate -k /etc/bind/rndc.key "'.$nsupdate_file.'"';
  1181. do_exec($run_cmd);
  1182. }
  1183. if (-e "$nsupdate_file") { unlink "$nsupdate_file"; }
  1184. }
  1185. #---------------------------------------------------------------------------------------------------------------
  1186. sub delete_dns_ptr {
  1187. my $fqdn = shift;
  1188. my $ip = shift;
  1189. my $ad_zone = shift;
  1190. my $server = shift;
  1191. my $db = shift;
  1192. my $radr;
  1193. my $zone;
  1194. #skip update domain controllers
  1195. if ($fqdn=~/^dc[0-9]{1,2}\./i) { return; }
  1196. if ($ip =~ /([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})(\/[0-9]{1,2}){0,1}/) {
  1197. return 0 if($1 > 255 || $2 > 255 || $3 > 255 || $4 > 255);
  1198. $radr = "$4.$3.$2.$1.in-addr.arpa";
  1199. $zone = "$3.$2.$1.in-addr.arpa";
  1200. }
  1201. if (!$radr or !$zone) { return 0; }
  1202. if (!$db) { return 0 ; }
  1203. db_log_info($db,"DNS-UPDATE: Delete => Zone $zone Server: $server A: $fqdn PTR: $ip");
  1204. my $nsupdate_file = "/tmp/".$radr."-nsupdate";
  1205. my @add_dns;
  1206. if ($config_ref{dns_server_type}=~/windows/i) {
  1207. push(@add_dns,"gsstsig");
  1208. push(@add_dns,"server $server");
  1209. push(@add_dns,"zone $zone");
  1210. push(@add_dns,"update delete $radr PTR");
  1211. push(@add_dns,"send");
  1212. write_to_file($nsupdate_file,\@add_dns);
  1213. 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.'"';
  1214. do_exec($run_cmd);
  1215. }
  1216. if ($config_ref{dns_server_type}=~/bind/i) {
  1217. push(@add_dns,"server $server");
  1218. push(@add_dns,"zone $zone");
  1219. push(@add_dns,"update delete $radr PTR");
  1220. push(@add_dns,"send");
  1221. write_to_file($nsupdate_file,\@add_dns);
  1222. my $run_cmd = '/usr/bin/nsupdate -k /etc/bind/rndc.key "'.$nsupdate_file.'"';
  1223. do_exec($run_cmd);
  1224. }
  1225. if (-e "$nsupdate_file") { unlink "$nsupdate_file"; }
  1226. }
  1227. #---------------------------------------------------------------------------------------------------------------
  1228. sub new_user {
  1229. my $db = shift;
  1230. my $user_info = shift;
  1231. my $user;
  1232. if ($user_info->{mac}) {
  1233. $user->{login}=mac_splitted($user_info->{mac});
  1234. } else {
  1235. $user->{login}=$user_info->{ip};
  1236. }
  1237. if ($user_info->{dhcp_hostname}) { $user->{fio}=$user_info->{dhcp_hostname}; }
  1238. if (!$user->{fio}) { $user->{fio}=$user_info->{ip}; }
  1239. my $login_count = get_count_records($db,"User_list","(login LIKE '".$user->{login}."(%)') OR (login='".$user->{login}."')");
  1240. if ($login_count) { $login_count++; $user->{login} .="(".$login_count.")"; }
  1241. $user->{ou_id} = $user_info->{ou_id};
  1242. my $ou_info = get_record_sql($db,"SELECT * FROM OU WHERE id=".$user_info->{'ou_id'});
  1243. if ($ou_info) {
  1244. $user->{'enabled'} = $ou_info->{'enabled'};
  1245. $user->{'queue_id'} = $ou_info->{'queue_id'};
  1246. $user->{'filter_group_id'} = $ou_info->{'filter_group_id'};
  1247. }
  1248. my $result = insert_record($db,"User_list",$user);
  1249. if ($result and $config_ref{auto_mac_rule} and $user_info->{mac}) {
  1250. my $auth_rule;
  1251. $auth_rule->{user_id} = $result;
  1252. $auth_rule->{type} = 2;
  1253. $auth_rule->{rule} = mac_splitted($user_info->{mac});
  1254. insert_record($db,"auth_rules",$auth_rule);
  1255. }
  1256. return $result;
  1257. }
  1258. #---------------------------------------------------------------------------------------------------------------
  1259. sub get_ip_subnet {
  1260. my $db = shift;
  1261. my $ip = shift;
  1262. if (!$ip) { return; }
  1263. my $ip_aton = StrToIp($ip);
  1264. 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)");
  1265. return $user_subnet;
  1266. }
  1267. #---------------------------------------------------------------------------------------------------------------
  1268. sub find_mac_in_subnet {
  1269. my $db = shift;
  1270. my $ip = shift;
  1271. my $mac = shift;
  1272. if (!$ip or !$mac) { return; }
  1273. my $ip_subnet = get_ip_subnet($db, $ip);
  1274. if (!$ip_subnet) { return; }
  1275. 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");
  1276. my $auth_count = 0;
  1277. my $result;
  1278. $result->{'count'} = 0;
  1279. foreach my $row (@t_auth) {
  1280. next if (!$row);
  1281. $auth_count++;
  1282. $result->{'count'} = $auth_count;
  1283. $result->{$auth_count} = $row->{'id'};
  1284. push(@{$result->{'users_id'}}, $row->{'user_id'});
  1285. }
  1286. return $result;
  1287. }
  1288. #---------------------------------------------------------------------------------------------------------------
  1289. sub resurrection_auth {
  1290. my $db = shift;
  1291. my $ip_record = shift;
  1292. my $ip = $ip_record->{'ip'};
  1293. my $mac = $ip_record->{'mac'};
  1294. my $action = $ip_record->{'type'};
  1295. my $hostname = $ip_record->{'hostname_utf8'};
  1296. my $client_id = $ip_record->{'client-id'};
  1297. if (!exists $ip_record->{ip_aton}) { $ip_record->{ip_aton}=StrToIp($ip); }
  1298. if (!exists $ip_record->{hotspot}) { $ip_record->{hotspot}=is_hotspot($db,$ip); }
  1299. my $ip_aton=$ip_record->{ip_aton};
  1300. my $timestamp=GetNowTime();
  1301. my $record=get_record_sql($db,'SELECT * FROM User_auth WHERE `deleted`=0 AND `ip_int`='.$ip_aton.' AND `mac`="'.$mac.'"');
  1302. my $new_record;
  1303. $new_record->{last_found}=$timestamp;
  1304. if ($client_id) { $new_record->{'client-id'} = $client_id; }
  1305. #auth found?
  1306. if ($record->{user_id}) {
  1307. #update timestamp and return
  1308. if ($action!~/arp/i) {
  1309. $new_record->{dhcp_action}=$action;
  1310. $new_record->{dhcp_time}=$timestamp;
  1311. if ($hostname) { $new_record->{dhcp_hostname} = $hostname; }
  1312. update_record($db,'User_auth',$new_record,"id=$record->{id}");
  1313. } else {
  1314. update_record($db,'User_auth',$new_record,"id=$record->{id}");
  1315. }
  1316. return $record->{id};
  1317. }
  1318. my $user_subnet=$office_networks->match_string($ip);
  1319. if ($user_subnet->{static}) {
  1320. db_log_warning($db,"Unknown ip+mac found in static subnet! Abort create record for ip: $ip mac: [".$mac."]");
  1321. return 0;
  1322. }
  1323. #search changed mac
  1324. $record=get_record_sql($db,'SELECT * FROM User_auth WHERE `ip_int`='.$ip_aton." and deleted=0");
  1325. if ($record->{id}) {
  1326. #if found record with same ip but another mac
  1327. if (!$record->{mac}) {
  1328. db_log_verbose($db,"use empty auth record...");
  1329. $new_record->{mac}=$mac;
  1330. if ($action!~/arp/i) {
  1331. $new_record->{dhcp_action}=$action;
  1332. $new_record->{dhcp_time}=$timestamp;
  1333. if ($hostname) { $new_record->{dhcp_hostname} = $hostname; }
  1334. update_record($db,'User_auth',$new_record,"id=$record->{id}");
  1335. } else {
  1336. update_record($db,'User_auth',$new_record,"id=$record->{id}");
  1337. }
  1338. return $record->{id};
  1339. }
  1340. if ($record->{mac}) {
  1341. 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});
  1342. my $disable_record;
  1343. $disable_record->{deleted}="1";
  1344. update_record($db,'User_auth',$disable_record,"id=".$record->{id});
  1345. }
  1346. }
  1347. #default user
  1348. my $new_user_info=get_new_user_id($db,$ip,$mac,$hostname);
  1349. my $new_user_id;
  1350. if ($new_user_info->{user_id}) { $new_user_id = $new_user_info->{user_id}; }
  1351. if (!$new_user_id) { $new_user_id = new_user($db,$new_user_info); }
  1352. my $mac_exists=find_mac_in_subnet($db,$ip,$mac);
  1353. #disable dhcp for same mac in one ip subnet
  1354. if ($mac_exists and $mac_exists->{'count'}) { $new_record->{dhcp}=0; }
  1355. #seek old auth with same ip and mac
  1356. my $auth_exists=get_count_records($db,'User_auth',"ip_int=".$ip_aton." and mac='".$mac."'");
  1357. $new_record->{ip_int}=$ip_aton;
  1358. $new_record->{ip}=$ip;
  1359. $new_record->{mac}=$mac;
  1360. $new_record->{user_id}=$new_user_id;
  1361. $new_record->{save_traf}="$save_detail";
  1362. $new_record->{deleted}="0";
  1363. $new_record->{dhcp_action}=$action;
  1364. $new_record->{dhcp_time}=$timestamp;
  1365. if ($auth_exists) {
  1366. #found ->Resurrection old record
  1367. my $resurrection_id = get_id_record($db,'User_auth',"ip_int=".$ip_aton." and mac='".$mac."'");
  1368. if (!$ip_record->{hotspot}) { db_log_warning($db,"Resurrection auth_id: $resurrection_id with ip: $ip and mac: $mac"); }
  1369. else { db_log_info($db,"Resurrection auth_id: $resurrection_id with ip: $ip and mac: $mac"); }
  1370. update_record($db,'User_auth',$new_record,"id=$resurrection_id");
  1371. } else {
  1372. #not found ->create new record
  1373. 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"); }
  1374. insert_record($db,'User_auth',$new_record);
  1375. }
  1376. #filter and status
  1377. my $cur_auth_id=get_id_record($db,'User_auth',"ip='$ip' and mac='$mac' and deleted=0 ORDER BY last_found DESC");
  1378. if ($cur_auth_id) {
  1379. my $user_record=get_record_sql($db,"SELECT * FROM User_list WHERE id=".$new_user_id);
  1380. if ($user_record) {
  1381. $new_record->{ou_id}=$user_record->{ou_id};
  1382. $new_record->{comments}=$user_record->{fio};
  1383. $new_record->{filter_group_id}=$user_record->{filter_group_id};
  1384. $new_record->{queue_id}=$user_record->{queue_id};
  1385. $new_record->{enabled}="$user_record->{enabled}";
  1386. update_record($db,'User_auth',$new_record,"id=$cur_auth_id");
  1387. }
  1388. } else { return; }
  1389. return $cur_auth_id;
  1390. }
  1391. #---------------------------------------------------------------------------------------------------------------
  1392. sub new_auth {
  1393. my $db = shift;
  1394. my $ip = shift;
  1395. my $ip_aton=StrToIp($ip);
  1396. my $record=get_record_sql($db,'SELECT id FROM User_auth WHERE `deleted`=0 AND `ip_int`='.$ip_aton);
  1397. if ($record->{id}) { return $record->{id}; }
  1398. #default user
  1399. my $new_user_info=get_new_user_id($db,$ip,undef,undef);
  1400. my $new_user_id;
  1401. if ($new_user_info->{user_id}) { $new_user_id = $new_user_info->{user_id}; }
  1402. if ($new_user_info->{ou_id}) { $new_user_id = new_user($db,$new_user_info); }
  1403. my $user_record=get_record_sql($db,"SELECT * FROM User_list WHERE id=".$new_user_id);
  1404. my $timestamp=GetNowTime();
  1405. my $new_record;
  1406. $new_record->{ip_int}=$ip_aton;
  1407. $new_record->{ip}=$ip;
  1408. $new_record->{user_id}=$new_user_id;
  1409. $new_record->{save_traf}="$save_detail";
  1410. $new_record->{deleted}="0";
  1411. $new_record->{dhcp_action}='netflow';
  1412. $new_record->{ou_id}=$user_record->{ou_id};
  1413. $new_record->{filter_group_id}=$user_record->{filter_group_id};
  1414. $new_record->{queue_id}=$user_record->{queue_id};
  1415. $new_record->{enabled}="$user_record->{enabled}";
  1416. if ($user_record->{fio}) { $new_record->{comments}=$user_record->{fio}; }
  1417. my $cur_auth_id=insert_record($db,'User_auth',$new_record);
  1418. db_log_warning($db,"New ip created by netflow! ip: $ip") if ($cur_auth_id);
  1419. return $cur_auth_id;
  1420. }
  1421. #---------------------------------------------------------------------------------------------------------------
  1422. sub get_option {
  1423. my $db=shift;
  1424. my $option_id=shift;
  1425. return if (!$option_id);
  1426. return if (!$db);
  1427. my $default_option = get_record_sql($db,'SELECT * FROM config_options WHERE id='.$option_id);
  1428. my $config_options = get_record_sql($db,'SELECT * FROM config WHERE option_id='.$option_id);
  1429. my $result;
  1430. if (!$config_options) {
  1431. if ($default_option->{'type'}=~/^(int|bool)/i) { $result = $default_option->{'default_value'}*1; };
  1432. if ($default_option->{'type'}=~/^(string|text)/i) { $result = $default_option->{'default_value'}; }
  1433. if ($default_option->{'type'}=~/^list/i) { $result = $default_option->{'default_value'}; }
  1434. return $result;
  1435. }
  1436. $result = $config_options->{'value'};
  1437. return $result;
  1438. }
  1439. #---------------------------------------------------------------------------------------------------------------
  1440. sub init_option {
  1441. my $db=shift;
  1442. $last_refresh_config = time();
  1443. $config_ref{version}='';
  1444. my $version_record = get_record_sql($db,"SELECT version FROM version WHERE version is NOT NULL");
  1445. if ($version_record) { $config_ref{version}=$version_record->{version}; }
  1446. $config_ref{dbh}=$db;
  1447. $config_ref{save_detail}=get_option($db,23);
  1448. $config_ref{add_unknown_user}=get_option($db,22);
  1449. $config_ref{dhcp_server}=get_option($db,5);
  1450. $config_ref{snmp_default_version}=get_option($db,9);
  1451. $config_ref{snmp_default_community}=get_option($db,11);
  1452. $config_ref{KB}=get_option($db,1);
  1453. if ($config_ref{KB} ==0) { $config_ref{KB}=1000; }
  1454. if ($config_ref{KB} ==1) { $config_ref{KB}=1024; }
  1455. $config_ref{admin_email}=get_option($db,21);
  1456. $config_ref{sender_email}=get_option($db,52);
  1457. $config_ref{send_email}=get_option($db,51);
  1458. $config_ref{history}=get_option($db,26);
  1459. $config_ref{history_dhcp}=get_option($db,27);
  1460. $config_ref{router_login}=get_option($db,28);
  1461. $config_ref{router_password}=get_option($db,29);
  1462. $config_ref{router_port}=get_option($db,30);
  1463. $config_ref{org_name}=get_option($db,32);
  1464. $config_ref{domain_name}=get_option($db,33);
  1465. $config_ref{connections_history}=get_option($db,35);
  1466. $config_ref{debug}=get_option($db,34);
  1467. $config_ref{log_level} = get_option($db,53);
  1468. if ($config_ref{debug}) { $config_ref{log_level} = 255; }
  1469. $config_ref{urgent_sync}=get_option($db,50);
  1470. $config_ref{ignore_hotspot_dhcp_log} = get_option($db,44);
  1471. $config_ref{ignore_update_dhcp_event} = get_option($db,45);
  1472. $config_ref{update_hostname_from_dhcp} = get_option($db,46);
  1473. $config_ref{history_log_day}=get_option($db,47);
  1474. $config_ref{history_syslog_day} = get_option($db,48);
  1475. $config_ref{history_trafstat_day} = get_option($db,49);
  1476. $config_ref{enable_quotes} = get_option($db,54);
  1477. $config_ref{netflow_step} = get_option($db,55);
  1478. $config_ref{traffic_ipstat_history} = get_option($db,56);
  1479. $config_ref{nagios_url} = get_option($db,57);
  1480. $config_ref{cacti_url} = get_option($db,58);
  1481. $config_ref{torrus_url} = get_option($db,59);
  1482. $config_ref{wiki_url} = get_option($db,60);
  1483. $config_ref{stat_url} = get_option($db,62);
  1484. $config_ref{wiki_path} = get_option($db,61);
  1485. $config_ref{auto_mac_rule} = get_option($db,64);
  1486. #network configuration mode
  1487. $config_ref{config_mode}=get_option($db,68);
  1488. #auto clean old user record
  1489. $config_ref{clean_empty_user}=get_option($db,69);
  1490. #dns_server_type
  1491. $config_ref{dns_server}=get_option($db,3);
  1492. $config_ref{dns_server_type}=get_option($db,70);
  1493. $config_ref{enable_dns_updates}=get_option($db,71);
  1494. #$save_detail = 1; id=23
  1495. $save_detail=get_option($db,23);
  1496. #$add_unknown_user = 1; id=22
  1497. $add_unknown_user=get_option($db,22);
  1498. #$dns_server='192.168.2.12'; id=3
  1499. $dns_server=get_option($db,3);
  1500. #$dhcp_server='192.168.2.12'; id=5
  1501. $dhcp_server=get_option($db,5);
  1502. #$snmp_default_version='2'; id=9
  1503. $snmp_default_version=get_option($db,9);
  1504. #$snmp_default_community='public'; id=11
  1505. $snmp_default_community=get_option($db,11);
  1506. #$KB=1024; id=1
  1507. $KB=$config_ref{KB};
  1508. #$admin_email; id=21
  1509. $admin_email=get_option($db,21);
  1510. #sender email
  1511. $sender_email=get_option($db,52);
  1512. #send email
  1513. $send_email=get_option($db,51);
  1514. #$history=15; id=26
  1515. $history=get_option($db,26);
  1516. #$history_dhcp=7; id=27
  1517. $history_dhcp=get_option($db,27);
  1518. #$router_login="admin"; id=28
  1519. $router_login=get_option($db,28);
  1520. #$router_password="admin"; id=29
  1521. $router_password=get_option($db,29);
  1522. #$router_port=23; id=30
  1523. $router_port=get_option($db,30);
  1524. #32
  1525. $org_name=get_option($db,32);
  1526. #33
  1527. $domain_name=get_option($db,33);
  1528. #35
  1529. $connections_history=get_option($db,35);
  1530. #debug
  1531. $debug=get_option($db,34);
  1532. #log level
  1533. $log_level = get_option($db,53);
  1534. if ($debug) { $log_level = 255; }
  1535. #urgent sync access
  1536. $urgent_sync=get_option($db,50);
  1537. $ignore_hotspot_dhcp_log = get_option($db,44);
  1538. $ignore_update_dhcp_event = get_option($db,45);
  1539. $update_hostname_from_dhcp = get_option($db,46);
  1540. $history_log_day=get_option($db,47);
  1541. $history_syslog_day = get_option($db,48);
  1542. $history_trafstat_day = get_option($db,49);
  1543. my $ou = get_record_sql($db,"SELECT id FROM OU WHERE default_users = 1");
  1544. if (!$ou) { $default_user_ou_id = 0; } else { $default_user_ou_id = $ou->{'id'}; }
  1545. $ou = get_record_sql($db,"SELECT id FROM OU WHERE default_hotspot = 1");
  1546. if (!$ou) { $default_hotspot_ou_id = $default_user_ou_id; } else { $default_hotspot_ou_id = $ou->{'id'}; }
  1547. @subnets=get_records_sql($db,'SELECT * FROM subnets ORDER BY ip_int_start');
  1548. if (defined $office_networks) { undef $office_networks; }
  1549. if (defined $free_networks) { undef $free_networks; }
  1550. if (defined $vpn_networks) { undef $vpn_networks; }
  1551. if (defined $hotspot_networks) { undef $hotspot_networks; }
  1552. if (defined $all_networks) { undef $all_networks; }
  1553. $office_networks = new Net::Patricia;
  1554. $free_networks = new Net::Patricia;
  1555. $vpn_networks = new Net::Patricia;
  1556. $hotspot_networks = new Net::Patricia;
  1557. $all_networks = new Net::Patricia;
  1558. @office_network_list=();
  1559. @free_network_list=();
  1560. @free_network_list=();
  1561. @vpn_network_list=();
  1562. @hotspot_network_list=();
  1563. @all_network_list=();
  1564. foreach my $net (@subnets) {
  1565. next if (!$net->{subnet});
  1566. $subnets_ref{$net->{subnet}}=$net;
  1567. if ($net->{office}) {
  1568. push(@office_network_list,$net->{subnet});
  1569. $office_networks->add_string($net->{subnet},$net);
  1570. }
  1571. if ($net->{free}) {
  1572. push(@free_network_list,$net->{subnet});
  1573. $free_networks->add_string($net->{subnet},$net);
  1574. }
  1575. if ($net->{vpn}) {
  1576. push(@vpn_network_list,$net->{subnet});
  1577. $vpn_networks->add_string($net->{subnet},$net);
  1578. }
  1579. if ($net->{hotspot}) {
  1580. push(@hotspot_network_list,$net->{subnet});
  1581. push(@all_network_list,$net->{subnet});
  1582. $hotspot_networks->add_string($net->{subnet},$net);
  1583. }
  1584. push(@all_network_list,$net->{subnet});
  1585. $all_networks->add_string($net->{subnet},$net);
  1586. }
  1587. }
  1588. #---------------------------------------------------------------------------------------------------------------
  1589. sub get_subnets_ref {
  1590. my $db = shift;
  1591. my @list=get_records_sql($db,'SELECT * FROM subnets ORDER BY ip_int_start');
  1592. my $list_ref;
  1593. foreach my $net (@list) {
  1594. next if (!$net->{subnet});
  1595. $list_ref->{$net->{subnet}}=$net;
  1596. }
  1597. return $list_ref;
  1598. }
  1599. #---------------------------------------------------------------------------------------------------------------
  1600. sub get_device_by_ip {
  1601. my $db = shift;
  1602. my $ip = shift;
  1603. my $netdev=get_record_sql($db,'SELECT * FROM devices WHERE ip="'.$ip.'"');
  1604. if ($netdev and $netdev->{id}>0) { return $netdev; }
  1605. my $auth_rec=get_record_sql($db,'SELECT user_id FROM User_auth WHERE ip="'.$ip.'" and deleted=0');
  1606. if ($auth_rec and $auth_rec->{user_id}>0) {
  1607. $netdev=get_record_sql($db,'SELECT * FROM devices WHERE user_id='.$auth_rec->{user_id});
  1608. return $netdev;
  1609. }
  1610. return;
  1611. }
  1612. #---------------------------------------------------------------------------------------------------------------
  1613. sub GetUnixTimeByStr {
  1614. my $time_str = shift;
  1615. $time_str =~s/\//-/g;
  1616. $time_str = trim($time_str);
  1617. my ($sec,$min,$hour,$day,$mon,$year) = (localtime())[0,1,2,3,4,5];
  1618. $year+=1900;
  1619. $mon++;
  1620. if ($time_str =~/^([0-9]{2,4})\-([0-9]{1,2})-([0-9]{1,2})\s+/) {
  1621. $year = $1; $mon = $2; $day = $3;
  1622. }
  1623. if ($time_str =~/([0-9]{1,2})\:([0-9]{1,2})\:([0-9]{1,2})$/) {
  1624. $hour = $1; $min = $2; $sec = $3;
  1625. }
  1626. my $result = mktime($sec,$min,$hour,$day,$mon-1,$year-1900);
  1627. return $result;
  1628. }
  1629. #---------------------------------------------------------------------------------------------------------------
  1630. sub GetTimeStrByUnixTime {
  1631. my $time = shift || time();
  1632. my ($sec, $min, $hour, $mday, $mon, $year) = (localtime($time))[0,1,2,3,4,5];
  1633. my $result = strftime("%Y-%m-%d %H:%M:%S",$sec, $min, $hour, $mday, $mon, $year);
  1634. return $result;
  1635. }
  1636. #---------------------------------------------------------------------------------------------------------------
  1637. sub Set_Variable {
  1638. my $db = shift;
  1639. my $name = shift || $MY_NAME;
  1640. my $value = shift || $$;
  1641. my $timeshift = shift || 60;
  1642. Del_Variable($db,$name);
  1643. my $clean_variables = time() + $timeshift;
  1644. my ($sec,$min,$hour,$day,$month,$year,$zone) = localtime($clean_variables);
  1645. $month++;
  1646. $year += 1900;
  1647. my $clean_str=sprintf "%04d-%02d-%02d %02d:%02d:%02d",$year,$month,$day,$hour,$min,$sec;
  1648. my $clean_variables_date=$db->quote($clean_str);
  1649. do_sql($db,"INSERT INTO variables(name,value,clear_time) VALUES('".$name."','".$value."',".$clean_variables_date.");");
  1650. }
  1651. #---------------------------------------------------------------------------------------------------------------
  1652. sub Get_Variable {
  1653. my $db = shift;
  1654. my $name = shift || $MY_NAME;
  1655. my $variable=get_record_sql($db,'SELECT `value` FROM `variables` WHERE name="'.$name.'"');
  1656. if (!$variable and $variable->{'value'}) { return $variable->{'value'}; }
  1657. return;
  1658. }
  1659. #---------------------------------------------------------------------------------------------------------------
  1660. sub Del_Variable {
  1661. my $db = shift;
  1662. my $name = shift || $MY_NAME;
  1663. do_sql($db,"DELETE FROM `variables` WHERE name='".$name."';");
  1664. }
  1665. #---------------------------------------------------------------------------------------------------------------
  1666. sub recalc_quotes {
  1667. my $db = shift;
  1668. my $calc_id = shift || $$;
  1669. return if (!get_option($db,54));
  1670. clean_variables($db);
  1671. return if (Get_Variable($db,'RECALC'));
  1672. my $timeshift = get_option($db,55);
  1673. if ($timeshift >5 ) { $timeshift=$timeshift-1; }
  1674. Set_Variable($db,'RECALC',$calc_id,time()+$timeshift*60);
  1675. my $now = DateTime->now(time_zone=>'local');
  1676. my $day_start = $db->quote($now->ymd("-")." 00:00:00");
  1677. my $day_dur = DateTime::Duration->new( days => 1 );
  1678. my $tomorrow = $now+$day_dur;
  1679. my $day_stop = $db->quote($tomorrow->ymd("-")." 00:00:00");
  1680. $now->set(day=>1);
  1681. my $month_start=$db->quote($now->ymd("-")." 00:00:00");
  1682. my $month_dur = DateTime::Duration->new( months => 1 );
  1683. my $next_month = $now + $month_dur;
  1684. $next_month->set(day=>1);
  1685. my $month_stop = $db->quote($next_month->ymd("-")." 00:00:00");
  1686. #get user limits
  1687. 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";
  1688. my @authlist_ref = get_records_sql($db,$user_auth_list_sql);
  1689. my %user_stats;
  1690. my %auth_info;
  1691. foreach my $row (@authlist_ref) {
  1692. $auth_info{$row->{auth_id}}{user_id}=$row->{id};
  1693. $auth_info{$row->{auth_id}}{day_limit}=$row->{auth_day};
  1694. $auth_info{$row->{auth_id}}{month_limit}=$row->{auth_month};
  1695. $auth_info{$row->{auth_id}}{day}=0;
  1696. $auth_info{$row->{auth_id}}{month}=0;
  1697. $user_stats{$row->{id}}{day_limit}=$row->{day_quota};
  1698. $user_stats{$row->{id}}{month_limit}=$row->{month_quota};
  1699. $user_stats{$row->{id}}{day}=0;
  1700. $user_stats{$row->{id}}{month}=0;
  1701. }
  1702. #recalc quotes - global
  1703. #day
  1704. my $day_sql="SELECT User_stats.auth_id, SUM( byte_in + byte_out ) AS traf_all FROM User_stats
  1705. WHERE User_stats.`timestamp`>= $day_start AND User_stats.`timestamp`< $day_stop GROUP BY User_stats.auth_id";
  1706. my @day_stats = get_records_sql($db,$day_sql);
  1707. foreach my $row (@day_stats) {
  1708. my $user_id=$auth_info{$row->{auth_id}}{user_id};
  1709. $auth_info{$row->{auth_id}}{day}=$row->{traf_all};
  1710. $user_stats{$user_id}{day}+=$row->{traf_all};
  1711. }
  1712. #month
  1713. my $month_sql="SELECT User_stats.auth_id, SUM( byte_in + byte_out ) AS traf_all FROM User_stats
  1714. WHERE User_stats.`timestamp`>= $month_start AND User_stats.`timestamp`< $month_stop GROUP BY User_stats.auth_id";
  1715. my @month_stats = get_records_sql($db,$month_sql);
  1716. foreach my $row (@month_stats) {
  1717. my $user_id=$auth_info{$row->{auth_id}}{user_id};
  1718. $auth_info{$row->{auth_id}}{month}=$row->{traf_all};
  1719. $user_stats{$user_id}{month}+=$row->{traf_all};
  1720. }
  1721. foreach my $auth_id (keys %auth_info) {
  1722. next if (!$auth_info{$auth_id}{day_limit});
  1723. next if (!$auth_info{$auth_id}{month_limit});
  1724. my $day_limit=$auth_info{$auth_id}{day_limit}*$KB*$KB;
  1725. my $month_limit=$auth_info{$auth_id}{month_limit}*$KB*$KB;
  1726. my $blocked_d=($auth_info{$auth_id}{day}>$day_limit);
  1727. my $blocked_m=($auth_info{$auth_id}{month}>$month_limit);
  1728. if ($blocked_d or $blocked_m) {
  1729. my $history_msg;
  1730. 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; }
  1731. 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; }
  1732. do_sql($db,"UPDATE User_auth set blocked=1, changed=1 where id=$auth_id");
  1733. db_log_verbose($db,$history_msg);
  1734. }
  1735. }
  1736. foreach my $user_id (keys %user_stats) {
  1737. next if (!$user_stats{$user_id}{day_limit});
  1738. next if (!$user_stats{$user_id}{month_limit});
  1739. my $day_limit=$user_stats{$user_id}{day_limit}*$KB*$KB;
  1740. my $month_limit=$user_stats{$user_id}{month_limit}*$KB*$KB;
  1741. my $blocked_d=($user_stats{$user_id}{day}>$day_limit);
  1742. my $blocked_m=($user_stats{$user_id}{month}>$month_limit);
  1743. if ($blocked_d or $blocked_m) {
  1744. my $history_msg;
  1745. 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; }
  1746. 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; }
  1747. do_sql($db,"UPDATE User_user set blocked=1 where id=$user_id");
  1748. do_sql($db,"UPDATE User_auth set blocked=1, changed=1 where user_id=$user_id");
  1749. db_log_verbose($db,$history_msg);
  1750. }
  1751. }
  1752. Del_Variable($db,'RECALC');
  1753. }
  1754. #---------------------------------------------------------------------------------------------------------------
  1755. sub clean_variables {
  1756. my $db = shift;
  1757. #clean temporary variables
  1758. my $clean_variables = time();
  1759. my ($sec,$min,$hour,$day,$month,$year,$zone) = localtime($clean_variables);
  1760. $month++;
  1761. $year += 1900;
  1762. my $now_str=sprintf "%04d-%02d-%02d %02d:%02d:%02d",$year,$month,$day,$hour,$min,$sec;
  1763. my $clean_variables_date=$db->quote($now_str);
  1764. do_sql($db,"DELETE FROM `variables` WHERE clear_time<=$clean_variables_date");
  1765. #clean old AD computer cache
  1766. my $now = DateTime->now(time_zone=>'local');
  1767. my $day_dur = DateTime::Duration->new( days => 1 );
  1768. my $clean_date = $now - $day_dur;
  1769. my $clean_str = $dbh->quote($clean_date->ymd("-")." 00:00:00");
  1770. do_sql($db,"DELETE FROM `ad_comp_cache` WHERE last_found<=$clean_str");
  1771. }
  1772. #---------------------------------------------------------------------------------------------------------------
  1773. #skip init for upgrade
  1774. if ($MY_NAME!~/upgrade.pl/) {
  1775. $dbh=init_db();
  1776. init_option($dbh);
  1777. clean_variables($dbh);
  1778. Set_Variable($dbh);
  1779. }
  1780. 1;
  1781. }