Project

General

Profile

Download (12.2 KB) Statistics
| Branch: | Tag: | Revision:
1 9774f564 Colin Smith
<?php
2
/****h* pfSense/notices
3 b37a2e8c Phil Davis
	NAME
4
		notices.inc - pfSense notice utilities
5
	DESCRIPTION
6
		This include contains the pfSense notice facilities.
7
	HISTORY
8
		$Id$
9
10
	Copyright (C) 2009 Scott Ullrich (sullrich@gmail.com)
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 9774f564 Colin Smith
 */
34
35 523855b0 Scott Ullrich
/*
36
	pfSense_BUILDER_BINARIES:	/bin/echo
37
	pfSense_MODULE:	notifications
38
*/
39
40 9774f564 Colin Smith
require_once("globals.inc");
41 0c884086 jim-p
require_once("led.inc");
42 9774f564 Colin Smith
43
$notice_path = $g['tmp_path'] . '/notices';
44 c4249322 Phil Davis
$smtp_authentication_mechanisms = array(
45
	'PLAIN' => 'PLAIN',
46
	'LOGIN' => 'LOGIN');
47
/* Other SMTP Authentication Mechanisms that could be supported.
48
 * Note that MD5 is no longer considered secure.
49
 *	'GSSAPI' => 'GSSAPI ' . gettext("Generic Security Services Application Program Interface")
50
 *	'DIGEST-MD5' => 'DIGEST-MD5 ' . gettext("Digest access authentication")
51
 *	'MD5' => 'MD5'
52
 *	'CRAM-MD5' => 'CRAM-MD5'
53
*/
54 9774f564 Colin Smith
55 91cae49d Scott Ullrich
/****f* notices/file_notice
56
 * NAME
57
 *   file_notice
58
 * INPUTS
59
 *	 $id, $notice, $category, $url, $priority
60
 * RESULT
61
 *   Files a notice and kicks off the various alerts, smtp, growl, system log, LED's, etc.
62
 ******/
63 c6e2b163 Colin Smith
function file_notice($id, $notice, $category = "General", $url = "", $priority = 1) {
64 91cae49d Scott Ullrich
	/*
65
	 * $category - Category that this notice should be displayed under. This can be arbitrary,
66
	 * 	       but a page must be set to receive this messages for it to be displayed.
67
	 *
68
	 * $priority - A notice's priority. Higher numbers indicate greater severity.
69
	 *	       0 = informational, 1 = warning, 2 = error, etc. This may also be arbitrary,
70
	 */
71 9774f564 Colin Smith
	global $notice_path;
72 b37a2e8c Phil Davis
	if (!$queue = get_notices()) {
73
		$queue = array();
74
	}
75 9774f564 Colin Smith
	$queuekey = time();
76
	$toqueue = array(
77
				'id'		=> $id,
78
				'notice'	=> $notice,
79
				'url'		=> $url,
80
				'category'	=> $category,
81
				'priority'	=> $priority,
82
			);
83 80b53805 Phil Davis
	while (isset($queue[$queuekey])) {
84
		$queuekey++;
85
	}
86 9774f564 Colin Smith
	$queue[$queuekey] = $toqueue;
87
	$queueout = fopen($notice_path, "w");
88 b37a2e8c Phil Davis
	if (!$queueout) {
89 5025dfe3 Carlos Eduardo Ramos
		log_error(printf(gettext("Could not open %s for writing"), $notice_path));
90 34e1ff97 Scott Ullrich
		return;
91
	}
92 9774f564 Colin Smith
	fwrite($queueout, serialize($queue));
93
	fclose($queueout);
94 a120c194 Vinicius Coque
	log_error("New alert found: $notice");
95 a0a125b9 Scott Ullrich
	/* soekris */
96 b37a2e8c Phil Davis
	if (file_exists("/dev/led/error")) {
97 5f9bcd7a Scott Ullrich
		exec("/bin/echo 1 > /dev/led/error");
98 b37a2e8c Phil Davis
	}
99 0c884086 jim-p
	/* wrap & alix */
100 1c9512f2 jim-p
	led_normalize();
101 0c884086 jim-p
	led_morse(1, 'sos');
102 3b3a7338 Scott Ullrich
	notify_via_growl($notice);
103 b19369b7 Scott Ullrich
	notify_via_smtp($notice);
104 e2c48999 Colin Smith
	return $queuekey;
105 9774f564 Colin Smith
}
106
107 91cae49d Scott Ullrich
/****f* notices/get_notices
108
 * NAME
109
 *   get_notices
110
 * INPUTS
111
 *	 $category
112
 * RESULT
113
 *   Returns a specific notices text
114
 ******/
