|
| 1 | +<?php |
| 2 | +/* |
| 3 | + * andwatch.inc |
| 4 | + * |
| 5 | + * part of pfSense (https://www.pfsense.org) |
| 6 | + * Copyright (c) 2025 Denny Page |
| 7 | + * All rights reserved. |
| 8 | + * |
| 9 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 10 | + * you may not use this file except in compliance with the License. |
| 11 | + * You may obtain a copy of the License at |
| 12 | + * |
| 13 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 14 | + * |
| 15 | + * Unless required by applicable law or agreed to in writing, software |
| 16 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 17 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 18 | + * See the License for the specific language governing permissions and |
| 19 | + * limitations under the License. |
| 20 | + */ |
| 21 | + |
| 22 | +require_once("config.inc"); |
| 23 | +require_once("functions.inc"); |
| 24 | +require_once("util.inc"); |
| 25 | +require_once("service-utils.inc"); |
| 26 | + |
| 27 | +const ANDWATCH_SVC_NAME = 'andwatchd'; |
| 28 | +const ANDWATCH_RC_FILE = '/usr/local/etc/rc.d/andwatch.sh'; |
| 29 | +const ANDWATCHD_CMD = '/usr/local/bin/andwatchd'; |
| 30 | +const ANDWATCHD_NOTIFY_CMD = '/usr/local/pkg/andwatch-notify.php'; |
| 31 | +const ANDWATCHD_PID_PREFIX = '/var/run/andwatch-'; |
| 32 | +const ANDWATCH_MA_DB_FILE = '/var/db/andwatch/ma_db.sqlite'; |
| 33 | +const ANDWATCH_UPDATE_MA_CMD = '/usr/local/bin/andwatch-update-ma'; |
| 34 | +const ANDWATCH_QUERY_CMD = '/usr/local/bin/andwatch-query'; |
| 35 | + |
| 36 | +$shortcut_section = 'andwatch'; |
| 37 | + |
| 38 | +// Is ANDwatch enabled? |
| 39 | +function andwatch_enabled() { |
| 40 | + return (config_get_path('installedpackages/andwatch/enable', false)); |
| 41 | +} |
| 42 | + |
| 43 | + |
| 44 | +// Write the rc file |
| 45 | +function andwatch_write_rcfile() { |
| 46 | + // Get the current configuration |
| 47 | + $current_config = config_get_path('installedpackages/andwatch'); |
| 48 | + |
| 49 | + // Get the real interface names |
| 50 | + $interfaces = array(); |
| 51 | + foreach (array_filter(explode(',', array_get_path($current_config, 'active_interfaces', ''))) as $interface) { |
| 52 | + $interface_name = get_real_interface($interface); |
| 53 | + if (!isset($interface_name)) { |
| 54 | + continue; |
| 55 | + } |
| 56 | + $interfaces[$interface] = $interface_name; |
| 57 | + } |
| 58 | + |
| 59 | + // Ensure the assignments database exists |
| 60 | + $start = "if [ \! -f " . ANDWATCH_MA_DB_FILE . " ]\n"; |
| 61 | + $start .= "\tthen\n"; |
| 62 | + $start .= "\t\techo Updating MAC Assignments database\n"; |
| 63 | + $start .= "\t\t" . ANDWATCH_UPDATE_MA_CMD . "\n"; |
| 64 | + $start .= "\tfi\n\n"; |
| 65 | + |
| 66 | + // Individual daemons |
| 67 | + $start .= "\techo Starting ANDwatch daemons"; |
| 68 | + foreach ($interfaces as $interface => $real_interface) { |
| 69 | + // Basic configuration with syslog and pid file |
| 70 | + $cmd = ANDWATCHD_CMD . " -s -p " . ANDWATCHD_PID_PREFIX . "{$real_interface}.pid"; |
| 71 | + |
| 72 | + // Notifications |
| 73 | + if (array_get_path($current_config, "interfaces/{$interface}/notifications", false)) { |
| 74 | + $cmd .= " -n " . ANDWATCHD_NOTIFY_CMD; |
| 75 | + } |
| 76 | + |
| 77 | + // Expiration (andwatchd default is 30) |
| 78 | + $expiration = array_get_path($current_config, "interfaces/{$interface}/expiration", '30'); |
| 79 | + if (is_numericint($expiration) && ($expiration != '30')) { |
| 80 | + $cmd .= " -O {$expiration}"; |
| 81 | + } |
| 82 | + |
| 83 | + // PCAP filter |
| 84 | + $filter = null; |
| 85 | + switch (array_get_path($current_config, "interfaces/{$interface}/pcap_filter", 'none')) { |
| 86 | + case 'link-local': |
| 87 | + $filter = "'not net 169.254.0.0/16 and not net fe80::0/10'"; |
| 88 | + break; |
| 89 | + case 'link-local-unique': |
| 90 | + $filter = "'not net 169.254.0.0/16 and not net fe80::0/10 and not net fc00::0/7'"; |
| 91 | + break; |
| 92 | + case 'custom': |
| 93 | + $filter = escapeshellarg(trim(array_get_path($current_config, "interfaces/{$interface}/custom_filter"))); |
| 94 | + break; |
| 95 | + } |
| 96 | + if (!empty($filter)) { |
| 97 | + $cmd .= " -F " . $filter; |
| 98 | + } |
| 99 | + |
| 100 | + $cmd .= " {$real_interface}"; |
| 101 | + $start .= "\n\t{$cmd}"; |
| 102 | + } |
| 103 | + |
| 104 | + // Write the rc file |
| 105 | + $stop = '/usr/bin/killall ' . ANDWATCH_SVC_NAME; |
| 106 | + write_rcfile(array( |
| 107 | + "file" => "andwatch.sh", |
| 108 | + "start" => $start, |
| 109 | + "stop" => $stop |
| 110 | + ) |
| 111 | + ); |
| 112 | +} |
| 113 | + |
| 114 | + |
| 115 | +// Sync the config |
| 116 | +function andwatch_sync_config() { |
| 117 | + // Stop the service if it is currently running |
| 118 | + if (is_service_running(ANDWATCH_SVC_NAME)) { |
| 119 | + log_error("Stopping service ANDwatch"); |
| 120 | + stop_service(ANDWATCH_SVC_NAME); |
| 121 | + } |
| 122 | + |
| 123 | + // If the service is now disabled, remove the cron job and rc file |
| 124 | + if (!andwatch_enabled()) { |
| 125 | + install_cron_job(ANDWATCH_UPDATE_MA_CMD, false); |
| 126 | + unlink_if_exists(ANDWATCH_RC_FILE); |
| 127 | + return; |
| 128 | + } |
| 129 | + |
| 130 | + // Set a cron job to update the MAC registration database |
| 131 | + $cron_already_installed = false; |
| 132 | + foreach (config_get_path('cron/item', []) as $item) { |
| 133 | + if ($item['command'] == ANDWATCH_UPDATE_MA_CMD) { |
| 134 | + $cron_already_installed = true; |
| 135 | + break; |
| 136 | + } |
| 137 | + } |
| 138 | + if ($cron_already_installed == false) { |
| 139 | + install_cron_job(ANDWATCH_UPDATE_MA_CMD, true, rand(0,59), rand(0,23), '1,15', '*', '*', 'root', true); |
| 140 | + } |
| 141 | + |
| 142 | + // Write the rc file |
| 143 | + andwatch_write_rcfile(); |
| 144 | + |
| 145 | + // Take no further action during platform boot |
| 146 | + if (is_platform_booting()) { |
| 147 | + return; |
| 148 | + } |
| 149 | + |
| 150 | + // Start the service |
| 151 | + log_error("Starting service ANDwatch"); |
| 152 | + start_service(ANDWATCH_SVC_NAME); |
| 153 | +} |
| 154 | + |
| 155 | + |
| 156 | +// Clean up on deinstall |
| 157 | +function andwatch_deinstall_command() { |
| 158 | + if (is_service_running(ANDWATCH_SVC_NAME)) { |
| 159 | + log_error("Stopping service ANDwatch"); |
| 160 | + stop_service(ANDWATCH_SVC_NAME); |
| 161 | + } |
| 162 | + |
| 163 | + install_cron_job(ANDWATCH_UPDATE_MA_CMD, false); |
| 164 | + unlink_if_exists(ANDWATCH_RC_FILE); |
| 165 | +} |
| 166 | + |
| 167 | + |
| 168 | +// Run a query |
| 169 | +function andwatch_query_interface($ifname, $all = false) |
| 170 | +{ |
| 171 | + $entries = array(); |
| 172 | + |
| 173 | + // Build the query command |
| 174 | + $cmd = ANDWATCH_QUERY_CMD; |
| 175 | + if ($all) { |
| 176 | + $cmd .= ' -a'; |
| 177 | + } |
| 178 | + $cmd .= ' ' . get_real_interface($ifname); |
| 179 | + |
| 180 | + // Run the query |
| 181 | + $pipe = popen($cmd, 'r'); |
| 182 | + if ($pipe) { |
| 183 | + while ($line = fgets($pipe)) { |
| 184 | + list($date, $time, $age, $hostname, $ipaddr, $hwaddr, $org) = sscanf(trim($line), '%s %s %s %s %s %s %[^$]s'); |
| 185 | + $entry = [ |
| 186 | + 'datetime' => "$date $time", |
| 187 | + 'age' => $age, |
| 188 | + 'hostname' => $hostname, |
| 189 | + 'ipaddr' => $ipaddr, |
| 190 | + 'hwaddr' => $hwaddr, |
| 191 | + 'org' => $org |
| 192 | + ]; |
| 193 | + $entries[] = $entry; |
| 194 | + } |
| 195 | + pclose($pipe); |
| 196 | + } |
| 197 | + |
| 198 | + return $entries; |
| 199 | +} |
| 200 | + |
| 201 | +?> |
0 commit comments