Project

General

Profile

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