115 9774f564 Colin Smith
function get_notices($category = "all") {
116 6955830f Ermal Lu?i
	global $g;
117
118 b37a2e8c Phil Davis
	if (file_exists("{$g['tmp_path']}/notices")) {
119 6955830f Ermal Lu?i
		$queue = unserialize(file_get_contents("{$g['tmp_path']}/notices"));
120 b37a2e8c Phil Davis
		if (!$queue) {
121
			return false;
122
		}
123
		if ($category != 'all') {
124
			foreach ($queue as $time => $notice) {
125
				if (strtolower($notice['category']) == strtolower($category)) {
126 9774f564 Colin Smith
					$toreturn[$time] = $notice;
127 b37a2e8c Phil Davis
				}
128 9774f564 Colin Smith
			}
129
			return $toreturn;
130
		} else {
131
			return $queue;
132
		}
133
	} else {
134
		return false;
135
	}
136
}
137
138 91cae49d Scott Ullrich
/****f* notices/close_notice
139
 * NAME
140
 *   close_notice
141
 * INPUTS
142
 *	 $id
143
 * RESULT
144
 *   Removes a notice from the list
145
 ******/
146 9774f564 Colin Smith
function close_notice($id) {
147
	global $notice_path;
148
	require_once("util.inc");
149 5f9bcd7a Scott Ullrich
	/* soekris */
150 b37a2e8c Phil Davis
	if (file_exists("/dev/led/error")) {
151 5f9bcd7a Scott Ullrich
		exec("/bin/echo 0 > /dev/led/error");
152 b37a2e8c Phil Davis
	}
153 0c884086 jim-p
	/* wrap & alix */
154
	led_normalize();
155 fab14377 Colin Smith
	$ids = array();
156 b37a2e8c Phil Davis
	if (!$notices = get_notices()) {
157
		return;
158
	}
159
	if ($id == "all") {
160 9774f564 Colin Smith
		unlink_if_exists($notice_path);
161
		return;
162
	}
163 b37a2e8c Phil Davis
	foreach (array_keys($notices) as $time) {
164
		if ($id == $time) {
165 9774f564 Colin Smith
			unset($notices[$id]);
166
			break;
167
		}
168
	}
169 b37a2e8c Phil Davis
	foreach ($notices as $key => $notice) {
170 9774f564 Colin Smith
		$ids[$key] = $notice['id'];
171
	}
172 b37a2e8c Phil Davis
	foreach ($ids as $time => $tocheck) {
173
		if ($id == $tocheck) {
174 9774f564 Colin Smith
			unset($notices[$time]);
175
			break;
176
		}
177
	}
178 b37a2e8c Phil Davis
	if (count($notices) != 0) {
179 cebc42e6 Colin Smith
		$queueout = fopen($notice_path, "w");
180 b37a2e8c Phil Davis
		fwrite($queueout, serialize($notices));
181
		fclose($queueout);
182 cebc42e6 Colin Smith
	} else {
183
		unlink_if_exists($notice_path);
184
	}
185 a0a125b9 Scott Ullrich
186 669e1adb Bill Marquette
	return;
187 9774f564 Colin Smith
}
188
189 91cae49d Scott Ullrich
/****f* notices/dump_xml_notices
190
 * NAME
191
 *   dump_xml_notices
192
 * INPUTS
193
 *	 NONE
194
 * RESULT
195
 *   Outputs notices in XML formatted text
196
 ******/
197 9774f564 Colin Smith
function dump_xml_notices() {
198 b37a2e8c Phil Davis
	if (file_exists("/cf/conf/use_xmlreader")) {
199 093bcebc Scott Ullrich
		require_once("xmlreader.inc");
200 b37a2e8c Phil Davis
	} else {
201 093bcebc Scott Ullrich
		require_once("xmlparse.inc");
202 b37a2e8c Phil Davis
	}
203 9774f564 Colin Smith
	global $notice_path, $listtags;
204
	$listtags[] = 'notice';
205 b37a2e8c Phil Davis
	if (!$notices = get_notices()) {
206
		return;
207
	}
208
	foreach ($notices as $time => $notice) {
209 9774f564 Colin Smith
		$notice['time'] = $time;
210
		$toput['notice'][] = $notice;
211
	}
212
	$xml = dump_xml_config($toput, 'notices');
213
	return $xml;
214
}
215
216 91cae49d Scott Ullrich
/****f* notices/print_notices
217
 * NAME
218
 *   print_notices
219
 * INPUTS
220
 *	 $notices, $category
221
 * RESULT
222
 *   prints notices to the GUI
223
 ******/
