Project

General

Profile

Download (9.97 KB) Statistics
| Branch: | Tag: | Revision:
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) 2009 Scott Ullrich (sullrich@gmail.com)
12
 * 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
/*
38
	pfSense_BUILDER_BINARIES:	/bin/echo
39
	pfSense_MODULE:	notifications
40
*/
41

    
42
require_once("globals.inc");
43
require_once("led.inc");
44

    
45
$notice_path = $g['tmp_path'] . '/notices';
46

    
47
/****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
function file_notice($id, $notice, $category = "General", $url = "", $priority = 1) {
56
	/*
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
	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
	if(!$queueout) {
76
		log_error("Could not open {$notice_path} for writing");
77
		return;
78
	}
79
	fwrite($queueout, serialize($queue));
80
	fclose($queueout);
81
	log_error("New alert found: {$notice}");
82
	/* soekris */
83
	if(file_exists("/dev/led/error"))
84
		exec("/bin/echo 1 > /dev/led/error");
85
	/* wrap & alix */
86
	led_normalize();
87
	led_morse(1, 'sos');
88
	notify_via_growl($notice);
89
	notify_via_smtp($notice);
90
	return $queuekey;
91
}
92

    
93
/****f* notices/get_notices
94
 * NAME
95
 *   get_notices
96
 * INPUTS
97
 *	 $category
98
 * RESULT
99
 *   Returns a specific notices text
100
 ******/
101
function get_notices($category = "all") {
102
	global $g;
103

    
104
	if(file_exists("{$g['tmp_path']}/notices")) {
105
		$queue = unserialize(file_get_contents("{$g['tmp_path']}/notices"));
106
		if(!$queue) return false;
107
		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
/****f* notices/close_notice
122
 * NAME
123
 *   close_notice
124
 * INPUTS
125
 *	 $id
126
 * RESULT
127
 *   Removes a notice from the list
128
 ******/
129
function close_notice($id) {
130
	global $notice_path;
131
	require_once("util.inc");
132
	/* soekris */
133
	if(file_exists("/dev/led/error"))
134
		exec("/bin/echo 0 > /dev/led/error");
135
	/* wrap & alix */
136
	led_normalize();
137
	$ids = array();
138
	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
	if(count($notices) != 0) {
159
		$queueout = fopen($notice_path, "w");
160
        	fwrite($queueout, serialize($notices));
161
        	fclose($queueout);
162
	} else {
163
		unlink_if_exists($notice_path);
164
	}
165

    
166
	return;
167
}
168

    
169
/****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
function dump_xml_notices() {
178
	if(file_exists("/cf/conf/use_xmlreader"))
179
		require_once("xmlreader.inc");
180
	else
181
		require_once("xmlparse.inc");
182
	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
/****f* notices/print_notices
194
 * NAME
195
 *   print_notices
196
 * INPUTS
197
 *	 $notices, $category
198
 * RESULT
199
 *   prints notices to the GUI
200
 ******/
201
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
			if(strtolower($notice['category']) == strtolower($category)) {
215
				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
/****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
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

    
244
/****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
function are_notices_pending($category = "all") {
253
	global $notice_path;
254
	if(file_exists($notice_path)) {
255
		return true;
256
	}
257
	return false;
258
}
259

    
260
/****f* notices/notify_via_smtp
261
 * 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
function notify_via_smtp($message) {
269
	global $config, $g;
270
	if($g['booting'])
271
		return;
272

    
273
	if(!$config['notifications']['smtp']['ipaddress'])
274
		return;
275

    
276
	if(!$config['notifications']['smtp']['notifyemailaddress']) 
277
		return;
278

    
279
	/* Do NOT send the same message twice */
280
	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

    
286
	require_once("smtp.inc");
287

    
288
	$smtp = new smtp_class;
289

    
290
	$from = "pfsense@{$config['system']['hostname']}.{$config['system']['domain']}";
291
	$to = $config['notifications']['smtp']['notifyemailaddress'];
292

    
293
	$smtp->host_name = $config['notifications']['smtp']['ipaddress'];
294
	$smtp->host_port = 25;
295

    
296
	$smtp->direct_delivery = 0;
297
	$smtp->ssl = 0;
298
	$smtp->debug = 0;
299
	$smtp->html_debug = 0;
300
	$smtp->localhost=$config['system']['hostname'].".".$config['system']['domain'];
301
	
302
	if($config['notifications']['smtp']['fromaddress'])
303
		$from = $config['notifications']['smtp']['fromaddress'];
304
	
305
	// Use SMTP Auth if fields are filled out
306
	if($config['notifications']['smtp']['username'] && 
307
	   $config['notifications']['smtp']['password']) {
308
		$smtp->authentication_mechanism = "PLAIN";
309
		$smtp->username = $config['notifications']['smtp']['username'];
310
		$smtp->password = $config['notifications']['smtp']['password'];
311
	}
312

    
313
	$headers = array(
314
		"From: {$from}",
315
		"To: {$to}",
316
		"Subject: {$config['system']['hostname']}.{$config['system']['domain']} - Notification",
317
		"Date: ".strftime("%a, %d %b %Y %H:%M:%S %Z")
318
	);
319

    
320
	/* Store last message sent to avoid spamming */
321
	$fd = fopen("/var/db/notices_lastmsg.txt", "w");
322
	fwrite($fd, $message);
323
	fclose($fd);
324

    
325
	if($smtp->SendMessage($from, array($to), $headers, $message)) {
326
		log_error("Message sent to {$to} OK");
327
		return;
328
	} else {
329
		log_error("Could not send the message to {$to} -- Error: {$smtp->error}");
330
		return("Could not send the message to {$to} -- Error: {$smtp->error}");
331
	}
332

    
333
}
334

    
335

    
336
/****f* notices/notify_via_growl
337
 * NAME
338
 *   notify_via_growl
339
 * INPUTS
340
 *	 notification string to send
341
 * RESULT
342
 *   returns true if message was sent
343
 ******/
344
function notify_via_growl($message) {
345
	require_once("growl.class");
346
	global $config;
347
	$growl_ip = $config['notifications']['growl']['ipaddress'];
348
	$growl_password = $config['notifications']['growl']['password'];
349
	$growl_name = $config['notifications']['growl']['name'];
350
	$growl_notification = $config['notifications']['growl']['notification_name'];
351
	
352
	if(!empty($growl_ip)) {
353
		$growl = new Growl($growl_ip, $growl_password, $growl_name);
354
		$growl->notify("{$growl_notification}", "pfSense", "{$message}");
355
	}
356
}
357

    
358
/****f* notices/register_via_growl
359
 * NAME
360
 *   register_via_growl
361
 * INPUTS
362
 *	 none
363
 * RESULT
364
 *   none
365
 ******/
366
function register_via_growl() {
367
	require_once("growl.class");
368
	global $config;
369
	$growl_ip = $config['notifications']['growl']['ipaddress'];
370
	$growl_password = $config['notifications']['growl']['password'];
371
	$growl_name = $config['notifications']['growl']['name'];
372
	$growl_notification = $config['notifications']['growl']['notification_name'];
373
	
374
	if($growl_ip) {
375
	  $growl = new Growl($growl_ip, $growl_password, $growl_name);
376
		$growl->addNotification($growl_notification);
377
		$growl->register();
378
	}
379
}
380

    
381
?>
(26-26/50)