Project

General

Profile

Download (10.7 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
47 91cae49d Scott Ullrich
/****f* notices/file_notice
48
 * NAME
49
 *   file_notice
50
 * INPUTS
51
 *	 $id, $notice, $category, $url, $priority
52
 * RESULT
53
 *   Files a notice and kicks off the various alerts, smtp, growl, system log, LED's, etc.
54
 ******/
55 c6e2b163 Colin Smith
function file_notice($id, $notice, $category = "General", $url = "", $priority = 1) {
56 91cae49d Scott Ullrich
	/*
57
	 * $category - Category that this notice should be displayed under. This can be arbitrary,
58
	 * 	       but a page must be set to receive this messages for it to be displayed.
59
	 *
60
	 * $priority - A notice's priority. Higher numbers indicate greater severity.
61
	 *	       0 = informational, 1 = warning, 2 = error, etc. This may also be arbitrary,
62
	 */
63 9774f564 Colin Smith
	global $notice_path;
64
	if(!$queue = get_notices()) $queue = array();
65
	$queuekey = time();
66
	$toqueue = array(
67
				'id'		=> $id,
68
				'notice'	=> $notice,
69
				'url'		=> $url,
70
				'category'	=> $category,
71
				'priority'	=> $priority,
72
			);
73
	$queue[$queuekey] = $toqueue;
74
	$queueout = fopen($notice_path, "w");
75 34e1ff97 Scott Ullrich
	if(!$queueout) {
76 5025dfe3 Carlos Eduardo Ramos
		log_error(printf(gettext("Could not open %s for writing"), $notice_path));
77 34e1ff97 Scott Ullrich
		return;
78
	}
79 9774f564 Colin Smith
	fwrite($queueout, serialize($queue));
80
	fclose($queueout);
81 a120c194 Vinicius Coque
	log_error("New alert found: $notice");
82 a0a125b9 Scott Ullrich
	/* soekris */
83 385b3413 Scott Ullrich
	if(file_exists("/dev/led/error"))
84 5f9bcd7a Scott Ullrich
		exec("/bin/echo 1 > /dev/led/error");
85 0c884086 jim-p
	/* wrap & alix */
86 1c9512f2 jim-p
	led_normalize();
87 0c884086 jim-p
	led_morse(1, 'sos');
88 3b3a7338 Scott Ullrich
	notify_via_growl($notice);
89 b19369b7 Scott Ullrich
	notify_via_smtp($notice);
90 e2c48999 Colin Smith
	return $queuekey;
91 9774f564 Colin Smith
}
92
93 91cae49d Scott Ullrich
/****f* notices/get_notices
94
 * NAME
95
 *   get_notices
96
 * INPUTS
97
 *	 $category
98
 * RESULT
99
 *   Returns a specific notices text
100
 ******/
101 9774f564 Colin Smith
function get_notices($category = "all") {
102 6955830f Ermal Lu?i
	global $g;
103
104
	if(file_exists("{$g['tmp_path']}/notices")) {
105
		$queue = unserialize(file_get_contents("{$g['tmp_path']}/notices"));
106 20d4d05e Colin Smith
		if(!$queue) return false;
107 9774f564 Colin Smith
		if($category != 'all') {
108
			foreach($queue as $time => $notice) {
109
				if(strtolower($notice['category']) == strtolower($category))
110
					$toreturn[$time] = $notice;
111
			}
112
			return $toreturn;
113
		} else {
114
			return $queue;
115
		}
116
	} else {
117
		return false;
118
	}
119
}
120
121 91cae49d Scott Ullrich
/****f* notices/close_notice
122
 * NAME
123
 *   close_notice
124
 * INPUTS
125
 *	 $id
126
 * RESULT
127
 *   Removes a notice from the list
128
 ******/
129 9774f564 Colin Smith
function close_notice($id) {
130
	global $notice_path;
131
	require_once("util.inc");
132 5f9bcd7a Scott Ullrich
	/* soekris */
133
	if(file_exists("/dev/led/error"))
134
		exec("/bin/echo 0 > /dev/led/error");
135 0c884086 jim-p
	/* wrap & alix */
136
	led_normalize();
137 fab14377 Colin Smith
	$ids = array();
138 9774f564 Colin Smith
	if(!$notices = get_notices()) return;
139
	if($id == "all") {
140
		unlink_if_exists($notice_path);
141
		return;
142
	}
143
	foreach(array_keys($notices) as $time) {
144
		if($id == $time) {
145
			unset($notices[$id]);
146
			break;
147
		}
148
	}
149
	foreach($notices as $key => $notice) {
150
		$ids[$key] = $notice['id'];
151
	}
152
	foreach($ids as $time => $tocheck) {
153
		if($id == $tocheck) {
154
			unset($notices[$time]);
155
			break;
156
		}
157
	}
158 fab14377 Colin Smith
	if(count($notices) != 0) {
159 cebc42e6 Colin Smith
		$queueout = fopen($notice_path, "w");
160 fab14377 Colin Smith
        	fwrite($queueout, serialize($notices));
161 cebc42e6 Colin Smith
        	fclose($queueout);
162
	} else {
163
		unlink_if_exists($notice_path);
164
	}
165 a0a125b9 Scott Ullrich
166 669e1adb Bill Marquette
	return;
167 9774f564 Colin Smith
}
168
169 91cae49d Scott Ullrich
/****f* notices/dump_xml_notices
170
 * NAME
171
 *   dump_xml_notices
172
 * INPUTS
173
 *	 NONE
174
 * RESULT
175
 *   Outputs notices in XML formatted text
176
 ******/
177 9774f564 Colin Smith
function dump_xml_notices() {
178 093bcebc Scott Ullrich
	if(file_exists("/cf/conf/use_xmlreader"))
179
		require_once("xmlreader.inc");
180
	else
181
		require_once("xmlparse.inc");
182 9774f564 Colin Smith
	global $notice_path, $listtags;
183
	$listtags[] = 'notice';
184
	if(!$notices = get_notices()) return;
185
	foreach($notices as $time => $notice) {
186
		$notice['time'] = $time;
187
		$toput['notice'][] = $notice;
188
	}
189
	$xml = dump_xml_config($toput, 'notices');
190
	return $xml;
191
}
192
193 91cae49d Scott Ullrich
/****f* notices/print_notices
194
 * NAME
195
 *   print_notices
196
 * INPUTS
197
 *	 $notices, $category
198
 * RESULT
199
 *   prints notices to the GUI
200
 ******/
201 9774f564 Colin Smith
function print_notices($notices, $category = "all") {
202
	foreach($notices as $notice) {
203
		if($category != "all") {
204
			if(in_array($notice['category'], $category)) $categories[] = $notice['category'];
205
		} else {
206
			$categories[] = $notice['category'];
207
		}
208
	}
209
	$categories = array_unique($categories);
210
	sort($categories);
211
	foreach($categories as $category) {
212
		$toreturn .= "<ul><li>{$category}<ul>";
213
		foreach($notices as $notice) {
214 08421c0d Colin Smith
			if(strtolower($notice['category']) == strtolower($category)) {
215 9774f564 Colin Smith
				if($notice['id'] != "") {
216
					if($notice['url'] != "") {
217
						$toreturn .= "<li><a href={$notice['url']}>{$notice['id']}</a> - {$notice['notice']}</li>";
218
					} else {
219
						$toreturn .= "<li>{$notice['id']} - {$notice['notice']}</li>";
220
					}
221
				}
222
			}
223
		}
224
		$toreturn .= "</ul></li></ul>";
225
	}
226
	return $toreturn;
227
}
228
229 91cae49d Scott Ullrich
/****f* notices/print_notice_box
230
 * NAME
231
 *   print_notice_box
232
 * INPUTS
233
 *	 $category
234
 * RESULT
235
 *   prints an info box to the GUI
236
 ******/
237 20d4d05e Colin Smith
function print_notice_box($category = "all") {
238
	$notices = get_notices();
239
	if(!$notices) return;
240
	print_info_box_np(print_notices($notices, $category));
241
	return;
242
}
243 6e2ec568 Scott Ullrich
244 91cae49d Scott Ullrich
/****f* notices/are_notices_pending
245
 * NAME
246
 *   are_notices_pending
247
 * INPUTS
248
 *	 $category to check
249
 * RESULT
250
 *   returns true if notices are pending, false if they are not
251
 ******/
252 34e1ff97 Scott Ullrich
function are_notices_pending($category = "all") {
253
	global $notice_path;
254
	if(file_exists($notice_path)) {
255
		return true;
256
	}
257
	return false;
258 6e2ec568 Scott Ullrich
}
259
260 91cae49d Scott Ullrich
/****f* notices/notify_via_smtp
261 b19369b7 Scott Ullrich
 * NAME
262
 *   notify_via_smtp
263
 * INPUTS
264
 *	 notification string to send as an email
265
 * RESULT
266
 *   returns true if message was sent
267
 ******/
268 7bb30b16 Scott Ullrich
function notify_via_smtp($message) {
269 8273ea35 Scott Ullrich
	global $config, $g;
270
	if($g['booting'])
271
		return;
272 b19369b7 Scott Ullrich
273
	if(!$config['notifications']['smtp']['ipaddress'])
274
		return;
275
276 4bbb3155 Scott Ullrich
	if(!$config['notifications']['smtp']['notifyemailaddress']) 
277
		return;
278
279 743c30df Scott Ullrich
	/* Do NOT send the same message twice */
280 2c7ac8dc Scott Ullrich
	if(file_exists("/var/db/notices_lastmsg.txt")) {
281
		$lastmsg = trim(file_get_contents("/var/db/notices_lastmsg.txt"));
282
		if($lastmsg == $message)
283
			return;
284
	}
285 743c30df Scott Ullrich
286 2cd8d942 Pierre POMES
	require_once("sasl.inc");
287 4bbb3155 Scott Ullrich
	require_once("smtp.inc");
288
289 b19369b7 Scott Ullrich
	$smtp = new smtp_class;
290
291
	$from = "pfsense@{$config['system']['hostname']}.{$config['system']['domain']}";
292
	$to = $config['notifications']['smtp']['notifyemailaddress'];
293
294
	$smtp->host_name = $config['notifications']['smtp']['ipaddress'];
295 9277b7ef jim-p
	$smtp->host_port = empty($config['notifications']['smtp']['port']) ? 25 : $config['notifications']['smtp']['port'];
296 b19369b7 Scott Ullrich
297 96c52992 Scott Ullrich
	$smtp->direct_delivery = 0;
298 2d74f1cf Albert S. Causing
	$smtp->ssl = ($config['notifications']['smtp']['ssl'] == "checked") ? 1 : 0;
299 f602d493 sullrich
	$smtp->debug = 0;
300 b19369b7 Scott Ullrich
	$smtp->html_debug = 0;
301 1467b79f pierrepomes
	$smtp->localhost=$config['system']['hostname'].".".$config['system']['domain'];
302 72306d5a Scott Ullrich
	
303
	if($config['notifications']['smtp']['fromaddress'])
304
		$from = $config['notifications']['smtp']['fromaddress'];
305
	
306
	// Use SMTP Auth if fields are filled out
307
	if($config['notifications']['smtp']['username'] && 
308
	   $config['notifications']['smtp']['password']) {
309
		$smtp->authentication_mechanism = "PLAIN";
310 1edfb2de Pierre POMES
		$smtp->user = $config['notifications']['smtp']['username'];
311 72306d5a Scott Ullrich
		$smtp->password = $config['notifications']['smtp']['password'];
312
	}
313 b19369b7 Scott Ullrich
314
	$headers = array(
315
		"From: {$from}",
316
		"To: {$to}",
317
		"Subject: {$config['system']['hostname']}.{$config['system']['domain']} - Notification",
318
		"Date: ".strftime("%a, %d %b %Y %H:%M:%S %Z")
319
	);
320
321 743c30df Scott Ullrich
	/* Store last message sent to avoid spamming */
322
	$fd = fopen("/var/db/notices_lastmsg.txt", "w");
323
	fwrite($fd, $message);
324
	fclose($fd);
325
326 f9fb2569 Scott Ullrich
	if($smtp->SendMessage($from, array($to), $headers, $message)) {
327 5025dfe3 Carlos Eduardo Ramos
		log_error(sprintf(gettext("Message sent to %s OK"), $to));
328 f9fb2569 Scott Ullrich
		return;
329
	} else {
330 addc0439 Renato Botelho
		log_error(sprintf(gettext('Could not send the message to %1$s -- Error: %2$s'), $to, $smtp->error));
331
		return(sprintf(gettext('Could not send the message to %1$s -- Error: %2$s'), $to, $smtp->error));
332 f9fb2569 Scott Ullrich
	}
333 b19369b7 Scott Ullrich
334
}
335
336
337 91cae49d Scott Ullrich
/****f* notices/notify_via_growl
338 b21fc797 Scott Ullrich
 * NAME
339
 *   notify_via_growl
340
 * INPUTS
341
 *	 notification string to send
342
 * RESULT
343
 *   returns true if message was sent
344
 ******/
345
function notify_via_growl($message) {
346
	require_once("growl.class");
347 fea89a63 Warren Baker
	global $config,$g;
348 a93020d5 Scott Ullrich
349
	/* Do NOT send the same message twice */
350
	if(file_exists("/var/db/growlnotices_lastmsg.txt")) {
351
		$lastmsg = trim(file_get_contents("/var/db/growlnotices_lastmsg.txt"));
352
		if($lastmsg == $message)
353
			return;
354
	}
355
356 9a132756 Scott Ullrich
	$hostname = $config['system']['hostname'] . "." . $config['system']['domain'];
357 b21fc797 Scott Ullrich
	$growl_ip = $config['notifications']['growl']['ipaddress'];
358
	$growl_password = $config['notifications']['growl']['password'];
359 addbcae7 Scott Ullrich
	$growl_name = $config['notifications']['growl']['name'];
360
	$growl_notification = $config['notifications']['growl']['notification_name'];
361
	
362 575a42f5 Ermal Lu?i
	if(!empty($growl_ip)) {
363 addbcae7 Scott Ullrich
		$growl = new Growl($growl_ip, $growl_password, $growl_name);
364 fea89a63 Warren Baker
		$growl->notify("{$growl_notification}", gettext(sprintf("%s (%s) - Notification", $g['product_name'], $hostname)), "{$message}");
365 b21fc797 Scott Ullrich
	}
366 a93020d5 Scott Ullrich
367
	/* Store last message sent to avoid spamming */
368
	$fd = fopen("/var/db/growlnotices_lastmsg.txt", "w");
369
	fwrite($fd, $message);
370
	fclose($fd);
371 b21fc797 Scott Ullrich
}
372
373 91cae49d Scott Ullrich
/****f* notices/register_via_growl
374 b21fc797 Scott Ullrich
 * NAME
375
 *   register_via_growl
376
 * INPUTS
377
 *	 none
378
 * RESULT
379
 *   none
380
 ******/
381
function register_via_growl() {
382
	require_once("growl.class");
383
	global $config;
384
	$growl_ip = $config['notifications']['growl']['ipaddress'];
385
	$growl_password = $config['notifications']['growl']['password'];
386 addbcae7 Scott Ullrich
	$growl_name = $config['notifications']['growl']['name'];
387
	$growl_notification = $config['notifications']['growl']['notification_name'];
388
	
389 b21fc797 Scott Ullrich
	if($growl_ip) {
390 addbcae7 Scott Ullrich
	  $growl = new Growl($growl_ip, $growl_password, $growl_name);
391
		$growl->addNotification($growl_notification);
392 b21fc797 Scott Ullrich
		$growl->register();
393
	}
394
}
395
396 6955830f Ermal Lu?i
?>