224 9774f564 Colin Smith
function print_notices($notices, $category = "all") {
225 b37a2e8c Phil Davis
	foreach ($notices as $notice) {
226
		if ($category != "all") {
227
			if (in_array($notice['category'], $category)) {
228
				$categories[] = $notice['category'];
229
			}
230 9774f564 Colin Smith
		} else {
231
			$categories[] = $notice['category'];
232
		}
233
	}
234
	$categories = array_unique($categories);
235
	sort($categories);
236 b37a2e8c Phil Davis
	foreach ($categories as $category) {
237 9774f564 Colin Smith
		$toreturn .= "<ul><li>{$category}<ul>";
238 b37a2e8c Phil Davis
		foreach ($notices as $notice) {
239
			if (strtolower($notice['category']) == strtolower($category)) {
240
				if ($notice['id'] != "") {
241
					if ($notice['url'] != "") {
242 9774f564 Colin Smith
						$toreturn .= "<li><a href={$notice['url']}>{$notice['id']}</a> - {$notice['notice']}</li>";
243
					} else {
244
						$toreturn .= "<li>{$notice['id']} - {$notice['notice']}</li>";
245
					}
246
				}
247
			}
248
		}
249
		$toreturn .= "</ul></li></ul>";
250
	}
251
	return $toreturn;
252
}
253
254 91cae49d Scott Ullrich
/****f* notices/print_notice_box
255
 * NAME
256
 *   print_notice_box
257
 * INPUTS
258
 *	 $category
259
 * RESULT
260
 *   prints an info box to the GUI
261
 ******/
262 20d4d05e Colin Smith
function print_notice_box($category = "all") {
263
	$notices = get_notices();
264 b37a2e8c Phil Davis
	if (!$notices) {
265
		return;
266
	}
267 20d4d05e Colin Smith
	print_info_box_np(print_notices($notices, $category));
268
	return;
269
}
270 6e2ec568 Scott Ullrich
271 91cae49d Scott Ullrich
/****f* notices/are_notices_pending
272
 * NAME
273
 *   are_notices_pending
274
 * INPUTS
275
 *	 $category to check
276
 * RESULT
277
 *   returns true if notices are pending, false if they are not
278
 ******/
279 34e1ff97 Scott Ullrich
function are_notices_pending($category = "all") {
280
	global $notice_path;
281 b37a2e8c Phil Davis
	if (file_exists($notice_path)) {
282 34e1ff97 Scott Ullrich
		return true;
283
	}
284
	return false;
285 6e2ec568 Scott Ullrich
}
286
287 91cae49d Scott Ullrich
/****f* notices/notify_via_smtp
288 b19369b7 Scott Ullrich
 * NAME
289
 *   notify_via_smtp
290
 * INPUTS
291
 *	 notification string to send as an email
292
 * RESULT
293
 *   returns true if message was sent
294
 ******/
