File: nagzillad.pl

package info (click to toggle)
nagzilla 2.0-1.1
  • links: PTS
  • area: main
  • in suites: bookworm, bullseye, buster, sid
  • size: 504 kB
  • sloc: perl: 272; sh: 191; makefile: 17
file content (336 lines) | stat: -rwxr-xr-x 8,715 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
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
324
325
326
327
328
329
330
331
332
333
334
335
336
#!/usr/bin/perl

#(c)2007-2009 Hurricane Labs
#Author: Billford
#License: See LICENSE file GPL v2
#Nagzilla Jabber Relay Bot Server

#import our required Perl modules
require Config::Simple;
require Data::Dumper;
require Log::LogLite;
use IO::Socket;
use IO::Socket::INET;
use IO::Select;
use Net::Jabber qw(Client);
use Net::Jabber qw(Message);
use Net::Jabber qw(Protocol);
use Net::Jabber qw(Presence);

#Point me to your nagzillad.pl directory
$CONFDIR = "/etc/nagzillad";

# DO NOT EDIT ANYTHING BEYOND THIS POINT!!

$0.=''; # so that the init script is able to detect the process

$0.=''; # so that the init script is able to detect the process

##############Config File Setup#####################
Config::Simple->import_from('nagzillad.cfg', \%Config);
$cfg = new Config::Simple("$CONFDIR/nagzillad.cfg");

#Nagzillad Server Settings

$bindaddr = $cfg->param("BindAddr");
$bindport  = $cfg->param("BindPort");
$binduser = $cfg->param("BindUser");
$daemon = $cfg->param("Daemon");
$to_log = $cfg->param("To_Log");
$LOG_DIRECTORY = $cfg->param("Log_Directory");
$ERROR_LOG_LEVEL = $cfg->param("Error_Log_Level");

#Nagzillad Access Control
@allowedipaddr = $cfg->param("AllowedIPAddr");

#Jabber Server Settings

$jabberserver = $cfg->param("JabberServer");
$jabberport = $cfg->param("JabberPort");
$jabberdebug = $cfg->param("JabberDebug");
$jabbertls = $cfg->param("JabberTLS");
$jabberssl = $cfg->param("JabberSSL");
$jabbersasl = $cfg->param("JabberSASL");
$jabberuser = $cfg->param("JabberUser");
$jabberpass = $cfg->param("JabberPass");
$jabberresource = $cfg->param("JabberResource");
$jabbermaxwait = $cfg->param("JabberMaxWait");

#conference server settings

$confserver = $cfg->param("ConfServer");
@confrooms = $cfg->param("ConfRooms");
$confnick = $cfg->param("ConfNick");

@roominess = @confrooms;

###########End Config Setup#####################


my $log = new Log::LogLite( $LOG_DIRECTORY . "/nagzillad.log", $ERROR_LOG_LEVEL );

###########Function Setups#######################

# make_access_checker - returns a function which checks if an IP address
# should be allowed to connect to this nagzillad. 
#
# It takes a reference to an an array of IP addresses (dotted quad strings).
#
# It returns a function which takes an IP address (also a dotted quad
# string). That function will return 1 if the IP address is allowed and
# undef if it isn't.
#
sub make_access_checker {
	my $ok_addrs = shift;
	my %ok_addr_set = map { $_ => 1 } @{$ok_addrs};
	return sub {
		my $addr = shift;
		return $ok_addr_set{$addr};
	}
}

my $check_access = make_access_checker(\@allowedipaddr);

sub send_plain_chat {


 	our $message = Net::Jabber::Message->new();
        $message->SetMessage(
        "type" => $style,
        "to"   => $roomy =~ /@/ ? $roomy : "$roomy\@$confserver",
        "body" => $mess
	);

}#end send_plain_chat

sub send_color_chat {

	#make some pretty colors
if ($col eq "red"){
	$col = "\#ff0c00\;";
}elsif ($col eq "yellow"){
	$col = "\#fff00\;";
}elsif ($col eq "green"){
	$col = "\#00ff00\;";
}elsif ($col eq ""){
	$col = "\#000000\;";
};
 

 	our $message = Net::Jabber::Message->new();
	my $bod = "<body>hello</body><html xmlns='http://jabber.org/protocol/xhtml-im'><body xmlns='http://www.w3.org/1999/xhtml'><span style='font-family: Helvetica; font-size: medium; color: $col'>$mess</span></body></html>";

	$message->RemoveBody();
	$bod =~ s/^\^</</;

        $message->SetMessage(
        "type" => $style,
        "to"   => $roomy =~ /@/ ? $roomy : "$roomy\@$confserver"
	);
	$message->InsertRawXML($bod);

}#end send_color_chat

sub PresenceReceived
{
        my ($sid, $presence) = @_;

        my $from = $presence->GetFrom();
        my $type = $presence->GetType();

        return "" if ($type eq '');

        $log->write( "Received presence notification of type '$type' from '$from'", 5 );

        if ($type eq 'subscribe')
        {
                $connection->Subscription
                (
                        type => 'subscribed',
                        to => $from
                );
        }
        elsif ($type eq 'unsubscribe')
        {
                $connection->Subscription
                (
                        type => 'unsubscribed',
                        to => $from
                );
        }
        $connection->PresenceSend();
}#End PresenceReceived



