1
|
<?php
|
2
|
/****h* pfSense/notices
|
3
|
* NAME
|
4
|
* notices.inc - pfSense notice utilities
|
5
|
* DESCRIPTION
|
6
|
* This include contains the pfSense notice facilities.
|
7
|
* HISTORY
|
8
|
* $Id$
|
9
|
******
|
10
|
*
|
11
|
* Copyright (C) 2005 Colin Smith (ethethlay@gmail.com)
|
12
|
* All rights reserved.
|
13
|
* Redistribution and use in source and binary forms, with or without
|
14
|
* modification, are permitted provided that the following conditions are met:
|
15
|
*
|
16
|
* 1. Redistributions of source code must retain the above copyright notice,
|
17
|
* this list of conditions and the following disclaimer.
|
18
|
*
|
19
|
* 2. Redistributions in binary form must reproduce the above copyright
|
20
|
* notice, this list of conditions and the following disclaimer in the
|
21
|
* documentation and/or other materials provided with the distribution.
|
22
|
*
|
23
|
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
|
24
|
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
|
25
|
* AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
26
|
* AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
|
27
|
* OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
28
|
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
29
|
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
30
|
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
31
|
* RISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
32
|
* POSSIBILITY OF SUCH DAMAGE.
|
33
|
*
|
34
|
*/
|
35
|
|
36
|
require_once("globals.inc");
|
37
|
require_once("led.inc");
|
38
|
|
39
|
$notice_path = $g['tmp_path'] . '/notices';
|
40
|
|
41
|
/*
|
42
|
* $category - Category that this notice should be displayed under. This can be arbitrary,
|
43
|
* but a page must be set to receive this messages for it to be displayed.
|
44
|
*
|
45
|
* $priority - A notice's priority. Higher numbers indicate greater severity.
|
46
|
* 0 = informational, 1 = warning, 2 = error, etc. This may also be arbitrary,
|
47
|
*/
|
48
|
function file_notice($id, $notice, $category = "General", $url = "", $priority = 1) {
|
49
|
global $notice_path;
|
50
|
if(!$queue = get_notices()) $queue = array();
|
51
|
$queuekey = time();
|
52
|
$toqueue = array(
|
53
|
'id' => $id,
|
54
|
'notice' => $notice,
|
55
|
'url' => $url,
|
56
|
'category' => $category,
|
57
|
'priority' => $priority,
|
58
|
);
|
59
|
$queue[$queuekey] = $toqueue;
|
60
|
$queueout = fopen($notice_path, "w");
|
61
|
if(!$queueout) {
|
62
|
log_error("Could not open {$notice_path} for writing");
|
63
|
return;
|
64
|
}
|
65
|
fwrite($queueout, serialize($queue));
|
66
|
fclose($queueout);
|
67
|
log_error("New alert found: {$notice}");
|
68
|
/* soekris */
|
69
|
if(file_exists("/dev/led/error"))
|
70
|
exec("/bin/echo 1 > /dev/led/error");
|
71
|
/* wrap & alix */
|
72
|
led_normalize();
|
73
|
led_morse(1, 'sos');
|
74
|
notify_via_growl($notice);
|
75
|
notify_via_smtp($notice);
|
76
|
return $queuekey;
|
77
|
}
|
78
|
|
79
|
function get_notices($category = "all") {
|
80
|
if(file_exists('/tmp/notices')) {
|
81
|
$queue = unserialize(file_get_contents('/tmp/notices'));
|
82
|
if(!$queue) return false;
|
83
|
if($category != 'all') {
|
84
|
foreach($queue as $time => $notice) {
|
85
|
if(strtolower($notice['category']) == strtolower($category))
|
86
|
$toreturn[$time] = $notice;
|
87
|
}
|
88
|
return $toreturn;
|
89
|
} else {
|
90
|
return $queue;
|
91
|
}
|
92
|
} else {
|
93
|
return false;
|
94
|
}
|
95
|
}
|
96
|
|
97
|
function close_notice($id) {
|
98
|
global $notice_path;
|
99
|
require_once("util.inc");
|
100
|
/* soekris */
|
101
|
if(file_exists("/dev/led/error"))
|
102
|
exec("/bin/echo 0 > /dev/led/error");
|
103
|
/* wrap & alix */
|
104
|
led_normalize();
|
105
|
$ids = array();
|
106
|
if(!$notices = get_notices()) return;
|
107
|
if($id == "all") {
|
108
|
unlink_if_exists($notice_path);
|
109
|
return;
|
110
|
}
|
111
|
foreach(array_keys($notices) as $time) {
|
112
|
if($id == $time) {
|
113
|
unset($notices[$id]);
|
114
|
break;
|
115
|
}
|
116
|
}
|
117
|
foreach($notices as $key => $notice) {
|
118
|
$ids[$key] = $notice['id'];
|
119
|
}
|
120
|
foreach($ids as $time => $tocheck) {
|
121
|
if($id == $tocheck) {
|
122
|
unset($notices[$time]);
|
123
|
break;
|
124
|
}
|
125
|
}
|
126
|
if(count($notices) != 0) {
|
127
|
$queueout = fopen($notice_path, "w");
|
128
|
fwrite($queueout, serialize($notices));
|
129
|
fclose($queueout);
|
130
|
} else {
|
131
|
unlink_if_exists($notice_path);
|
132
|
}
|
133
|
|
134
|
return;
|
135
|
}
|
136
|
|
137
|
function dump_xml_notices() {
|
138
|
require_once("xmlparse.inc");
|
139
|
global $notice_path, $listtags;
|
140
|
$listtags[] = 'notice';
|
141
|
if(!$notices = get_notices()) return;
|
142
|
foreach($notices as $time => $notice) {
|
143
|
$notice['time'] = $time;
|
144
|
$toput['notice'][] = $notice;
|
145
|
}
|
146
|
$xml = dump_xml_config($toput, 'notices');
|
147
|
return $xml;
|
148
|
}
|
149
|
|
150
|
function print_notices($notices, $category = "all") {
|
151
|
foreach($notices as $notice) {
|
152
|
if($category != "all") {
|
153
|
if(in_array($notice['category'], $category)) $categories[] = $notice['category'];
|
154
|
} else {
|
155
|
$categories[] = $notice['category'];
|
156
|
}
|
157
|
}
|
158
|
$categories = array_unique($categories);
|
159
|
sort($categories);
|
160
|
foreach($categories as $category) {
|
161
|
$toreturn .= "<ul><li>{$category}<ul>";
|
162
|
foreach($notices as $notice) {
|
163
|
if(strtolower($notice['category']) == strtolower($category)) {
|
164
|
if($notice['id'] != "") {
|
165
|
if($notice['url'] != "") {
|
166
|
$toreturn .= "<li><a href={$notice['url']}>{$notice['id']}</a> - {$notice['notice']}</li>";
|
167
|
} else {
|
168
|
$toreturn .= "<li>{$notice['id']} - {$notice['notice']}</li>";
|
169
|
}
|
170
|
}
|
171
|
}
|
172
|
}
|
173
|
$toreturn .= "</ul></li></ul>";
|
174
|
}
|
175
|
return $toreturn;
|
176
|
}
|
177
|
|
178
|
function print_notice_box($category = "all") {
|
179
|
$notices = get_notices();
|
180
|
if(!$notices) return;
|
181
|
print_info_box_np(print_notices($notices, $category));
|
182
|
return;
|
183
|
}
|
184
|
|
185
|
|
186
|
function are_notices_pending($category = "all") {
|
187
|
global $notice_path;
|
188
|
if(file_exists($notice_path)) {
|
189
|
return true;
|
190
|
}
|
191
|
return false;
|
192
|
}
|
193
|
|
194
|
/****f* pfsense-utils/notify_via_smtp
|
195
|
* NAME
|
196
|
* notify_via_smtp
|
197
|
* INPUTS
|
198
|
* notification string to send as an email
|
199
|
* RESULT
|
200
|
* returns true if message was sent
|
201
|
******/
|
202
|
function notify_via_smtp($message) {
|
203
|
global $config;
|
204
|
|
205
|
if(!$config['notifications']['smtp']['ipaddress'])
|
206
|
return;
|
207
|
|
208
|
if(!$config['notifications']['smtp']['notifyemailaddress'])
|
209
|
return;
|
210
|
|
211
|
/* Do NOT send the same message twice */
|
212
|
if(file_exists("/var/db/notices_lastmsg.txt")) {
|
213
|
$lastmsg = trim(file_get_contents("/var/db/notices_lastmsg.txt"));
|
214
|
if($lastmsg == $message)
|
215
|
return;
|
216
|
}
|
217
|
|
218
|
require_once("smtp.inc");
|
219
|
|
220
|
$smtp = new smtp_class;
|
221
|
|
222
|
$from = "pfsense@{$config['system']['hostname']}.{$config['system']['domain']}";
|
223
|
$to = $config['notifications']['smtp']['notifyemailaddress'];
|
224
|
|
225
|
$smtp->host_name = $config['notifications']['smtp']['ipaddress'];
|
226
|
$smtp->host_port = 25;
|
227
|
|
228
|
$smtp->direct_delivery = 0;
|
229
|
$smtp->ssl = 0;
|
230
|
$smtp->debug = 1;
|
231
|
$smtp->html_debug = 0;
|
232
|
|
233
|
$headers = array(
|
234
|
"From: {$from}",
|
235
|
"To: {$to}",
|
236
|
"Subject: {$config['system']['hostname']}.{$config['system']['domain']} - Notification",
|
237
|
"Date: ".strftime("%a, %d %b %Y %H:%M:%S %Z")
|
238
|
);
|
239
|
|
240
|
/* Store last message sent to avoid spamming */
|
241
|
$fd = fopen("/var/db/notices_lastmsg.txt", "w");
|
242
|
fwrite($fd, $message);
|
243
|
fclose($fd);
|
244
|
|
245
|
if($smtp->SendMessage($from, array($to), $headers, $message))
|
246
|
log_error("Message sent to {$to} OK");
|
247
|
else
|
248
|
log_error("Could not send the message to {$to} -- Error: {$smtp->error}");
|
249
|
|
250
|
}
|
251
|
|
252
|
|
253
|
/****f* pfsense-utils/notify_via_growl
|
254
|
* NAME
|
255
|
* notify_via_growl
|
256
|
* INPUTS
|
257
|
* notification string to send
|
258
|
* RESULT
|
259
|
* returns true if message was sent
|
260
|
******/
|
261
|
function notify_via_growl($message) {
|
262
|
require_once("growl.class");
|
263
|
global $config;
|
264
|
$growl_ip = $config['notifications']['growl']['ipaddress'];
|
265
|
$growl_password = $config['notifications']['growl']['password'];
|
266
|
if($growl_ip) {
|
267
|
$growl = new Growl($growl_ip, $growl_password);
|
268
|
$growl->notify("pfSense growl alert", "pfSense", "{$message}");
|
269
|
}
|
270
|
}
|
271
|
|
272
|
/****f* pfsense-utils/register_via_growl
|
273
|
* NAME
|
274
|
* register_via_growl
|
275
|
* INPUTS
|
276
|
* none
|
277
|
* RESULT
|
278
|
* none
|
279
|
******/
|
280
|
function register_via_growl() {
|
281
|
require_once("growl.class");
|
282
|
global $config;
|
283
|
$growl_ip = $config['notifications']['growl']['ipaddress'];
|
284
|
$growl_password = $config['notifications']['growl']['password'];
|
285
|
if($growl_ip) {
|
286
|
$growl = new Growl($growl_ip, $growl_password);
|
287
|
$growl->register();
|
288
|
}
|
289
|
}
|
290
|
|
291
|
?>
|