295 48b86f62 jim-p
function notify_via_smtp($message, $force = false) {
296 8273ea35 Scott Ullrich
	global $config, $g;
297 b37a2e8c Phil Davis
	if (platform_booting()) {
298 8273ea35 Scott Ullrich
		return;
299 b37a2e8c Phil Davis
	}
300 b19369b7 Scott Ullrich
301 b37a2e8c Phil Davis
	if (isset($config['notifications']['smtp']['disable']) && !$force) {
302 48b86f62 jim-p
		return;
303 b37a2e8c Phil Davis
	}
304 48b86f62 jim-p
305 743c30df Scott Ullrich
	/* Do NOT send the same message twice */
306 b37a2e8c Phil Davis
	if (file_exists("/var/db/notices_lastmsg.txt")) {
307 2c7ac8dc Scott Ullrich
		$lastmsg = trim(file_get_contents("/var/db/notices_lastmsg.txt"));
308 b37a2e8c Phil Davis
		if ($lastmsg == $message) {
309 2c7ac8dc Scott Ullrich
			return;
310 b37a2e8c Phil Davis
		}
311 2c7ac8dc Scott Ullrich
	}
312 743c30df Scott Ullrich
313 7c845149 jim-p
	/* Store last message sent to avoid spamming */
314
	$fd = fopen("/var/db/notices_lastmsg.txt", "w");
315
	fwrite($fd, $message);
316
	fclose($fd);
317
318 02376f6f Phil Davis
	send_smtp_message($message, "{$config['system']['hostname']}.{$config['system']['domain']} - Notification", $force);
319 7c845149 jim-p
	return;
320
}
321
322 02376f6f Phil Davis
function send_smtp_message($message, $subject = "(no subject)", $force = false) {
323 7c845149 jim-p
	global $config, $g;
324 2cd8d942 Pierre POMES
	require_once("sasl.inc");
325 4bbb3155 Scott Ullrich
	require_once("smtp.inc");
326
327 b37a2e8c Phil Davis
	if (isset($config['notifications']['smtp']['disable']) && !$force) {
328 02376f6f Phil Davis
		return;
329 b37a2e8c Phil Davis
	}
330 02376f6f Phil Davis
331 b37a2e8c Phil Davis
	if (!$config['notifications']['smtp']['ipaddress']) {
332 7c845149 jim-p
		return;
333 b37a2e8c Phil Davis
	}
334 7c845149 jim-p
335 b37a2e8c Phil Davis
	if (!$config['notifications']['smtp']['notifyemailaddress']) {
336 7c845149 jim-p
		return;
337 b37a2e8c Phil Davis
	}
338 7c845149 jim-p
339 b19369b7 Scott Ullrich
	$smtp = new smtp_class;
340
341
	$from = "pfsense@{$config['system']['hostname']}.{$config['system']['domain']}";
342
	$to = $config['notifications']['smtp']['notifyemailaddress'];
343
344
	$smtp->host_name = $config['notifications']['smtp']['ipaddress'];
345 9277b7ef jim-p
	$smtp->host_port = empty($config['notifications']['smtp']['port']) ? 25 : $config['notifications']['smtp']['port'];
346 b19369b7 Scott Ullrich
347 96c52992 Scott Ullrich
	$smtp->direct_delivery = 0;
348 c2fe67eb Warren Baker
	$smtp->ssl = (isset($config['notifications']['smtp']['ssl'])) ? 1 : 0;
349
	$smtp->tls = (isset($config['notifications']['smtp']['tls'])) ? 1 : 0;
350 f602d493 sullrich
	$smtp->debug = 0;
351 b19369b7 Scott Ullrich
	$smtp->html_debug = 0;
352 6c07db48 Phil Davis
	$smtp->localhost = $config['system']['hostname'] . "." . $config['system']['domain'];
353 b37a2e8c Phil Davis
354
	if ($config['notifications']['smtp']['fromaddress']) {
355 72306d5a Scott Ullrich
		$from = $config['notifications']['smtp']['fromaddress'];
356 b37a2e8c Phil Davis
	}
357
358 72306d5a Scott Ullrich
	// Use SMTP Auth if fields are filled out
359 b37a2e8c Phil Davis
	if ($config['notifications']['smtp']['username'] &&
360
	    $config['notifications']['smtp']['password']) {
361 c4249322 Phil Davis
		if (isset($config['notifications']['smtp']['authentication_mechanism'])) {
362
			$smtp->authentication_mechanism = $config['notifications']['smtp']['authentication_mechanism'];
363
		} else {
364
			$smtp->authentication_mechanism = "PLAIN";
365
		}
366 1edfb2de Pierre POMES
		$smtp->user = $config['notifications']['smtp']['username'];
367 72306d5a Scott Ullrich
		$smtp->password = $config['notifications']['smtp']['password'];
368
	}
369 b19369b7 Scott Ullrich
370
	$headers = array(
371
		"From: {$from}",
372
		"To: {$to}",
373 7c845149 jim-p
		"Subject: {$subject}",
374 086cf944 Phil Davis
		"Date: " . date("r")
375 b19369b7 Scott Ullrich
	);
376
377 b37a2e8c Phil Davis
	if ($smtp->SendMessage($from, preg_split('/\s*,\s*/', trim($to)), $headers, $message)) {
378 5025dfe3 Carlos Eduardo Ramos
		log_error(sprintf(gettext("Message sent to %s OK"), $to));
379 f9fb2569 Scott Ullrich
		return;
380
	} else {
381 addc0439 Renato Botelho
		log_error(sprintf(gettext('Could not send the message to %1$s -- Error: %2$s'), $to, $smtp->error));
382
		return(sprintf(gettext('Could not send the message to %1$s -- Error: %2$s'), $to, $smtp->error));
383 f9fb2569 Scott Ullrich
	}
384 b19369b7 Scott Ullrich
}
385
386 91cae49d Scott Ullrich
/****f* notices/notify_via_growl
387 b21fc797 Scott Ullrich
 * NAME
388
 *   notify_via_growl
389
 * INPUTS
390
 *	 notification string to send
391
 * RESULT
392
 *   returns true if message was sent
393
 ******/