##########End Function Setups###################

#taken from a Perl tutorial somewhere a million years ago so direct reference escapes me.
sub daemonize {
	chdir '/' or die "Can't chdir to /: $!";
	open STDIN,  '/dev/null'   or die "Can't read /dev/null: $!";
	open STDOUT, '>>/dev/null' or die "Can't write to /dev/null: $!";
	open STDERR, '>>/dev/null' or die "Can't write to /dev/null: $!";
	defined( my $pid = fork ) or die "Can't fork: $!";

	#setsid - from McMaster's salvia;
	if( $pid != 0 ) {
		open( PIDFILE, '>/var/run/nagzillad.pid' );
		print PIDFILE "$pid\n";
		close(PIDFILE);
		chown $uid, 0, "/var/run/nagzillad.pid";
		chmod oct("0644"), "/var/run/nagzillad.pid";

		# Change user ID - automagically
		my $uid = ( getpwnam('nobody') )[2];
		$> = $uid;
		exit(0);
	}
	setsid or die "Can't start a new session: $!";
	umask 0;
}

if ($daemon eq 1) {
	&daemonize;

	# Change user ID - automagically
	my $uid = ( getpwnam('nobody') )[2];
	$> = $uid;
}

#Let's make us a socket
$local = IO::Socket::INET->new(
	Proto	 => 'tcp',
	LocalAddr => $bindaddr,
	LocalPort => $bindport,
	Reuse	 => 1
) or die "$!";
$local->listen();	#Tell our socket to listen
#This is on by default with newer versions of IO::Socket but for bc let's turn it on anyway
$local->autoflush(1); 

if ($to_log eq 1) {
	$log->write( "Nagzillad Server Started", 5 );
}

#Start Jabber Stuff

# Creates a new connection to the jabber server
our $connection = Net::Jabber::Client->new( debugLevel => $jabberdebug );
$connection->Connect(
	"hostname" => $jabberserver,
	"port"	 => $jabberport,
	"tls"	  => $jabbertls,
	"ssl"	  => $jabberssl
) or die "Cannot connect ($!)\n";	#Return an error on failure

my @result;
if ($jabbersasl) {
	@result = $connection->AuthSend(
		"username" => $jabberuser,
		"password" => $jabberpass,
		"resource" => $jabberresource
	);

} else {
	@result = $connection->AuthIQAuth(
		"username" => $jabberuser,
		"password" => $jabberpass,
		"resource" => $jabberresource
	);
}

$connection->PresenceSend();

if ($result[0] ne "ok") {
	#Send an auth request, return on failure
	die "Ident/Auth with server failed: $result[0] - $result[1]\n";	
}

#Allow the daemon to hang out in as many rooms as needed (defined in config file)
#Depending on your Jabber server setup specifying a non-existent room will create that room

foreach $confs (@confrooms) {
	my $room = $connection->MUCJoin(
		room   => "$confs",
		server => "$confserver",	#Conference server
		nick   => "$confnick"
	);							  #Join a room
}

$connection->SetCallBacks
(
        presence => \&PresenceReceived
);


$readable_handles = new IO::Select();
$readable_handles->add($local);

while (1) {
	($new_readable) = IO::Select->select( $readable_handles, undef, undef, undef );

	foreach $sock (@$new_readable) {
		if ($sock == $local) {
			$new_sock = $sock->accept();
			my $client_name = $new_sock->peeraddr;
			$ip = inet_ntoa($client_name);

			next if not defined $new_sock;
			
			# check if the new peer's IP address is permitted via
			# the AllowedIPAddr conf statement.
			if ($check_access->($ip)) {
				$log->write("accepted connection from $ip",1);
				$readable_handles->add($new_sock);
			} else {
				$log->write("rejected connection from $ip - not in AllowedIPAddr",5);
				close $new_sock;
			}
		} else {
			$buf = <$sock>;

			$readable_handles->remove($sock);
			close $sock;

			if ($buf) {


				our ( $style, $roomy, $mess, $col ) = split( /\^/, $buf, 4 );

				 if ($style && $roomy && $mess) {
				send_color_chat( $style, $roomy, $mess, $col );

				$connection->Process();
				}


				}

				# dirty hack to allow RawXML: message text starting off with ^<,
				# strip off the ^
				if ($mess =~ /^\^</) {
					$log->write("--RawXML-- $style for $roomy ", 10);
					$message->RemoveBody();
					$mess =~ s/^\^</</;
					$message->InsertRawXML($mess);
				}

				#Send message from STDIN -- probably should add some error handling
				#There is some bug in at least ejabberd that causes messages to
				#be sent "offline". They get flushed on nagzilla restart
				#I don't know of a good way to check if this is happening yet
				#More to come - BM

				$connection->Send($message); 

				if ($to_log eq 1) {
					$log->write( "--$mess-- sent to $roomy from $ip", 5 );

				}
			}
		}

	}
	#Creates a sleep timer so many messages can be handled without issue
	#sleep(MAXWAIT);