1 package FS::Misc::FixIPFormat;
4 use FS::Record qw(dbh qsearchs);
5 use FS::upgrade_journal;
9 FS::Misc::FixIPFormat - Functions to repair bad IP address input
13 Provides functions for freeside_upgrade to check IP address storage for
14 user-entered leading 0's in IP addresses. When read from database, NetAddr::IP
15 would treat the number as octal isntead of decimal. If a user entered
16 10.0.0.052, this may get invisibly translated to 10.0.0.42 when exported.
19 Tied to freeside_upgrade with journal name TABLE__fixipformat
27 # require, not use - this module is only run once
28 require FS::Misc::FixIPFormat;
30 my $error = FS::Misc::FixIPFormat::fix_bad_addresses_in_table(
31 'svc_broadband', 'svcnum', 'ip_addr'
33 die "oh no!" if $error;
35 =head2 fix_bad_addresses_in_table TABLE, ID_COLUMN, IP_COLUMN
37 $error = fix_bad_addresses_in_table( 'svc_broadband', 'svcnum', 'ip_addr' );
41 sub fix_bad_addresses_in_table {
43 return if FS::upgrade_journal->is_done("${table}__fixipformat");
44 for my $id ( find_bad_addresses_in_table( @_ )) {
45 if ( my $error = fix_ip_for_record( $id, @_ )) {
46 die "fix_bad_addresses_in_table(): $error";
49 FS::upgrade_journal->set_done("${table}__fixipformat");
53 =head2 find_bad_addresses_in_table TABLE, ID_COLUMN, IP_COLUMN
55 @id = find_bad_addresses_in_table( 'svc_broadband', 'svcnum', 'ip_addr' );
59 sub find_bad_addresses_in_table {
60 my ( $table, $id_col, $ip_col ) = @_;
63 # using DBI directly for performance
65 SELECT $id_col, $ip_col
67 WHERE $ip_col IS NOT NULL
69 my $sth = dbh->prepare( $sql_statement ) || die "SQL ERROR ".dbh->errstr;
70 $sth->execute || die "SQL ERROR ".dbh->errstr;
71 while ( my $row = $sth->fetchrow_hashref ) {
72 push @fix_ids, $row->{ $id_col }
73 if $row->{ $ip_col } =~ /[\.^]0\d/;
78 =head2 fix_ip_for_record ID, TABLE, ID_COLUMN, IP_COLUMN
80 Attempt to strip the leading 0 from a stored IP address record. If
81 the corrected IP address would be a duplicate of another record in the
82 same table, thow an exception.
84 $error = fix_ip_for_record( 1001, 'svc_broadband', 'svcnum', 'ip_addr', );
88 sub fix_ip_for_record {
89 my ( $id, $table, $id_col, $ip_col ) = @_;
91 my $row = qsearchs($table, {$id_col => $id})
92 || die "Error finding $table record for id $id";
94 my $ip = $row->getfield( $ip_col );
95 my $fixed_ip = join( '.',
100 return undef unless $ip ne $fixed_ip;
102 if ( my $dupe_row = qsearchs( $table, {$ip_col => $fixed_ip} )) {
103 if ( $dupe_row->getfield( $id_col ) != $row->getfield( $id_col )) {
104 # Another record in the table has this IP address
105 # Eg one ip is provisioned as 10.0.0.51 and another is
106 # provisioned as 10.0.0.051. Cannot auto-correct by simply
107 # trimming leading 0. Die, let support decide how to fix.
109 die "Invalid IP address could not be auto-corrected - ".
110 "($table - $id_col = $id, $ip_col = $ip) ".
111 "colission with another reocrd - ".
112 "($table - $id_col = ".$dupe_row->getfield( $id_col )." ".
113 "$ip_col = ",$dupe_row->getfield( $ip_col )." ) - ".
114 "The entry must be corrected to continue";
118 warn "Autocorrecting IP address problem for ".
119 "($table - $id_col = $id, $ip_col = $ip) $fixed_ip\n";
120 $row->setfield( $ip_col, $fixed_ip );