1 | <?php |
||
2 | /* |
||
3 | * Copyright (C) 2014 <[email protected]> |
||
4 | * Modified and Relicensed by <[email protected]> under the expressed |
||
5 | * permission by the Copyright-Holder <[email protected]>. |
||
6 | * |
||
7 | * This program is free software: you can redistribute it and/or modify |
||
8 | * it under the terms of the GNU General Public License as published by |
||
9 | * the Free Software Foundation, either version 3 of the License, or |
||
10 | * (at your option) any later version. |
||
11 | * |
||
12 | * This program is distributed in the hope that it will be useful, |
||
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
||
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the |
||
15 | * GNU General Public License for more details. |
||
16 | * |
||
17 | * You should have received a copy of the GNU General Public License |
||
18 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
||
19 | * */ |
||
20 | |||
21 | namespace LibreNMS; |
||
22 | |||
23 | use LibreNMS\DB\Eloquent; |
||
24 | use LibreNMS\Authentication\LegacyAuth; |
||
25 | |||
26 | class IRCBot |
||
27 | { |
||
28 | |||
29 | private $last_activity = ''; |
||
30 | |||
31 | private $data = ''; |
||
32 | |||
33 | private $authd = array(); |
||
34 | |||
35 | private $debug = false; |
||
36 | |||
37 | private $server = ''; |
||
38 | |||
39 | private $port = ''; |
||
40 | |||
41 | private $ssl = false; |
||
42 | |||
43 | private $pass = ''; |
||
44 | |||
45 | private $nick = 'LibreNMS'; |
||
46 | |||
47 | private $chan = array(); |
||
48 | |||
49 | private $commands = array( |
||
50 | 'auth', |
||
51 | 'quit', |
||
52 | 'listdevices', |
||
53 | 'device', |
||
54 | 'port', |
||
55 | 'down', |
||
56 | 'version', |
||
57 | 'status', |
||
58 | 'log', |
||
59 | 'help', |
||
60 | 'reload', |
||
61 | 'join', |
||
62 | ); |
||
63 | |||
64 | private $external = array(); |
||
65 | |||
66 | private $tick = 62500; |
||
67 | |||
68 | |||
69 | public function __construct() |
||
70 | { |
||
71 | global $config; |
||
72 | $this->log('Setting up IRC-Bot..'); |
||
73 | |||
74 | $this->config = $config; |
||
75 | $this->debug = $this->config['irc_debug']; |
||
76 | $this->config['irc_authtime'] = $this->config['irc_authtime'] ? $this->config['irc_authtime'] : 3; |
||
77 | $this->max_retry = $this->config['irc_maxretry']; |
||
78 | $this->server = $this->config['irc_host']; |
||
79 | if ($this->config['irc_port'][0] == '+') { |
||
80 | $this->ssl = true; |
||
81 | $this->port = substr($this->config['irc_port'], 1); |
||
82 | } else { |
||
83 | $this->port = $this->config['irc_port']; |
||
84 | } |
||
85 | |||
86 | if ($this->config['irc_nick']) { |
||
87 | $this->nick = $this->config['irc_nick']; |
||
88 | } |
||
89 | |||
90 | if ($this->config['irc_chan']) { |
||
91 | if (is_array($this->config['irc_chan'])) { |
||
92 | $this->chan = $this->config['irc_chan']; |
||
93 | } elseif (strstr($this->config['irc_chan'], ',')) { |
||
94 | $this->chan = explode(',', $this->config['irc_chan']); |
||
95 | } else { |
||
96 | $this->chan = array($this->config['irc_chan']); |
||
97 | } |
||
98 | } |
||
99 | |||
100 | if ($this->config['irc_alert_chan']) { |
||
101 | if (strstr($this->config['irc_alert_chan'], ',')) { |
||
102 | $this->config['irc_alert_chan'] = explode(',', $this->config['irc_alert_chan']); |
||
103 | } else { |
||
104 | $this->config['irc_alert_chan'] = array($this->config['irc_alert_chan']); |
||
105 | } |
||
106 | } |
||
107 | |||
108 | if ($this->config['irc_pass']) { |
||
109 | $this->pass = $this->config['irc_pass']; |
||
110 | } |
||
111 | |||
112 | $this->loadExternal(); |
||
113 | $this->log('Starting IRC-Bot..'); |
||
114 | $this->init(); |
||
115 | }//end __construct() |
||
116 | |||
117 | |||
118 | private function loadExternal() |
||
119 | { |
||
120 | if (!$this->config['irc_external']) { |
||
121 | return true; |
||
122 | } |
||
123 | |||
124 | $this->log('Caching external commands...'); |
||
125 | if (!is_array($this->config['irc_external'])) { |
||
126 | $this->config['irc_external'] = explode(',', $this->config['irc_external']); |
||
127 | } |
||
128 | |||
129 | foreach ($this->config['irc_external'] as $ext) { |
||
130 | if (($this->external[$ext] = file_get_contents('includes/ircbot/'.$ext.'.inc.php')) == '') { |
||
131 | unset($this->external[$ext]); |
||
132 | } |
||
133 | } |
||
134 | |||
135 | return $this->log('Cached '.sizeof($this->external).' commands.'); |
||
136 | }//end load_external() |
||
137 | |||
138 | |||
139 | private function init() |
||
140 | { |
||
141 | if ($this->config['irc_alert']) { |
||
142 | $this->connectAlert(); |
||
143 | } |
||
144 | |||
145 | $this->last_activity = time(); |
||
146 | |||
147 | $this->j = 2; |
||
148 | |||
149 | $this->connect(); |
||
150 | $this->log('Connected'); |
||
151 | if ($this->pass) { |
||
152 | fwrite($this->socket['irc'], 'PASS '.$this->pass."\n\r"); |
||
153 | } |
||
154 | |||
155 | $this->doAuth(); |
||
156 | while (true) { |
||
157 | foreach ($this->socket as $n => $socket) { |
||
158 | if (!is_resource($socket) || feof($socket)) { |
||
159 | $this->log("Socket '$n' closed. Restarting."); |
||
160 | break 2; |
||
161 | } |
||
162 | } |
||
163 | |||
164 | if (isset($this->tempnick)) { |
||
165 | $this->ircRaw('NICK '.$this->nick); |
||
166 | } |
||
167 | |||
168 | $this->getData(); |
||
169 | if ($this->config['irc_alert']) { |
||
170 | $this->alertData(); |
||
171 | } |
||
172 | |||
173 | if ($this->config['irc_conn_timeout']) { |
||
174 | $inactive_seconds = time() - $this->last_activity; |
||
175 | $max_inactive = $this->config['irc_conn_timeout']; |
||
176 | if ($inactive_seconds > $max_inactive) { |
||
177 | $this->log("No data from server since " . $max_inactive . " seconds. Restarting."); |
||
178 | break; |
||
179 | } |
||
180 | } |
||
181 | |||
182 | |||
183 | usleep($this->tick); |
||
184 | } |
||
185 | |||
186 | return $this->init(); |
||
187 | }//end init() |
||
188 | |||
189 | |||
190 | private function connectAlert() |
||
191 | { |
||
192 | $f = $this->config['install_dir'].'/.ircbot.alert'; |
||
193 | if (( file_exists($f) && filetype($f) != 'fifo' && !unlink($f) ) || ( !file_exists($f) && !shell_exec("mkfifo $f && echo 1") )) { |
||
194 | $this->log('Error - Cannot create Alert-File'); |
||
195 | return false; |
||
196 | } |
||
197 | |||
198 | if (($this->socket['alert'] = fopen($f, 'r+'))) { |
||
199 | $this->log('Opened Alert-File'); |
||
200 | stream_set_blocking($this->socket['alert'], false); |
||
201 | return true; |
||
202 | } |
||
203 | |||
204 | $this->log('Error - Cannot open Alert-File'); |
||
205 | return false; |
||
206 | }//end connect_alert() |
||
207 | |||
208 | |||
209 | private function read($buff) |
||
210 | { |
||
211 | $r = fread($this->socket[$buff], 64); |
||
212 | $this->buff[$buff] .= $r; |
||
213 | $r = strlen($r); |
||
214 | if (strstr($this->buff[$buff], "\n")) { |
||
215 | $tmp = explode("\n", $this->buff[$buff], 2); |
||
216 | $this->buff[$buff] = substr($this->buff[$buff], (strlen($tmp[0]) + 1)); |
||
217 | if ($this->debug) { |
||
218 | $this->log("Returning buffer '$buff': '".trim($tmp[0])."'"); |
||
219 | } |
||
220 | |||
221 | return $tmp[0]; |
||
222 | } |
||
223 | |||
224 | if ($this->debug && $r > 0) { |
||
225 | $this->log("Expanding buffer '$buff' = '".trim($this->buff[$buff])."'"); |
||
226 | } |
||
227 | |||
228 | return false; |
||
229 | }//end read() |
||
230 | |||
231 | |||
232 | private function alertData() |
||
233 | { |
||
234 | if (($alert = $this->read('alert')) !== false) { |
||
235 | $alert = json_decode($alert, true); |
||
236 | if (!is_array($alert)) { |
||
237 | return false; |
||
238 | } |
||
239 | |||
240 | switch ($alert['state']) : |
||
241 | case 3: |
||
242 | $severity_extended = '+'; |
||
243 | break; |
||
244 | case 4: |
||
245 | $severity_extended = '-'; |
||
246 | break; |
||
247 | default: |
||
248 | $severity_extended = ''; |
||
249 | endswitch; |
||
250 | |||
251 | $severity = str_replace(array('warning', 'critical'), array($this->_color('Warning', 'yellow'), $this->_color('Critical', 'red')), $alert['severity']).$severity_extended.' '; |
||
252 | if ($alert['state'] == 0 and $this->config['irc_alert_utf8']) { |
||
253 | $severity = str_replace(array('Warning', 'Critical'), array('̶W̶a̶r̶n̶i̶n̶g', '̶C̶r̶i̶t̶i̶c̶a̶l'), $severity); |
||
254 | } |
||
255 | |||
256 | if ($this->config['irc_alert_chan']) { |
||
257 | foreach ($this->config['irc_alert_chan'] as $chan) { |
||
258 | $this->ircRaw('PRIVMSG '.$chan.' :'.$severity.trim($alert['title'])); |
||
259 | if (!$this->config['irc_alert_short']) { // Only send the title if set to short |
||
260 | foreach (explode("\n", $alert['msg']) as $line) { |
||
261 | // We don't need to repeat the title |
||
262 | $line = strip_tags($line); |
||
263 | if (trim($line) != trim($alert['title'])) { |
||
264 | $this->ircRaw('PRIVMSG '.$chan.' :'.$line); |
||
265 | } |
||
266 | } |
||
267 | } |
||
268 | } |
||
269 | } else { |
||
270 | foreach ($this->authd as $nick => $data) { |
||
271 | if ($data['expire'] >= time()) { |
||
272 | $this->ircRaw('PRIVMSG '.$nick.' :'.$severity.trim($alert['title'])); |
||
273 | if (!$this->config['irc_alert_short']) { // Only send the title if set to short |
||
274 | foreach (explode("\n", $alert['msg']) as $line) { |
||
275 | // We don't need to repeat the title |
||
276 | $line = strip_tags($line); |
||
277 | if (trim($line) != trim($alert['title'])) { |
||
278 | $this->ircRaw('PRIVMSG '.$nick.' :'.$line); |
||
279 | } |
||
280 | } |
||
281 | } |
||
282 | } |
||
283 | } |
||
284 | } |
||
285 | }//end if |
||
286 | }//end alertData() |
||
287 | |||
288 | |||
289 | private function getData() |
||
290 | { |
||
291 | if (($data = $this->read('irc')) !== false) { |
||
292 | $this->last_activity = time(); |
||
293 | $this->data = $data; |
||
294 | $ex = explode(' ', $this->data); |
||
295 | if ($ex[0] == 'PING') { |
||
296 | return $this->ircRaw('PONG '.$ex[1]); |
||
297 | } |
||
298 | |||
299 | if ($ex[1] == 376 || $ex[1] == 422 || ($ex[1] == 'MODE' && $ex[2] == $this->nick)) { |
||
300 | if ($this->j == 2) { |
||
301 | $this->joinChan(); |
||
302 | $this->j = 0; |
||
303 | } |
||
304 | } |
||
305 | |||
306 | if (($this->config['irc_ctcp']) && (preg_match("/^:".chr(1).".*/", $ex[3]))) { |
||
307 | // Handle CTCP |
||
308 | $ctcp = trim(preg_replace("/[^A-Z]/", "", $ex[3])); |
||
309 | $ctcp_reply = null; |
||
310 | $this->log("Received irc CTCP: ".$ctcp." from ".$this->getUser($this->data)); |
||
311 | switch ($ctcp) { |
||
312 | case 'VERSION': |
||
313 | $ctcp_reply = chr(1)."$ctcp ".$this->config['irc_ctcp_version'].chr(1); |
||
314 | break; |
||
315 | case 'PING': |
||
316 | $ctcp_reply = chr(1)."$ctcp ".$ex[4]. " ".$ex[5].chr(1); |
||
317 | break; |
||
318 | case 'TIME': |
||
319 | $ctcp_reply = chr(1)."$ctcp ".date('c').chr(1); |
||
320 | break; |
||
321 | } |
||
322 | if ($ctcp_reply !== null) { |
||
323 | $this->log("Sending irc CTCP: ".'NOTICE '.$this->getUser($this->data)." :".$ctcp_reply); |
||
324 | return $this->ircRaw('NOTICE '.$this->getUser($this->data)." :".$ctcp_reply); |
||
325 | } |
||
326 | } |
||
327 | |||
328 | if (($ex[1] == 'NICK') && (preg_replace("/^:/", "", $ex[2]) == $this->nick)) { |
||
329 | // Nickname changed successfully |
||
330 | if ($this->debug) { |
||
331 | $this->log("Regained our real nick"); |
||
332 | } |
||
333 | unset($this->tempnick); |
||
334 | } |
||
335 | if (($ex[1] == 433) || ($ex[1] == 437)) { |
||
336 | // Nickname already in use / temp unavailable |
||
337 | if ($this->debug) { |
||
338 | $this->log("Nickname already in use..."); |
||
339 | } |
||
340 | if ($ex[2] != "*") { |
||
341 | $this->tempnick = $ex[2]; |
||
0 ignored issues
–
show
Bug
Best Practice
introduced
by
Loading history...
|
|||
342 | } |
||
343 | if (!isset($this->tempnick)) { |
||
344 | $this->tempnick = $this->nick.rand(0, 99); |
||
345 | } |
||
346 | if ($this->debug) { |
||
347 | $this->log("Using temp nick ".$this->tempnick); |
||
348 | } |
||
349 | return $this->ircRaw('NICK '.$this->tempnick); |
||
350 | } |
||
351 | |||
352 | $this->command = str_replace(array(chr(10), chr(13)), '', $ex[3]); |
||
353 | if (strstr($this->command, ':.')) { |
||
354 | $this->handleCommand(); |
||
355 | } |
||
356 | } |
||
357 | }//end getData() |
||
358 | |||
359 | |||
360 | private function joinChan($chan = false) |
||
361 | { |
||
362 | if ($chan) { |
||
363 | $this->chan[] = $chan; |
||
364 | } |
||
365 | |||
366 | foreach ($this->chan as $chan) { |
||
367 | $this->ircRaw('JOIN '.$chan); |
||
368 | } |
||
369 | |||
370 | return true; |
||
371 | }//end joinChan() |
||
372 | |||
373 | |||
374 | private function handleCommand() |
||
375 | { |
||
376 | $this->command = str_replace(':.', '', $this->command); |
||
377 | $tmp = explode(':.'.$this->command.' ', $this->data); |
||
378 | $this->user = $this->getAuthdUser(); |
||
379 | if (!$this->isAuthd() && (isset($this->config['irc_auth']))) { |
||
380 | $this->hostAuth(); |
||
381 | } |
||
382 | if ($this->isAuthd() || trim($this->command) == 'auth') { |
||
383 | $this->proceedCommand(str_replace("\n", '', trim($this->command)), trim($tmp[1])); |
||
384 | } |
||
385 | |||
386 | $this->authd[$this->getUser($this->data)] = $this->user; |
||
387 | return false; |
||
388 | }//end handleCommand() |
||
389 | |||
390 | |||
391 | private function proceedCommand($command, $params) |
||
392 | { |
||
393 | $command = strtolower($command); |
||
394 | if (in_array($command, $this->commands)) { |
||
395 | $this->chkdb(); |
||
396 | $this->log($command." ( '".$params."' )"); |
||
397 | return $this->{'_'.$command}($params); |
||
398 | } elseif ($this->external[$command]) { |
||
399 | $this->chkdb(); |
||
400 | $this->log($command." ( '".$params."' ) [Ext]"); |
||
401 | return eval($this->external[$command]); |
||
402 | } |
||
403 | |||
404 | return false; |
||
405 | }//end proceedCommand() |
||
406 | |||
407 | |||
408 | private function respond($msg) |
||
409 | { |
||
410 | $chan = $this->getChan($this->data); |
||
411 | return $this->sendMessage($msg, strstr($chan, '#') ? $chan : $this->getUser($this->data)); |
||
412 | }//end respond() |
||
413 | |||
414 | |||
415 | private function getChan($param) |
||
416 | { |
||
417 | $data = explode('PRIVMSG ', $this->data, 3); |
||
418 | $data = explode(' ', $data[1], 2); |
||
419 | return $data[0]; |
||
420 | }//end getChan() |
||
421 | |||
422 | |||
423 | private function getUser($param) |
||
424 | { |
||
425 | $arrData = explode('!', $param, 2); |
||
426 | return str_replace(':', '', $arrData[0]); |
||
427 | }//end getUser() |
||
428 | |||
429 | private function getUserHost($param) |
||
430 | { |
||
431 | $arrData = explode(' ', $param, 2); |
||
432 | return str_replace(':', '', $arrData[0]); |
||
433 | }//end getUserHost() |
||
434 | |||
435 | private function connect($try = 0) |
||
436 | { |
||
437 | if ($try > $this->max_retry) { |
||
438 | $this->log('Failed too many connection attempts, aborting'); |
||
439 | return die(); |
||
440 | } |
||
441 | |||
442 | $this->log('Trying to connect ('.($try + 1).') to '.$this->server.':'.$this->port.($this->ssl ? ' (SSL)' : '')); |
||
443 | if ($this->socket['irc']) { |
||
444 | $this->ircRaw('QUIT :Reloading'); |
||
445 | fclose($this->socket['irc']); |
||
446 | } |
||
447 | |||
448 | if ($this->ssl) { |
||
449 | $server = 'ssl://'.$this->server; |
||
450 | } else { |
||
451 | $server = $this->server; |
||
452 | } |
||
453 | |||
454 | if ($this->ssl && $this->config['irc_disable_ssl_check']) { |
||
455 | $ssl_context_params = array('ssl'=>array('allow_self_signed'=> true, 'verify_peer' => false, 'verify_peer_name' => false )); |
||
456 | $ssl_context = stream_context_create($ssl_context_params); |
||
457 | $this->socket['irc'] = stream_socket_client($server.':'.$this->port, $errno, $errstr, 30, STREAM_CLIENT_CONNECT, $ssl_context); |
||
458 | } else { |
||
459 | $this->socket['irc'] = fsockopen($server, $this->port); |
||
460 | } |
||
461 | |||
462 | if (!is_resource($this->socket['irc'])) { |
||
463 | if ($try < 5) { |
||
464 | sleep(5); |
||
465 | } elseif ($try < 10) { |
||
466 | sleep(60); |
||
467 | } else { |
||
468 | sleep(300); |
||
469 | } |
||
470 | return $this->connect($try + 1); |
||
471 | } else { |
||
472 | stream_set_blocking($this->socket['irc'], false); |
||
473 | return true; |
||
474 | } |
||
475 | }//end connect() |
||
476 | |||
477 | |||
478 | private function doAuth() |
||
479 | { |
||
480 | if ($this->ircRaw('USER '.$this->nick.' 0 '.$this->nick.' :'.$this->nick) && $this->ircRaw('NICK '.$this->nick)) { |
||
481 | return true; |
||
482 | } |
||
483 | |||
484 | return false; |
||
485 | }//end doAuth() |
||
486 | |||
487 | |||
488 | private function sendMessage($message, $chan) |
||
489 | { |
||
490 | if ($this->debug) { |
||
491 | $this->log("Sending 'PRIVMSG ".trim($chan).' :'.trim($message)."'"); |
||
492 | } |
||
493 | |||
494 | return $this->ircRaw('PRIVMSG '.trim($chan).' :'.trim($message)); |
||
495 | }//end sendMessage() |
||
496 | |||
497 | |||
498 | private function log($msg) |
||
499 | { |
||
500 | echo '['.date('r').'] '.trim($msg)."\n"; |
||
501 | return true; |
||
502 | }//end log() |
||
503 | |||
504 | |||
505 | private function chkdb() |
||
506 | { |
||
507 | if (!Eloquent::isConnected()) { |
||
508 | try { |
||
509 | Eloquent::boot(); |
||
510 | } catch (\PDOException $e) { |
||
511 | $this->log('Cannot connect to MySQL: ' . $e->getMessage()); |
||
512 | return die(); |
||
513 | } |
||
514 | } |
||
515 | return true; |
||
516 | }//end chkdb() |
||
517 | |||
518 | |||
519 | private function isAuthd() |
||
520 | { |
||
521 | if ($this->user['expire'] >= time()) { |
||
522 | $this->user['expire'] = (time() + ($this->config['irc_authtime'] * 3600)); |
||
523 | return true; |
||
524 | } else { |
||
525 | return false; |
||
526 | } |
||
527 | }//end isAuthd() |
||
528 | |||
529 | |||
530 | private function getAuthdUser() |
||
531 | { |
||
532 | return $this->authd[$this->getUser($this->data)]; |
||
533 | }//end getAuthUser() |
||
534 | |||
535 | private function hostAuth() |
||
536 | { |
||
537 | global $authorizer; |
||
538 | foreach ($this->config['irc_auth'] as $nms_user => $hosts) { |
||
539 | foreach ($hosts as $host) { |
||
540 | $host = preg_replace("/\*/", ".*", $host); |
||
541 | if (preg_match("/$host/", $this->getUserHost($this->data))) { |
||
542 | $user_id = LegacyAuth::get()->getUserid(mres($nms_user)); |
||
543 | $user = LegacyAuth::get()->getUser($user_id); |
||
544 | $this->user['name'] = $user['username']; |
||
0 ignored issues
–
show
|
|||
545 | $this->user['id'] = $user_id; |
||
546 | $this->user['level'] = LegacyAuth::get()->getUserlevel($user['username']); |
||
547 | $this->user['expire'] = (time() + ($this->config['irc_authtime'] * 3600)); |
||
548 | if ($this->user['level'] < 5) { |
||
549 | foreach (dbFetchRows('SELECT device_id FROM devices_perms WHERE user_id = ?', array($this->user['id'])) as $tmp) { |
||
550 | $this->user['devices'][] = $tmp['device_id']; |
||
551 | } |
||
552 | |||
553 | foreach (dbFetchRows('SELECT port_id FROM ports_perms WHERE user_id = ?', array($this->user['id'])) as $tmp) { |
||
554 | $this->user['ports'][] = $tmp['port_id']; |
||
555 | } |
||
556 | } |
||
557 | if ($this->debug) { |
||
558 | $this->log("HostAuth on irc for '".$user['username']."', ID: '".$user_id."', Host: '".$host); |
||
559 | } |
||
560 | return true; |
||
561 | } |
||
562 | } |
||
563 | } |
||
564 | return false; |
||
565 | }//end hostAuth |
||
566 | |||
567 | |||
568 | private function ircRaw($params) |
||
569 | { |
||
570 | return fputs($this->socket['irc'], $params."\r\n"); |
||
571 | }//end irc_raw() |
||
572 | |||
573 | |||
574 | private function _auth($params) |
||
575 | { |
||
576 | global $authorizer; |
||
577 | $params = explode(' ', $params, 2); |
||
578 | if (strlen($params[0]) == 64) { |
||
579 | if ($this->tokens[$this->getUser($this->data)] == $params[0]) { |
||
580 | $this->user['expire'] = (time() + ($this->config['irc_authtime'] * 3600)); |
||
581 | $tmp_user = LegacyAuth::get()->getUser($this->user['id']); |
||
582 | $tmp = LegacyAuth::get()->getUserlevel($tmp_user['username']); |
||
583 | $this->user['level'] = $tmp; |
||
584 | if ($this->user['level'] < 5) { |
||
585 | foreach (dbFetchRows('SELECT device_id FROM devices_perms WHERE user_id = ?', array($this->user['id'])) as $tmp) { |
||
586 | $this->user['devices'][] = $tmp['device_id']; |
||
587 | } |
||
588 | |||
589 | foreach (dbFetchRows('SELECT port_id FROM ports_perms WHERE user_id = ?', array($this->user['id'])) as $tmp) { |
||
590 | $this->user['ports'][] = $tmp['port_id']; |
||
591 | } |
||
592 | } |
||
593 | |||
594 | return $this->respond('Authenticated.'); |
||
595 | } else { |
||
596 | return $this->respond('Nope.'); |
||
597 | } |
||
598 | } else { |
||
599 | $user_id = LegacyAuth::get()->getUserid(mres($params[0])); |
||
600 | $user = LegacyAuth::get()->getUser($user_id); |
||
601 | if ($user['email'] && $user['username'] == $params[0]) { |
||
602 | $token = hash('gost', openssl_random_pseudo_bytes(1024)); |
||
603 | $this->tokens[$this->getUser($this->data)] = $token; |
||
604 | $this->user['name'] = $params[0]; |
||
605 | $this->user['id'] = $user['user_id']; |
||
606 | if ($this->debug) { |
||
607 | $this->log("Auth for '".$params[0]."', ID: '".$user['user_id']."', Token: '".$token."', Mail: '".$user['email']."'"); |
||
608 | } |
||
609 | |||
610 | if (send_mail($user['email'], 'LibreNMS IRC-Bot Authtoken', "Your Authtoken for the IRC-Bot:\r\n\r\n".$token."\r\n\r\n") === true) { |
||
611 | return $this->respond('Token sent!'); |
||
612 | } else { |
||
613 | return $this->respond('Sorry, seems like mail doesnt like us.'); |
||
614 | } |
||
615 | } else { |
||
616 | return $this->respond('Who are you again?'); |
||
617 | } |
||
618 | }//end if |
||
619 | return false; |
||
620 | }//end _auth() |
||
621 | |||
622 | |||
623 | private function _reload() |
||
624 | { |
||
625 | if ($this->user['level'] == 10) { |
||
626 | global $config; |
||
627 | $config = array(); |
||
628 | $config['install_dir'] = $this->config['install_dir']; |
||
629 | chdir($config['install_dir']); |
||
630 | include 'includes/defaults.inc.php'; |
||
631 | include 'config.php'; |
||
632 | include 'includes/definitions.inc.php'; |
||
633 | $this->respond('Reloading configuration & defaults'); |
||
634 | if ($config != $this->config) { |
||
635 | return $this->__construct(); |
||
636 | } |
||
637 | } else { |
||
638 | return $this->respond('Permission denied.'); |
||
639 | } |
||
640 | }//end _reload() |
||
641 | |||
642 | |||
643 | private function _join($params) |
||
644 | { |
||
645 | if ($this->user['level'] == 10) { |
||
646 | return $this->joinChan($params); |
||
647 | } else { |
||
648 | return $this->respond('Permission denied.'); |
||
649 | } |
||
650 | }//end _join() |
||
651 | |||
652 | |||
653 | private function _quit($params) |
||
654 | { |
||
655 | if ($this->user['level'] == 10) { |
||
656 | $this->ircRaw("QUIT :Requested"); |
||
657 | return die(); |
||
658 | } else { |
||
659 | return $this->respond('Permission denied.'); |
||
660 | } |
||
661 | }//end _quit() |
||
662 | |||
663 | |||
664 | private function _help($params) |
||
665 | { |
||
666 | foreach ($this->commands as $cmd) { |
||
667 | $msg .= ', '.$cmd; |
||
668 | } |
||
669 | |||
670 | $msg = substr($msg, 2); |
||
671 | return $this->respond("Available commands: $msg"); |
||
672 | }//end _help() |
||
673 | |||
674 | |||
675 | private function _version($params) |
||
676 | { |
||
677 | $versions = version_info(); |
||
678 | $schema_version = $versions['db_schema']; |
||
679 | $version = substr($versions['local_sha'], 0, 7); |
||
680 | |||
681 | $msg = $this->config['project_name_version'].', Version: '.$version.', DB schema: '.$schema_version.', PHP: '.PHP_VERSION; |
||
682 | return $this->respond($msg); |
||
683 | }//end _version() |
||
684 | |||
685 | |||
686 | private function _log($params) |
||
687 | { |
||
688 | $num = 1; |
||
689 | if ($params > 1) { |
||
690 | $num = $params; |
||
691 | } |
||
692 | |||
693 | if ($this->user['level'] < 5) { |
||
694 | $tmp = dbFetchRows('SELECT `event_id`,`device_id`,`datetime`,`message`,`type` FROM `eventlog` WHERE `device_id` IN ('.implode(',', $this->user['devices']).') ORDER BY `event_id` DESC LIMIT '. (int)$num); |
||
695 | } else { |
||
696 | $tmp = dbFetchRows('SELECT `event_id`,`device_id`,`datetime`,`message`,`type` FROM `eventlog` ORDER BY `event_id` DESC LIMIT '.(int)$num); |
||
697 | } |
||
698 | |||
699 | foreach ($tmp as $device) { |
||
700 | $hostid = dbFetchRow('SELECT `hostname` FROM `devices` WHERE `device_id` = '.$device['device_id']); |
||
701 | $this->respond($device['event_id'].' '.$hostid['hostname'].' '.$device['datetime'].' '.$device['message'].' '.$device['type']); |
||
702 | } |
||
703 | |||
704 | if (!$hostid) { |
||
705 | $this->respond('Nothing to see, maybe a bug?'); |
||
706 | } |
||
707 | |||
708 | return true; |
||
709 | }//end _log() |
||
710 | |||
711 | |||
712 | private function _down($params) |
||
713 | { |
||
714 | if ($this->user['level'] < 5) { |
||
715 | $tmp = dbFetchRows('SELECT `hostname` FROM `devices` WHERE status=0 AND `device_id` IN ('.implode(',', $this->user['devices']).')'); |
||
716 | } else { |
||
717 | $tmp = dbFetchRows('SELECT `hostname` FROM `devices` WHERE status=0'); |
||
718 | } |
||
719 | |||
720 | foreach ($tmp as $db) { |
||
721 | if ($db['hostname']) { |
||
722 | $msg .= ', '.$db['hostname']; |
||
723 | } |
||
724 | } |
||
725 | |||
726 | $msg = substr($msg, 2); |
||
727 | $msg = $msg ? $msg : 'Nothing to show :)'; |
||
728 | return $this->respond($msg); |
||
729 | }//end _down() |
||
730 | |||
731 | |||
732 | private function _device($params) |
||
733 | { |
||
734 | $params = explode(' ', $params); |
||
735 | $hostname = $params[0]; |
||
736 | $device = dbFetchRow('SELECT * FROM `devices` WHERE `hostname` = ?', array($hostname)); |
||
737 | if (!$device) { |
||
738 | return $this->respond('Error: Bad or Missing hostname, use .listdevices to show all devices.'); |
||
739 | } |
||
740 | |||
741 | if ($this->user['level'] < 5 && !in_array($device['device_id'], $this->user['devices'])) { |
||
742 | return $this->respond('Error: Permission denied.'); |
||
743 | } |
||
744 | |||
745 | $status = $device['status'] ? 'Up '.formatUptime($device['uptime']) : 'Down'; |
||
746 | $status .= $device['ignore'] ? '*Ignored*' : ''; |
||
747 | $status .= $device['disabled'] ? '*Disabled*' : ''; |
||
748 | return $this->respond($device['os'].' '.$device['version'].' '.$device['features'].' '.$status); |
||
749 | }//end _device() |
||
750 | |||
751 | |||
752 | private function _port($params) |
||
753 | { |
||
754 | $params = explode(' ', $params); |
||
755 | $hostname = $params[0]; |
||
756 | $ifname = $params[1]; |
||
757 | if (!$hostname || !$ifname) { |
||
758 | return $this->respond('Error: Missing hostname or ifname.'); |
||
759 | } |
||
760 | |||
761 | $device = dbFetchRow('SELECT * FROM `devices` WHERE `hostname` = ?', array($hostname)); |
||
762 | $port = dbFetchRow('SELECT * FROM `ports` WHERE (`ifName` = ? OR `ifDescr` = ?) AND device_id = ?', array($ifname, $ifname, $device['device_id'])); |
||
763 | if ($this->user['level'] < 5 && !in_array($port['port_id'], $this->user['ports']) && !in_array($device['device_id'], $this->user['devices'])) { |
||
764 | return $this->respond('Error: Permission denied.'); |
||
765 | } |
||
766 | |||
767 | $bps_in = formatRates($port['ifInOctets_rate'] * 8); |
||
768 | $bps_out = formatRates($port['ifOutOctets_rate'] * 8); |
||
769 | $pps_in = format_bi($port['ifInUcastPkts_rate']); |
||
770 | $pps_out = format_bi($port['ifOutUcastPkts_rate']); |
||
771 | return $this->respond($port['ifAdminStatus'].'/'.$port['ifOperStatus'].' '.$bps_in.' > bps > '.$bps_out.' | '.$pps_in.'pps > PPS > '.$pps_out.'pps'); |
||
772 | }//end _port() |
||
773 | |||
774 | |||
775 | private function _listdevices($params) |
||
776 | { |
||
777 | if ($this->user['level'] < 5) { |
||
778 | $tmp = dbFetchRows('SELECT `hostname` FROM `devices` WHERE `device_id` IN ('.implode(',', $this->user['devices']).')'); |
||
779 | } else { |
||
780 | $tmp = dbFetchRows('SELECT `hostname` FROM `devices`'); |
||
781 | } |
||
782 | |||
783 | foreach ($tmp as $device) { |
||
784 | $msg .= ', '.$device['hostname']; |
||
785 | } |
||
786 | |||
787 | $msg = substr($msg, 2); |
||
788 | $msg = $msg ? $msg : 'Nothing to show..?'; |
||
789 | return $this->respond($msg); |
||
790 | }//end _listdevices() |
||
791 | |||
792 | |||
793 | private function _status($params) |
||
794 | { |
||
795 | $params = explode(' ', $params); |
||
796 | $statustype = $params[0]; |
||
797 | if ($this->user['level'] < 5) { |
||
798 | $d_w = ' WHERE device_id IN ('.implode(',', $this->user['devices']).')'; |
||
799 | $d_a = ' AND device_id IN ('.implode(',', $this->user['devices']).')'; |
||
800 | $p_w = ' WHERE port_id IN ('.implode(',', $this->user['ports']).') OR device_id IN ('.implode(',', $this->user['devices']).')'; |
||
801 | $p_a = ' AND (I.port_id IN ('.implode(',', $this->user['ports']).') OR I.device_id IN ('.implode(',', $this->user['devices']).'))'; |
||
802 | } |
||
803 | |||
804 | switch ($statustype) { |
||
805 | case 'devices': |
||
806 | case 'device': |
||
807 | case 'dev': |
||
808 | $devcount = dbFetchCell('SELECT count(*) FROM devices'.$d_w); |
||
809 | $devup = dbFetchCell("SELECT count(*) FROM devices WHERE status = '1' AND `ignore` = '0'".$d_a); |
||
810 | $devdown = dbFetchCell("SELECT count(*) FROM devices WHERE status = '0' AND `ignore` = '0'".$d_a); |
||
811 | $devign = dbFetchCell("SELECT count(*) FROM devices WHERE `ignore` = '1'".$d_a); |
||
812 | $devdis = dbFetchCell("SELECT count(*) FROM devices WHERE `disabled` = '1'".$d_a); |
||
813 | if ($devup > 0) { |
||
814 | $devup = $this->_color($devup, 'green'); |
||
815 | } |
||
816 | if ($devdown > 0) { |
||
817 | $devdown = $this->_color($devdown, 'red'); |
||
818 | $devcount = $this->_color($devcount, 'yellow', null, 'bold'); |
||
819 | } else { |
||
820 | $devcount = $this->_color($devcount, 'green', null, 'bold'); |
||
821 | } |
||
822 | $msg = 'Devices: '.$devcount.' ('.$devup.' up, '.$devdown.' down, '.$devign.' ignored, '.$devdis.' disabled'.')'; |
||
823 | break; |
||
824 | |||
825 | case 'ports': |
||
826 | case 'port': |
||
827 | case 'prt': |
||
828 | $prtcount = dbFetchCell('SELECT count(*) FROM ports'.$p_w); |
||
829 | $prtup = dbFetchCell("SELECT count(*) FROM ports AS I, devices AS D WHERE I.ifOperStatus = 'up' AND I.ignore = '0' AND I.device_id = D.device_id AND D.ignore = '0'".$p_a); |
||
830 | $prtdown = dbFetchCell("SELECT count(*) FROM ports AS I, devices AS D WHERE I.ifOperStatus = 'down' AND I.ifAdminStatus = 'up' AND I.ignore = '0' AND D.device_id = I.device_id AND D.ignore = '0'".$p_a); |
||
831 | $prtsht = dbFetchCell("SELECT count(*) FROM ports AS I, devices AS D WHERE I.ifAdminStatus = 'down' AND I.ignore = '0' AND D.device_id = I.device_id AND D.ignore = '0'".$p_a); |
||
832 | $prtign = dbFetchCell("SELECT count(*) FROM ports AS I, devices AS D WHERE D.device_id = I.device_id AND (I.ignore = '1' OR D.ignore = '1')".$p_a); |
||
833 | // $prterr = dbFetchCell("SELECT count(*) FROM ports AS I, devices AS D WHERE D.device_id = I.device_id AND (I.ignore = '0' OR D.ignore = '0') AND (I.ifInErrors_delta > '0' OR I.ifOutErrors_delta > '0')".$p_a); |
||
834 | if ($prtup > 0) { |
||
835 | $prtup = $this->_color($prtup, 'green'); |
||
836 | } |
||
837 | if ($prtdown > 0) { |
||
838 | $prtdown = $this->_color($prtdown, 'red'); |
||
839 | $prtcount = $this->_color($prtcount, 'yellow', null, 'bold'); |
||
840 | } else { |
||
841 | $prtcount = $this->_color($prtcount, 'green', null, 'bold'); |
||
842 | } |
||
843 | $msg = 'Ports: '.$prtcount.' ('.$prtup.' up, '.$prtdown.' down, '.$prtign.' ignored, '.$prtsht.' shutdown'.')'; |
||
844 | break; |
||
845 | |||
846 | case 'services': |
||
847 | case 'service': |
||
848 | case 'srv': |
||
849 | $status_counts = array(); |
||
850 | $status_colors = array(0 => 'green', 3 => 'lightblue', 1 => 'yellow', 2 => 'red'); |
||
851 | $srvcount = dbFetchCell('SELECT COUNT(*) FROM services'.$d_w); |
||
852 | $srvign = dbFetchCell("SELECT COUNT(*) FROM services WHERE service_ignore = 1".$d_a); |
||
853 | $srvdis = dbFetchCell("SELECT COUNT(*) FROM services WHERE service_disabled = 1".$d_a); |
||
854 | $service_status = dbFetchRows("SELECT `service_status`, COUNT(*) AS `count` FROM `services` WHERE `service_disabled`=0 AND `service_ignore`=0 $d_a GROUP BY `service_status`"); |
||
855 | $service_status = array_column($service_status, 'count', 'service_status'); // key by status |
||
856 | |||
857 | foreach ($status_colors as $status => $color) { |
||
858 | if (isset($service_status[$status])) { |
||
859 | $status_counts[$status] = $this->_color($service_status[$status], $color); |
||
860 | $srvcount = $this->_color($srvcount, $color, null, 'bold'); // upgrade the main count color |
||
861 | } else { |
||
862 | $status_counts[$status] = 0; |
||
863 | } |
||
864 | } |
||
865 | |||
866 | $msg = "Services: $srvcount ({$status_counts[0]} up, {$status_counts[2]} down, {$status_counts[1]} warning, {$status_counts[3]} unknown, $srvign ignored, $srvdis disabled)"; |
||
867 | break; |
||
868 | |||
869 | default: |
||
870 | $msg = 'Error: STATUS requires one of the following: <devices|device|dev>|<ports|port|prt>|<services|service|src>'; |
||
871 | break; |
||
872 | }//end switch |
||
873 | |||
874 | return $this->respond($msg); |
||
875 | }//end _status() |
||
876 | |||
877 | private function _color($text, $fg_color, $bg_color = null, $other = null) |
||
878 | { |
||
879 | $colors = array( |
||
880 | 'white' => "00", |
||
881 | 'black' => "01", |
||
882 | 'blue' => "02", |
||
883 | 'green' => "03", |
||
884 | 'red' => "04", |
||
885 | 'brown' => "05", |
||
886 | 'purple' => "06", |
||
887 | 'orange' => "07", |
||
888 | 'yellow' => "08", |
||
889 | 'lightgreen' => "09", |
||
890 | 'cyan' => "10", |
||
891 | 'lightcyan' => "11", |
||
892 | 'lightblue' => "12", |
||
893 | 'pink' => "13", |
||
894 | 'grey' => "14", |
||
895 | 'lightgrey' => "15", |
||
896 | ); |
||
897 | $ret = chr(3); |
||
898 | if (array_key_exists($fg_color, $colors)) { |
||
899 | $ret .= $colors[$fg_color]; |
||
900 | if (array_key_exists($bg_color, $colors)) { |
||
901 | $ret .= ",".$colors[$fg_color]; |
||
902 | } |
||
903 | } |
||
904 | switch ($other) { |
||
905 | case 'bold': |
||
906 | $ret .= chr(2); |
||
907 | break; |
||
908 | case 'underline': |
||
909 | $ret .= chr(31); |
||
910 | break; |
||
911 | case 'italics': |
||
912 | case 'reverse': |
||
913 | $ret .= chr(22); |
||
914 | break; |
||
915 | } |
||
916 | $ret .= $text; |
||
917 | $ret .= chr(15); |
||
918 | return $ret; |
||
919 | }// end _color |
||
920 | }//end class |
||
921 |