File: steve.patch

package info (click to toggle)
nagzilla 2.0-1.1
  • links: PTS
  • area: main
  • in suites: bullseye, buster
  • size: 504 kB
  • sloc: perl: 272; sh: 191; makefile: 17
file content (345 lines) | stat: -rw-r--r-- 10,509 bytes parent folder | download | duplicates (6)
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
337
338
339
340
341
342
343
344
345
diff -ruN ../nagzilla-1.5.5-pre/nagzillad.pl ./nagzillad.pl
--- ../nagzilla-1.5.5-pre/nagzillad.pl	2009-05-27 19:56:52.000000000 +0000
+++ ./nagzillad.pl	2009-05-27 22:22:48.000000000 +0000
@@ -5,7 +5,6 @@
 #License: See LICENSE file GPL v2
 #Nagzilla Jabber Relay Bot Server
 
-
 #import our required Perl modules
 require Config::Simple;
 require Data::Dumper;
@@ -13,154 +12,199 @@
 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) ;
+use Net::Jabber qw(Client);
+use Net::Jabber qw(Message);
+use Net::Jabber qw(Protocol);
+use Net::Jabber qw(Presence);
 
-#bring in our configuration variables
 $CONFDIR = "/etc/nagzillad";
+
 # DO NOT EDIT ANYTHING BEYOND THIS POINT!!
 do "$CONFDIR/nagzillad-config.pl";
 
-my $log = new Log::LogLite($LOG_DIRECTORY."/nagzillad.log", $ERROR_LOG_LEVEL);
+my $log = new Log::LogLite( $LOG_DIRECTORY . "/nagzillad.log", $ERROR_LOG_LEVEL );
+
+# 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);
 
 
-#taken from a Perl tutorial somewhere a million years ago so direct reference escapes me. 
+#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";
+	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];
+		my $uid = ( getpwnam('nobody') )[2];
 		$> = $uid;
-                exit(0);
-        }
-    setsid                    or die "Can't start a new session: $!";
-    umask 0;
+		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;
-}
+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
-$local->autoflush(1); #This is on by default with newer versions of IO::Socket but for bc let's turn it on anyway
-
-if ($to_log eq 1) { 
-
-$log->write("Nagzillad Server Started", 5);
+$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
 
-	my $connection = Net::Jabber::Client -> new( debugLevel => $jabberdebug ); #Creates a new connection to the jabber server
-        $connection -> Connect( "hostname" => $jabberserver, "port" => $jabberport, "tls" => $jabbertls, "ssl" => $jabberssl )  or die "Cannot connect ($!)\n"; #Return an error on failure
+# Creates a new connection to the jabber server
+my $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
+	);
+}
+
+if ($result[0] ne "ok") {
+	#Send an auth request, return on failure
+	die "Ident/Auth with server failed: $result[0] - $result[1]\n";	
+}
 
-	my @result;
-   if ( $jabbersasl ) {
-       @result = $connection->AuthSend( "username" => $jabberuser, "password" => $jabberpass, "resource" => $jabberresource );
-
-   }
-   else {
-       @result = $connection->AuthIQAuth( "username" => $jabberuser, "password" => $jabberpass, "resource" => $jabberresource );
-
-   }
-   
-   if ($result[0] ne "ok") {
-     die "Ident/Auth with server failed: $result[0] - $result[1]\n"; #Send an auth request, return on failure
-   }
 #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
+	my $room = $connection->MUCJoin(
+		room   => "$confs",
+		server => "$confserver",	#Conference server
+		nick   => "$confnick"
+	);							  #Join a room
 }
 
 $readable_handles = new IO::Select();
 $readable_handles->add($local);
 
 while (1) {
-	($new_readable) = IO::Select->select($readable_handles, undef, undef, undef);
+	($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) {
+
+				my ( $style, $roomy, $mess ) = split( /\^/, $buf, 3 );
+
+				my $message = Net::Jabber::Message->new();
+				if ($style eq "room") {
+					$message->SetMessage(
+						"type" => 'groupchat',
+						"to"   => $roomy =~ /@/ ? $roomy : "$roomy\@$confserver",
+						"type" => 'chat',
+						"body" => $mess
+					);
+
+				}
+				else {
+					$message->SetMessage(
+						"to" => $roomy =~ /@/ ? $roomy: "$roomy\@$jabberserver",
+						"subject" => "Monitoring Alert",
+						"type"	=> 'chat',
+						"body"	=> "$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 );
 
-foreach $sock (@$new_readable) {
-	if ($sock == $local){
-		$new_sock = $sock->accept();
-	 	my $client_name = $new_sock->peeraddr;
-	    $ip = inet_ntoa($client_name);
-		#print "$ip \n";
-	next if not defined $new_sock;
-		$readable_handles->add($new_sock);
-	}else{
-  if (grep {$_ eq $ip} @allowedipaddr) {
-  
-		$buf = <$sock>;
-
-		$readable_handles->remove($sock);
-		close $sock;
-		
-		if ($buf) {
-
-my ($style, $roomy, $mess) = split(/\^/, $buf, 3);
-
-	my $message = Net::Jabber::Message->new();
-	if ($style eq "room") { 
-		$message->SetMessage( "type"	=> 'groupchat',
-							  "to"		=> $roomy =~ /@/ ? $roomy : "$roomy\@$confserver",
-							  "type"	=> 'chat',
-							  "body"	=> $mess );
-
-				} else { 
-	         $message->SetMessage ( "to"		=> $roomy =~ /@/ ? $roomy : "$roomy\@$jabberserver",
-					"subject"	=> "Monitoring Alert",
-					"type"		=> 'chat',
-					"body" 		=> "$mess" ); 
-
-				} 
-	$connection->Send($message); #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 
-				     
-	if ($to_log eq 1) {
-        $log->write("--$mess-- sent to $roomy from $ip", 5);
-        
+				}
+			}
 		}
-	
-}
-	
-	} 
-}
 
-}
-        sleep (MAXWAIT); #Creates a sleep timer so many messages can be handled without issue
-	
 	}
-	
\ No newline at end of file
+	#Creates a sleep timer so many messages can be handled without issue
+	sleep(MAXWAIT);	
+}
+
diff -ruN ../nagzilla-1.5.5-pre/README ./README
--- ../nagzilla-1.5.5-pre/README	2009-05-27 20:02:59.000000000 +0000
+++ ./README	2009-05-27 21:18:06.000000000 +0000
@@ -23,10 +23,10 @@
 
     IO::Socket
     IO::Socket::INET
-    Net::Jabber 
-    Config::Simple
+    Net::Jabber       (Ubuntu package: libnet-jabber-perl)
+    Config::Simple    (Ubuntu package: libconfig-simple-perl)
     Data::Dumper
-    Log::LogLite
+    Log::LogLite      (Ubuntu package: liblog-loglite-perl)
 
 Nagzillad Installation