394 48b86f62 jim-p
function notify_via_growl($message, $force=false) {
395 b21fc797 Scott Ullrich
	require_once("growl.class");
396 086cf944 Phil Davis
	global $config, $g;
397 a93020d5 Scott Ullrich
398 b37a2e8c Phil Davis
	if (isset($config['notifications']['growl']['disable']) && !$force) {
399 48b86f62 jim-p
		return;
400 b37a2e8c Phil Davis
	}
401 48b86f62 jim-p
402 a93020d5 Scott Ullrich
	/* Do NOT send the same message twice */
403 b37a2e8c Phil Davis
	if (file_exists("/var/db/growlnotices_lastmsg.txt")) {
404 a93020d5 Scott Ullrich
		$lastmsg = trim(file_get_contents("/var/db/growlnotices_lastmsg.txt"));
405 b37a2e8c Phil Davis
		if ($lastmsg == $message) {
406 a93020d5 Scott Ullrich
			return;
407 b37a2e8c Phil Davis
		}
408 a93020d5 Scott Ullrich
	}
409
410 9a132756 Scott Ullrich
	$hostname = $config['system']['hostname'] . "." . $config['system']['domain'];
411 b21fc797 Scott Ullrich
	$growl_ip = $config['notifications']['growl']['ipaddress'];
412
	$growl_password = $config['notifications']['growl']['password'];
413 addbcae7 Scott Ullrich
	$growl_name = $config['notifications']['growl']['name'];
414
	$growl_notification = $config['notifications']['growl']['notification_name'];
415 b37a2e8c Phil Davis
416 6c07db48 Phil Davis
	if (!empty($growl_ip) && (is_ipaddr($growl_ip) || dns_get_record($growl_ip, DNS_A) || dns_get_record($growl_ip, DNS_AAAA))) {
417 addbcae7 Scott Ullrich
		$growl = new Growl($growl_ip, $growl_password, $growl_name);
418 fea89a63 Warren Baker
		$growl->notify("{$growl_notification}", gettext(sprintf("%s (%s) - Notification", $g['product_name'], $hostname)), "{$message}");
419 b21fc797 Scott Ullrich
	}
420 a93020d5 Scott Ullrich
421
	/* Store last message sent to avoid spamming */
422
	$fd = fopen("/var/db/growlnotices_lastmsg.txt", "w");
423
	fwrite($fd, $message);
424
	fclose($fd);
425 b21fc797 Scott Ullrich
}
426
427 91cae49d Scott Ullrich
/****f* notices/register_via_growl
428 b21fc797 Scott Ullrich
 * NAME
429
 *   register_via_growl
430
 * INPUTS
431
 *	 none
432
 * RESULT
433
 *   none
434
 ******/
435
function register_via_growl() {
436
	require_once("growl.class");
437
	global $config;
438
	$growl_ip = $config['notifications']['growl']['ipaddress'];
439
	$growl_password = $config['notifications']['growl']['password'];
440 addbcae7 Scott Ullrich
	$growl_name = $config['notifications']['growl']['name'];
441
	$growl_notification = $config['notifications']['growl']['notification_name'];
442 b37a2e8c Phil Davis
443
	if ($growl_ip) {
444
		$growl = new Growl($growl_ip, $growl_password, $growl_name);
445 addbcae7 Scott Ullrich
		$growl->addNotification($growl_notification);
446 b21fc797 Scott Ullrich
		$growl->register();
447
	}
448
}
449
450 53842a6c jim-p
/* Notify via remote methods only - not via GUI. */
451
function notify_all_remote($msg) {
452
	notify_via_smtp($msg);
453
	notify_via_growl($msg);
454
}
455
456 6955830f Ermal Lu?i
?>