From 1a18e5fc2c3678e9a4a3049b9289e417ce174c7e Mon Sep 17 00:00:00 2001 From: "lpsolit%gmail.com" <> Date: Sat, 3 Jun 2006 19:23:12 +0000 Subject: Bug 340104: Move Bugzilla::Auth::get_netaddr() in Util.pm - Patch by Frédéric Buclin r/a=justdave MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Bugzilla/Util.pm | 39 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 38 insertions(+), 1 deletion(-) (limited to 'Bugzilla/Util.pm') diff --git a/Bugzilla/Util.pm b/Bugzilla/Util.pm index 28f5e71bc..98a3065cf 100644 --- a/Bugzilla/Util.pm +++ b/Bugzilla/Util.pm @@ -34,7 +34,7 @@ use base qw(Exporter); detaint_signed html_quote url_quote value_quote xml_quote css_class_quote - i_am_cgi + i_am_cgi get_netaddr lsearch max min diff_arrays diff_strings trim wrap_comment find_wrap_point @@ -396,6 +396,27 @@ sub clean_text { return trim($dtext); } +sub get_netaddr { + my $ipaddr = shift; + + # Check for a valid IPv4 addr which we know how to parse + if (!$ipaddr || $ipaddr !~ /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/) { + return undef; + } + + my $addr = unpack("N", pack("CCCC", split(/\./, $ipaddr))); + + my $maskbits = Param('loginnetmask'); + + # Make Bugzilla ignore the IP address if loginnetmask is set to 0 + return "0.0.0.0" if ($maskbits == 0); + + $addr >>= (32-$maskbits); + + $addr <<= (32-$maskbits); + return join(".", unpack("CCCC", pack("N", $addr))); +} + 1; __END__ @@ -425,6 +446,7 @@ Bugzilla::Util - Generic utility functions for bugzilla # Functions that tell you about your environment my $is_cgi = i_am_cgi(); + $net_addr = get_netaddr($ip_addr); # Functions for searching $loc = lsearch(\@arr, $val); @@ -540,12 +562,27 @@ is kept separate from html_quote partly for compatibility with previous code Converts the %xx encoding from the given URL back to its original form. +=back + +=head2 Environment and Location + +Functions returning information about your environment or location. + +=over 4 + =item C Tells you whether or not you are being run as a CGI script in a web server. For example, it would return false if the caller is running in a command-line script. +=item C + +Given an IP address, this returns the associated network address, using +C as the netmask. This can be used to obtain data +in order to restrict weak authentication methods (such as cookies) to +only some addresses. + =back =head2 Searching -- cgit v1.2.3-24-g4f1b