notices.inc 9.99 KB
Newer Older
Ad Schellevis's avatar
Ad Schellevis committed
1
<?php
2 3

/*
4 5
 * Copyright (C) 2009 Scott Ullrich <sullrich@gmail.com>
 * Copyright (C) 2005 Colin Smith <ethethlay@gmail.com>
Ad Schellevis's avatar
Ad Schellevis committed
6
 * All rights reserved.
7
 *
Ad Schellevis's avatar
Ad Schellevis committed
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
 * AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
 * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * RISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 *
 */

require_once("led.inc");
32 33 34
require_once("notices.growl.class");
require_once("notices.sasl.inc");
require_once("notices.smtp.inc");
Ad Schellevis's avatar
Ad Schellevis committed
35 36 37 38 39 40 41 42 43 44 45 46

/****f* notices/file_notice
 * NAME
 *   file_notice
 * INPUTS
 *	 $id, $notice, $category, $url, $priority
 * RESULT
 *   Files a notice and kicks off the various alerts, smtp, growl, system log, LED's, etc.
 ******/
function file_notice($id, $notice, $category = "General", $url = "", $priority = 1) {
	/*
	 * $category - Category that this notice should be displayed under. This can be arbitrary,
47
	 *	       but a page must be set to receive this messages for it to be displayed.
Ad Schellevis's avatar
Ad Schellevis committed
48 49 50 51 52 53 54 55 56 57 58 59 60 61
	 *
	 * $priority - A notice's priority. Higher numbers indicate greater severity.
	 *	       0 = informational, 1 = warning, 2 = error, etc. This may also be arbitrary,
	 */
	if(!$queue = get_notices()) $queue = array();
	$queuekey = time();
	$toqueue = array(
				'id'		=> $id,
				'notice'	=> $notice,
				'url'		=> $url,
				'category'	=> $category,
				'priority'	=> $priority,
			);
	$queue[$queuekey] = $toqueue;
62
	$queueout = fopen('/tmp/notices', 'w');
Ad Schellevis's avatar
Ad Schellevis committed
63
	if(!$queueout) {
64
		log_error(printf(gettext("Could not open %s for writing"), '/tmp/notices'));
Ad Schellevis's avatar
Ad Schellevis committed
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
		return;
	}
	fwrite($queueout, serialize($queue));
	fclose($queueout);
	log_error("New alert found: $notice");
	/* soekris */
	if(file_exists("/dev/led/error"))
		exec("/bin/echo 1 > /dev/led/error");
	/* wrap & alix */
	led_normalize();
	led_morse(1, 'sos');
	notify_via_growl($notice);
	notify_via_smtp($notice);
	return $queuekey;
}

/****f* notices/get_notices
 * NAME
 *   get_notices
 * INPUTS
 *	 $category
 * RESULT
 *   Returns a specific notices text
 ******/
89 90 91 92
function get_notices($category = 'all')
{
	if (file_exists('/tmp/notices')) {
		$queue = unserialize(file_get_contents('/tmp/notices'));
93 94 95 96
		if (!$queue) {
			return false;
		}
		if ($category != 'all') {
Ad Schellevis's avatar
Ad Schellevis committed
97
			foreach($queue as $time => $notice) {
98
				if (strtolower($notice['category']) == strtolower($category)) {
Ad Schellevis's avatar
Ad Schellevis committed
99
					$toreturn[$time] = $notice;
100
				}
Ad Schellevis's avatar
Ad Schellevis committed
101 102 103
			}
			return $toreturn;
		}
104 105

		return $queue;
Ad Schellevis's avatar
Ad Schellevis committed
106
	}
107 108

	return false;
Ad Schellevis's avatar
Ad Schellevis committed
109 110 111 112 113 114 115 116 117 118
}

/****f* notices/close_notice
 * NAME
 *   close_notice
 * INPUTS
 *	 $id
 * RESULT
 *   Removes a notice from the list
 ******/
