cbpadmin 5.25 KB
Newer Older
1
2
#!/usr/bin/perl
# Cluebringer administration tool
Wepongo's avatar
Wepongo committed
3
# Copyright (C) 2009-2015, AllWorldIT
4
# Copyright (C) 2008, LinuxRulz
Nigel Kukard's avatar
Nigel Kukard committed
5
#
6
7
8
9
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
Nigel Kukard's avatar
Nigel Kukard committed
10
#
11
12
13
14
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
Nigel Kukard's avatar
Nigel Kukard committed
15
#
16
17
18
19
20
21
22
23
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.


use strict;
use warnings;

24

25
use lib('/usr/local/lib/cbpolicyd-2.1','/usr/lib/cbpolicyd-2.1','/usr/lib64/cbpolicyd-2.1','awitpt');
26

27
28
29
30
31
32
33
use Config::IniFiles;
use Getopt::Long;

use cbp::logging;

use cbp::version;

Nigel Kukard's avatar
Nigel Kukard committed
34
35

print("PolicyD Admin Tool (Cluebringer) v".VERSION." - Copyright (c) 2009-2014 AllWorldIT\n");
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54

# Fire up commandline processing...
my %opts;
GetOptions(
	\%opts,
	"help",
	"config:s",
	"cleanup",
	"debug",
);

# Check for some args
if ($opts{'help'}) {
	displayHelp();
	exit 0;
}

# Set defaults
my $cfg;
55
$cfg->{'config_file'} = "/etc/cbpolicyd/cluebringer.conf";
56
57
58
59
60
61
62
63
64

# Check if we must override
if (defined($opts{'config'}) && $opts{'config'} ne "") {
	$cfg->{'config_file'} = $opts{'config'};
}

# Check config file exists
if (! -f $cfg->{'config_file'}) {
	print(STDERR "ERROR: No configuration file '".$cfg->{'config_file'}."' found!\n");
65
	displayHelp();
66
67
68
69
70
71
72
73
74
75
	exit 1;
}

# Use config file, ignore case
tie my %inifile, 'Config::IniFiles', (
		-file => $cfg->{'config_file'},
		-nocase => 1
) or die "Failed to open config file '".$cfg->{'config_file'}."': $!";
# Copy config
my %config = %inifile;
76
77
# FIXME: This now generates a warning as Config::Inifiles doesn't implement UNTIE
# untie(%inifile);
78
79


80
81
82
83
84
85
86
# Our fake server...
my $server;


#
# Check what to do
#
87
88
89

# We must cleanup
if ($opts{'cleanup'}) {
90
91
	loadModules();

92
93
94
95
96
97
98
99
100
	# Loop with modules
	foreach my $module ( sort { $b->{'priority'} <=> $a->{'priority'} }  @{$server->{'modules'}} ) {
		$server->log(LOG_INFO,"Module: " . $module->{'name'});
		# If we have a cleanup module, run it
		if (defined($module->{'cleanup'})) {
			$server->log(LOG_INFO,"  -> running cleanup...");
			$module->{'cleanup'}($server);
		}
	}
101
102
103
104
105
106

# Huh, nothing todo...
} else {
	print(STDERR "ERROR: Nothing to do!\n");
	displayHelp();
	exit 1;
107
108
109
}


110
111
#
# Misc functions
Nigel Kukard's avatar
Nigel Kukard committed
112
#
113
114


115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# Register plugin info
sub plugin_register {
	my ($self,$module,$info) = @_;


	# If no info, return
	if (!defined($info)) {
		$server->log(LOG_ERR,"Plugin info not found for module => $module");
		return -1;
	}

	# Set real module name & save
	$info->{'Module'} = $module;
	push(@{$self->{'modules'}},$info);

	# If we should, init the module
	if (defined($info->{'init'})) {
132
		$info->{'init'}($self);
133
134
135
136
137
138
139
	}


	return 0;
}


140
141
142
143
# Function to load our modules
sub loadModules
{
	# Pull in module list
144
145
	my $modules = $config{'server'}{'modules'};
	my @modulelist;
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
	if (ref($config{'server'}{'modules'}) eq "ARRAY") {
		foreach my $module (@{$modules}) {
			$module =~ s/\s+//g;
			# Skip comments
			next if ($module =~ /^#/);
			$module = "modules/$module";
			push(@modulelist,$module);
		}
	} else {
		my @splitModules = split(/\s+/,$config{'server'}{'modules'});
		foreach my $module (@splitModules) {
			# Skip comments
			next if ($module =~ /^#/);
			$module = "modules/$module";
			push(@modulelist,$module);
		}
162
	}
Nigel Kukard's avatar
Nigel Kukard committed
163

164
165
	# Emulate server
	$server = new cbpserver;
Nigel Kukard's avatar
Nigel Kukard committed
166
	$server->{'inifile'} = \%config;
167
168
	# Init everything
	$server->init();
Nigel Kukard's avatar
Nigel Kukard committed
169

170
	# Load modules
171
172
173
174
175
	foreach my $module (@modulelist) {
		# Split off dir and mod name
		$module =~ /^(\w+)\/(\w+)$/;
		my ($mod_dir,$mod_name) = ($1,$2);

176
177
		# Load module
		my $res = eval("
178
179
			use cbp::${mod_dir}::${mod_name};
			plugin_register(\$server,\"${mod_name}\",\$cbp::${mod_dir}::${mod_name}::pluginInfo);
180
181
182
183
184
185
186
187
188
189
190
191
		");
		if ($@ || (defined($res) && $res != 0)) {
			$server->log(LOG_ERR,"WARNING: Error loading plugin $module ($@)");
		}
	}
}






192
193
# Display help
sub displayHelp {
194
	print(<<EOF);
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214

Usage: $0 [args]
    --config=<file>        Configuration file
    --debug                Put into debug mode
    --cleanup              Cleanup database records

EOF
}




# Server emulation
package cbpserver;

use strict;
use warnings;

use cbp::logging;
use cbp::config;
Nigel Kukard's avatar
Nigel Kukard committed
215
216
use awitpt::db::dbilayer;
use awitpt::db::dblayer;
217
218
219
220
221
222
223
224

# Return oursevles
sub new
{
	my $class = shift;

	my $self = {
	};
Nigel Kukard's avatar
Nigel Kukard committed
225

226
227
228
229
230
231
232
233
234
235
236
237
	bless $self, $class;
	return $self;
};

sub init
{
	my $self = shift;

	# Init config
	cbp::config::Init($self);

	# Init system stuff
Nigel Kukard's avatar
Nigel Kukard committed
238
	$self->{'client'}->{'dbh'} = awitpt::db::dbilayer::Init($self,'cbp');
239
	if (!defined($self->{'client'}->{'dbh'})) {
Nigel Kukard's avatar
Nigel Kukard committed
240
		$self->log(LOG_WARN,"Failed to Initialize: ".awitpt::db::dbilayer::internalError()." ($$)");
241
242
243
244
245
246
247
		die;
	}
	if ($self->{'client'}->{'dbh'}->connect()) {
		$self->log(LOG_WARN,"Failed to connect to database: ".$self->{'client'}->{'dbh'}->Error()." ($$)");
		die;
	}
	# Setup database handle
Nigel Kukard's avatar
Nigel Kukard committed
248
	awitpt::db::dblayer::setHandle($self->{'client'}->{'dbh'});
249
250
251
252
253
254
255
}

sub log
{
	my ($self,$level,@msg) = @_;

	# FIXME: we shouldn't ignore $level
Nigel Kukard's avatar
Nigel Kukard committed
256
	print(@msg, "\n");
257
258
259
260
261
}



# vim: ts=4