| 1 | #!/usr/local/bin/perl | ||
| 2 | |||
| 3 | |||
| 4 | use strict; | ||
| 5 | use warnings; | ||
| 6 | |||
| 7 | use version 0.74; our $VERSION = qv( "v0.1.5" ); | ||
| 8 | |||
| 9 | use YAML; | ||
| 10 | use Getopt::Long; | ||
| 11 | use File::Basename qw/ dirname /; | ||
| 12 | |||
| 13 | BEGIN { | ||
| 14 | if ( -d '/opt/decency/lib' ) { | ||
| 15 | use lib '/opt/decency/lib'; | ||
| 16 | } | ||
| 17 | if ( -d '/opt/decency/locallib' ) { | ||
| 18 | use lib '/opt/decency/locallib'; | ||
| 19 | } | ||
| 20 | } | ||
| 21 | |||
| 22 | my %opt; | ||
| 23 | GetOptions( | ||
| 24 | "class|a=s" => \( $opt{ class } = "" ), | ||
| 25 | "config|c=s" => \( $opt{ config } = '/etc/decency/%s.yml' ), | ||
| 26 | "log-level|l=i" => \( $opt{ log_level } = 1 ), | ||
| 27 | "pid-file|p=s" => \( $opt{ pid } = "" ), | ||
| 28 | "port=i" => \( $opt{ port } ), | ||
| 29 | "host=s" => \( $opt{ host } ), | ||
| 30 | "help|h" => \( $opt{ help } = 0 ), | ||
| 31 | "maintenance" => \( $opt{ maintenance } = 0 ), | ||
| 32 | "print-stats" => \( $opt{ print_stats } = 0 ), | ||
| 33 | "print-sql" => \( $opt{ print_sql } = 0 ), | ||
| 34 | "export=s" => \( $opt{ export } = "" ), | ||
| 35 | "import=s" => \( $opt{ import } = "" ), | ||
| 36 | "import-replace" => \( $opt{ import_replace } = "" ), | ||
| 37 | "user|u=s" => \( $opt{ user } = "" ), | ||
| 38 | "group|g=s" => \( $opt{ group } = "" ), | ||
| 39 | "train-spam=s" => \( $opt{ train_spam } = "" ), | ||
| 40 | "train-ham=s" => \( $opt{ train_ham } = "" ), | ||
| 41 | "train-move=s" => \( $opt{ train_move } = "" ), | ||
| 42 | "train-remove" => \( $opt{ train_remove } = 0 ), | ||
| 43 | ); | ||
| 44 | |||
| 45 | die <<HELP if $opt{ help }; | ||
| 46 | |||
| 47 | Usage: $0 --class <classname> --config <configfile> --pidfile <pidfile> | ||
| 48 | |||
| 49 | --class | -a <policy|content-filter|syslog-parser> | ||
| 50 | What kind of server to start ? | ||
| 51 | policy = Mail::Decency::Policy | ||
| 52 | content-filter = Mail::Decency::ContentFilter | ||
| 53 | log-parser = Mail::Decency::LogParser | ||
| 54 | |||
| 55 | --config | -c <file> | ||
| 56 | Path to config .. | ||
| 57 | default: /etc/decency/<class>.yml | ||
| 58 | |||
| 59 | --pid-file | -p <file> | ||
| 60 | default: /tmp/<class>.pid | ||
| 61 | |||
| 62 | --log-level | -l <1..6> | ||
| 63 | the smaller the less verbose and vice versa, overwrite settings | ||
| 64 | in the config | ||
| 65 | |||
| 66 | --user | -u <uid | user name> | ||
| 67 | change to this user | ||
| 68 | |||
| 69 | --group | -g <gid | group name> | ||
| 70 | change to this user | ||
| 71 | |||
| 72 | --port <int> | ||
| 73 | optional port, overwrites the port settings in config | ||
| 74 | |||
| 75 | --host <inet address> | ||
| 76 | optional host address, overwrites the host settings in config | ||
| 77 | |||
| 78 | --maintenance | ||
| 79 | Run in maintenance mode and exit | ||
| 80 | This cleans up databases and so on | ||
| 81 | |||
| 82 | --print-stats | ||
| 83 | Print statistics and exit | ||
| 84 | |||
| 85 | --print-sql | ||
| 86 | Print SQL "CREATE *" statements in SQLite syntax | ||
| 87 | |||
| 88 | --export <path> | ||
| 89 | Exports all stored data in either a gziped tararchive or | ||
| 90 | to STDOUT ("-") | ||
| 91 | |||
| 92 | --import <path> | ||
| 93 | Imports exported databases back to decency. | ||
| 94 | |||
| 95 | --import-replace | ||
| 96 | Performces a replacive import which will remove all existing data | ||
| 97 | before. Default is additive. | ||
| 98 | |||
| 99 | --train-(spam|ham) <files> | ||
| 100 | For content filter only. Provide a list of files (eg /tmp/spam/*) which will | ||
| 101 | then be passed to the training methods of the enabled spam filters. | ||
| 102 | |||
| 103 | --train-move <dir> | ||
| 104 | Move file after training here | ||
| 105 | |||
| 106 | --train-remove | ||
| 107 | Delete file after training | ||
| 108 | |||
| 109 | --help | -h | ||
| 110 | this help | ||
| 111 | |||
| 112 | HELP | ||
| 113 | |||
| 114 | die "Provide --class <policy|content-filter|log-parser>\n" | ||
| 115 | unless $opt{ class } &&$opt{ class } =~ /^(?:policy|content\-filter|log\-parser)$/; | ||
| 116 | |||
| 117 | if ( $opt{ user } ) { | ||
| 118 | my $uid = $opt{ user } =~ /^\d+$/ | ||
| 119 | ? $opt{ user } | ||
| 120 | : getpwnam( $opt{ user } ) | ||
| 121 | ; | ||
| 122 | die "Cannot determine UID for '$opt{ user }'\n" | ||
| 123 | unless defined $uid; | ||
| 124 | $> = $uid; | ||
| 125 | } | ||
| 126 | |||
| 127 | if ( $opt{ group } ) { | ||
| 128 | my $gid = $opt{ group } =~ /^\d+$/ | ||
| 129 | ? $opt{ group } | ||
| 130 | : getgrnam( $opt{ group } ) | ||
| 131 | ; | ||
| 132 | die "Cannot determine GID for '$opt{ group }'\n" | ||
| 133 | unless defined $gid; | ||
| 134 | $) = $gid; | ||
| 135 | } | ||
| 136 | |||
| 137 | $opt{ config } = sprintf( $opt{ config }, $opt{ class } ); | ||
| 138 | die "Can't read from policy config file: $opt{ config }\n" | ||
| 139 | unless -f $opt{ config }; | ||
| 140 | |||
| 141 | # use class | ||
| 142 | my %map = qw/ | ||
| 143 | policy Policy | ||
| 144 | content-filter ContentFilter | ||
| 145 | log-parser LogParser | ||
| 146 | /; | ||
| 147 | my $class = "Mail::Decency::$map{ $opt{ class } }"; | ||
| 148 | eval "use $class; 1" or die "Cannot use load $opt{ class }: $@\n"; | ||
| 149 | |||
| 150 | |||
| 151 | |||
| 152 | # read config | ||
| 153 | my $config = YAML::LoadFile( $opt{ config } ); | ||
| 154 | |||
| 155 | # update log level | ||
| 156 | $config->{ logging } ||= { syslog => 1, console => 0, directory => undef }; | ||
| 157 | $config->{ logging }->{ log_level } = $opt{ log_level }; | ||
| 158 | |||
| 159 | # having other port ? | ||
| 160 | $config->{ server }->{ port } = $opt{ port } | ||
| 161 | if $opt{ port }; | ||
| 162 | $config->{ server }->{ host } = $opt{ host } | ||
| 163 | if $opt{ host }; | ||
| 164 | |||
| 165 | # create server | ||
| 166 | my $dir = dirname( $opt{ config } ); | ||
| 167 | |||
| 168 | |||
| 169 | $ENV{ NO_CHECK_DATABASE } = 1 | ||
| 170 | if $opt{ print_sql }; | ||
| 171 | |||
| 172 | my $server = $class->new( config => $config, config_dir => $dir ); | ||
| 173 | |||
| 174 | |||
| 175 | # perform maintenance | ||
| 176 | if ( $opt{ maintenance } || $opt{ print_stats } || $opt{ print_sql } || $opt{ export } || $opt{ import } ) { | ||
| 177 | $server->disable_logging; | ||
| 178 | |||
| 179 | if ( $opt{ maintenance } ) { | ||
| 180 | $server->maintenance; | ||
| 181 | } | ||
| 182 | |||
| 183 | # print out statistics | ||
| 184 | elsif ( $opt{ print_stats } ) { | ||
| 185 | $server->print_stats; | ||
| 186 | } | ||
| 187 | |||
| 188 | # print out statistics | ||
| 189 | elsif ( $opt{ print_sql } ) { | ||
| 190 | $server->print_sql; | ||
| 191 | } | ||
| 192 | |||
| 193 | # export database to file or STDOUT | ||
| 194 | elsif ( $opt{ export } ) { | ||
| 195 | $server->export_database( $opt{ export } ); | ||
| 196 | } | ||
| 197 | |||
| 198 | # print out statistics | ||
| 199 | elsif ( $opt{ import } ) { | ||
| 200 | my $replacive = $opt{ import_replace } ? 1 : 0; | ||
| 201 | $server->import_database( $opt{ import }, { replace => $replacive } ); | ||
| 202 | } | ||
| 203 | } | ||
| 204 | |||
| 205 | elsif ( $opt{ class } eq 'content-filter' && ( $opt{ train_spam } || $opt{ train_ham } ) ) { | ||
| 206 | $server->disable_logging; | ||
| 207 | |||
| 208 | # train SPAM | ||
| 209 | $server->train( { | ||
| 210 | spam => 1, | ||
| 211 | files => $opt{ train_spam }, | ||
| 212 | move => $opt{ train_move }, | ||
| 213 | remove => $opt{ train_remove }, | ||
| 214 | } ) if $opt{ train_spam }; | ||
| 215 | |||
| 216 | # train HAM | ||
| 217 | $server->train( { | ||
| 218 | ham => 1, | ||
| 219 | files => $opt{ train_ham }, | ||
| 220 | move => $opt{ train_move }, | ||
| 221 | remove => $opt{ train_remove }, | ||
| 222 | } ) if $opt{ train_ham }; | ||
| 223 | |||
| 224 | } | ||
| 225 | |||
| 226 | # just run the server | ||
| 227 | else { | ||
| 228 | |||
| 229 | $opt{ pid } ||= "/var/run/decency/$opt{ class }.pid"; | ||
| 230 | |||
| 231 | # write pid | ||
| 232 | if ( $opt{ pid } ) { | ||
| 233 | open my $fh, '>', $opt{ pid } | ||
| 234 | or die "Cannot open pid file '$opt{ pid }' for write: $!\n"; | ||
| 235 | print $fh $$; | ||
| 236 | close $fh; | ||
| 237 | } | ||
| 238 | |||
| 239 | # run ... | ||
| 240 | $server->run; | ||
| 241 | |||
| 242 | # remove pid file after going down | ||
| 243 | unlink( $opt{ pid } ) | ||
| 244 | if -f $opt{ pid }; | ||
| 245 | } | ||
| 246 | |||
| 247 | |||
| 248 | |||
| 249 | exit 0; |