119 120 121
function close_notice($id)
{

Ad Schellevis's avatar
Ad Schellevis committed
122 123 124 125 126 127 128 129
	/* soekris */
	if(file_exists("/dev/led/error"))
		exec("/bin/echo 0 > /dev/led/error");
	/* wrap & alix */
	led_normalize();
	$ids = array();
	if(!$notices = get_notices()) return;
	if($id == "all") {
130
		@unlink('/tmp/notices');
Ad Schellevis's avatar
Ad Schellevis committed
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
		return;
	}
	foreach(array_keys($notices) as $time) {
		if($id == $time) {
			unset($notices[$id]);
			break;
		}
	}
	foreach($notices as $key => $notice) {
		$ids[$key] = $notice['id'];
	}
	foreach($ids as $time => $tocheck) {
		if($id == $tocheck) {
			unset($notices[$time]);
			break;
		}
	}
	if(count($notices) != 0) {
149
		$queueout = fopen('/tmp/notices', 'w');
150 151
		fwrite($queueout, serialize($notices));
		fclose($queueout);
Ad Schellevis's avatar
Ad Schellevis committed
152
	} else {
153
		@unlink('/tmp/notices');
Ad Schellevis's avatar
Ad Schellevis committed
154 155 156 157 158 159 160 161 162 163 164
	}
}

/****f* notices/print_notices
 * NAME
 *   print_notices
 * INPUTS
 *	 $notices, $category
 * RESULT
 *   prints notices to the GUI
 ******/
165 166 167 168 169 170 171 172
function print_notices($notices, $category = "all")
{
	if (!is_array($notices) || count($notices) == 0) {
		/* nothing to do */
		return;
	}

	foreach ($notices as $notice) {
Ad Schellevis's avatar
Ad Schellevis committed
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195
		if($category != "all") {
			if(in_array($notice['category'], $category)) $categories[] = $notice['category'];
		} else {
			$categories[] = $notice['category'];
		}
	}
	$categories = array_unique($categories);
	sort($categories);
	foreach($categories as $category) {
		$toreturn .= "<ul><li>{$category}<ul>";
		foreach($notices as $notice) {
			if(strtolower($notice['category']) == strtolower($category)) {
				if($notice['id'] != "") {
					if($notice['url'] != "") {
						$toreturn .= "<li><a href={$notice['url']}>{$notice['id']}</a> - {$notice['notice']}</li>";
					} else {
						$toreturn .= "<li>{$notice['id']} - {$notice['notice']}</li>";
					}
				}
			}
		}
		$toreturn .= "</ul></li></ul>";
	}
196

Ad Schellevis's avatar
Ad Schellevis committed
197 198 199 200 201 202 203 204 205 206 207 208
	return $toreturn;
}


/****f* notices/are_notices_pending
 * NAME
 *   are_notices_pending
 * INPUTS
 *	 $category to check
 * RESULT
 *   returns true if notices are pending, false if they are not
 ******/
209 210 211
function are_notices_pending($category = 'all')
{
	if (file_exists('/tmp/notices')) {
Ad Schellevis's avatar
Ad Schellevis committed
212 213
		return true;
	}
214

Ad Schellevis's avatar
Ad Schellevis committed
215 216 217 218 219 220 221 222 223 224 225 226
	return false;
}

/****f* notices/notify_via_smtp
 * NAME
 *   notify_via_smtp
 * INPUTS
 *	 notification string to send as an email
 * RESULT
 *   returns true if message was sent
 ******/
function notify_via_smtp($message, $force = false) {
227
	global $config;
Ad Schellevis's avatar
Ad Schellevis committed
228
	if(file_exists("/var/run/booting"))
Ad Schellevis's avatar
Ad Schellevis committed
229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250
		return;

	if(isset($config['notifications']['smtp']['disable']) && !$force)
		return;

	/* Do NOT send the same message twice */
	if(file_exists("/var/db/notices_lastmsg.txt")) {
		$lastmsg = trim(file_get_contents("/var/db/notices_lastmsg.txt"));
		if($lastmsg == $message)
			return;
	}

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

	send_smtp_message($message, "{$config['system']['hostname']}.{$config['system']['domain']} - Notification");
	return;
}

function send_smtp_message($message, $subject = "(no subject)") {
251
	global $config;
Ad Schellevis's avatar
Ad Schellevis committed
252 253 254 255 256 257 258 259 260

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

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

	$smtp = new smtp_class;

261
	$from = "opnsense@{$config['system']['hostname']}.{$config['system']['domain']}";
Ad Schellevis's avatar
Ad Schellevis committed
262 263 264 265 266 267 268 269 270 271 272
	$to = $config['notifications']['smtp']['notifyemailaddress'];

	$smtp->host_name = $config['notifications']['smtp']['ipaddress'];
	$smtp->host_port = empty($config['notifications']['smtp']['port']) ? 25 : $config['notifications']['smtp']['port'];

	$smtp->direct_delivery = 0;
	$smtp->ssl = (isset($config['notifications']['smtp']['ssl'])) ? 1 : 0;
	$smtp->tls = (isset($config['notifications']['smtp']['tls'])) ? 1 : 0;
	$smtp->debug = 0;
	$smtp->html_debug = 0;
	$smtp->localhost=$config['system']['hostname'].".".$config['system']['domain'];
273

Ad Schellevis's avatar
Ad Schellevis committed
274 275
	if($config['notifications']['smtp']['fromaddress'])
		$from = $config['notifications']['smtp']['fromaddress'];
276

Ad Schellevis's avatar
Ad Schellevis committed
277
	// Use SMTP Auth if fields are filled out
278
	if($config['notifications']['smtp']['username'] &&
Ad Schellevis's avatar
Ad Schellevis committed
279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295
	   $config['notifications']['smtp']['password']) {
		$smtp->authentication_mechanism = "PLAIN";
		$smtp->user = $config['notifications']['smtp']['username'];
		$smtp->password = $config['notifications']['smtp']['password'];
	}

	$headers = array(
		"From: {$from}",
		"To: {$to}",
		"Subject: {$subject}",
		"Date: ".date("r")
	);

	if($smtp->SendMessage($from, preg_split('/\s*,\s*/', trim($to)), $headers, $message)) {
		log_error(sprintf(gettext("Message sent to %s OK"), $to));
		return;
	} else {
296 297 298
		$msg = sprintf(gettext('Could not send the message to %s -- Error: %s'), $to, $smtp->error);
		log_error($msg);
		return ($msg);
Ad Schellevis's avatar
Ad Schellevis committed
299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323
	}
}

/****f* notices/notify_via_growl
 * NAME
 *   notify_via_growl
 * INPUTS
 *	 notification string to send
 * RESULT
 *   returns true if message was sent
 ******/
function notify_via_growl($message, $force=false) {
	global $config,$g;

	if (isset($config['notifications']['growl']['disable']) && !$force)
		return;

	/* Do NOT send the same message twice */
	if(file_exists("/var/db/growlnotices_lastmsg.txt")) {
		$lastmsg = trim(file_get_contents("/var/db/growlnotices_lastmsg.txt"));
		if($lastmsg == $message)
			return;
	}

	$hostname = $config['system']['hostname'] . "." . $config['system']['domain'];
324 325 326 327 328
	if (isset($config['notifications']['growl']['ipaddress'])) {
		$growl_ip = $config['notifications']['growl']['ipaddress'];
		$growl_password = $config['notifications']['growl']['password'];
		$growl_name = $config['notifications']['growl']['name'];
		$growl_notification = $config['notifications']['growl']['notification_name'];
329

Ad Schellevis's avatar
Ad Schellevis committed
330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353
		$growl = new Growl($growl_ip, $growl_password, $growl_name);
		$growl->notify("{$growl_notification}", gettext(sprintf("%s (%s) - Notification", $g['product_name'], $hostname)), "{$message}");
	}

	/* Store last message sent to avoid spamming */
	$fd = fopen("/var/db/growlnotices_lastmsg.txt", "w");
	fwrite($fd, $message);
	fclose($fd);
}

/****f* notices/register_via_growl
 * NAME
 *   register_via_growl
 * INPUTS
 *	 none
 * RESULT
 *   none
 ******/
function register_via_growl() {
	global $config;
	$growl_ip = $config['notifications']['growl']['ipaddress'];
	$growl_password = $config['notifications']['growl']['password'];
	$growl_name = $config['notifications']['growl']['name'];
	$growl_notification = $config['notifications']['growl']['notification_name'];
354

Ad Schellevis's avatar
Ad Schellevis committed
355 356 357 358 359 360 361 362 363 364 365 366
	if($growl_ip) {
	  $growl = new Growl($growl_ip, $growl_password, $growl_name);
		$growl->addNotification($growl_notification);
		$growl->register();
	}
}

/* Notify via remote methods only - not via GUI. */
function notify_all_remote($msg) {
	notify_via_smtp($msg);
	notify_via_growl($msg);
}