Changeset 798
- Timestamp:
- 12/23/15 15:46:13 (9 years ago)
- Location:
- trunk
- Files:
-
- 6 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/cgi-bin/IPDB.pm
r795 r798 67 67 # Mapping hash for pooltype -> poolip-as-netblock conversions 68 68 my %poolmap = (sd => 'en', cd => 'cn', dp => 'cn', mp => 'cn', wp => 'cn', ld => 'in', ad => 'in', bd => 'in'); 69 70 # Backup fields, since we iterate over the set regularly 71 our @backupfields = qw(brand model type src user vpass epass port ip); 69 72 70 73 # Friendly display strings for merge scopes … … 1472 1475 my $backupid = 0; 1473 1476 if ($args{backup}) { 1474 my $ sql = "INSERT INTO backuplist (";1477 my $bksql = "INSERT INTO backuplist ("; 1475 1478 my @bkvals; 1476 for my $bk (qw(brand model type port src user vpass epass)) { 1479 my @bkfields; 1480 for my $bk (@backupfields) { 1477 1481 if ($args{"bk$bk"}) { 1478 $sql .= "bk$bk,";1482 push @bkfields, "bk$bk"; 1479 1483 push @bkvals, $args{"bk$bk"}; 1480 1484 } 1481 1485 } 1482 $ sql .= "ip) VALUES (".('?,' x scalar(@bkvals))."?)";1483 $dbh->do($ sql, undef, @bkvals, $args{cidr});1486 $bksql .= join(',',@bkfields).") VALUES (".join(',', map {'?'} @bkfields).")"; 1487 $dbh->do($bksql, undef, @bkvals); 1484 1488 ($backupid) = $dbh->selectrow_array("SELECT currval('backuplist_backup_id_seq')"); 1485 1489 } … … 1523 1527 $msg = "Unable to allocate $args{cidr} as '$disp_alloctypes{$args{type}}'"; 1524 1528 1529 # backup 1530 my $backupid = 0; 1531 if ($args{backup}) { 1532 if (!$args{bkip}) { 1533 # check for /32-ness. no point in skipping /32 "netblocks", because they already single IPs 1534 die "Backup data set on a netblock requires a backup IP\n" unless $args{cidr} =~ m{/32$}; 1535 $args{bkip} = $args{cidr}; 1536 } 1537 my $bksql = "INSERT INTO backuplist ("; 1538 my @bkfields; 1539 my @bkvals; 1540 for my $bk (@backupfields) { 1541 if ($args{"bk$bk"}) { 1542 push @bkfields, "bk$bk"; 1543 push @bkvals, $args{"bk$bk"}; 1544 } 1545 } 1546 $bksql .= join(',',@bkfields).") VALUES (".join(',',map {'?'} @bkfields).")"; 1547 $dbh->do($bksql, undef, @bkvals); 1548 ($backupid) = $dbh->selectrow_array("SELECT currval('backuplist_backup_id_seq')"); 1549 } # $args{backup} 1550 1525 1551 # Insert the allocations entry 1526 1552 $dbh->do("INSERT INTO allocations ". 1527 "(cidr,parent_id,master_id,vrf,vlan,custid,type,city,description,notes,circuitid,privdata,rdns )".1528 " VALUES (?,?,?,?,?,?,?,?,?,?,?,?,? )", undef,1553 "(cidr,parent_id,master_id,vrf,vlan,custid,type,city,description,notes,circuitid,privdata,rdns,backup_id)". 1554 " VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?)", undef, 1529 1555 ($args{cidr}, $fbparent, $fbmaster, $args{vrf}, $args{vlan}, $args{custid}, $args{type}, $args{city}, 1530 $args{desc}, $args{notes}, $args{circid}, $args{privdata}, $args{rdns} ) );1556 $args{desc}, $args{notes}, $args{circid}, $args{privdata}, $args{rdns}, $backupid) ); 1531 1557 my ($bid) = $dbh->selectrow_array("SELECT currval('allocations_id_seq')"); 1532 1558 … … 1845 1871 } 1846 1872 1847 # check on the backup data first. we may need to tweak the main update's parameters.1873 # backup 1848 1874 if (!defined($args{ignorebk})) { 1849 if ($args{backupfields}) { 1850 my @bkfields; 1851 my @bkvals; 1852 for my $bk (qw(brand model type src user vpass epass)) { 1853 if ($binfo->{"bk$bk"} ne $args{"bk$bk"}) { 1854 push @bkfields, "bk$bk = ?"; 1855 push @bkvals, $args{"bk$bk"}; 1875 # backup data considered "restricted"; caller should set this flag if user does not have 's' permission 1876 1877 my $backupid = $binfo->{hasbk}; 1878 if (!$binfo->{hasbk}) { 1879 if ($args{backup}) { 1880 # failure mode: backup data on netblock with no IP set 1881 if (!$args{bkip}) { 1882 # check for /32-ness. no point in skipping /32 "netblocks", because they already single IPs 1883 die "Backup data set on a netblock requires a backup IP\n" unless $binfo->{block} =~ m{/32$}; 1884 $args{bkip} = $binfo->{block}; 1856 1885 } 1886 # insert new backup record since we don't have one 1887 my $bksql = "INSERT INTO backuplist ("; 1888 my @bkfields; 1889 my @bkvals; 1890 for my $bk (@backupfields) { 1891 if ($args{"bk$bk"}) { 1892 push @bkfields, "bk$bk"; 1893 push @bkvals, $args{"bk$bk"}; 1894 } 1895 } 1896 $bksql .= join(',',@bkfields).") VALUES (".join(',', map {'?'} @bkfields).")"; 1897 $dbh->do($bksql, undef, @bkvals); 1898 ($backupid) = $dbh->selectrow_array("SELECT currval('backuplist_backup_id_seq')"); 1899 # add the backup ID to the update 1900 push @vallist, $backupid; 1901 $sql .= " = ?, backup_id"; 1857 1902 } 1858 $dbh->do("UPDATE backuplist SET ".join(',', @bkfields)." WHERE backup_id = ?", 1859 undef, @bkvals, $binfo->{hasbk}) 1860 if @bkfields; 1903 1904 } else { # !$binfo->{hasbk} 1905 1906 # allocation already has backup data 1907 if ($args{backup}) { 1908 if (!$args{bkip}) { 1909 # check for /32-ness. no point in skipping /32 "netblocks", because they are already single IPs 1910 die "Backup data set on a netblock requires a backup IP\n" unless $binfo->{block} =~ m{/32$}; 1911 $args{bkip} = $binfo->{block}; 1912 } 1913 my @bkfields; 1914 my @bkvals; 1915 for my $bk (@backupfields) { 1916 no warnings qw( uninitialized ); 1917 if ($binfo->{"bk$bk"} ne $args{"bk$bk"}) { 1918 push @bkfields, "bk$bk = ?"; 1919 push @bkvals, $args{"bk$bk"}; 1920 } 1921 } 1922 1923 $dbh->do("UPDATE backuplist SET ".join(',', @bkfields)." WHERE backup_id = ?", 1924 undef, @bkvals, $binfo->{hasbk}) 1925 if @bkfields; 1861 1926 ##todo: keep historic changes for $timeperiod, by adding a backref ID field, and on updates adding a new backup 1862 1927 # record instead of updating the existing one. should probably check if new==old so we don't do needless updates 1863 1928 # in that case... 1864 } else { 1865 if ($binfo->{hasbk}) { 1866 # had backup data, no longer checked - delete backup entry 1867 $dbh->do("DELETE FROM backuplist WHERE backup_id = ?", undef, $binfo->{hasbk}); 1868 $sql .= " = ?, backup_id"; 1869 push @vallist, 0; 1929 } else { 1930 if ($binfo->{hasbk}) { 1931 # had backup data, no longer checked - delete backup entry 1932 $dbh->do("DELETE FROM backuplist WHERE backup_id = ?", undef, $binfo->{hasbk}); 1933 $sql .= " = ?, backup_id"; 1934 push @vallist, 0; 1935 } 1870 1936 } 1871 } 1872 } 1937 } # $binfo->{hasbk} defined 1938 } # if !args{ignorebk} 1873 1939 1874 1940 # append another SQL fragment … … 3027 3093 ## IPDB::getBlockData() 3028 3094 # Get CIDR or IP, custid, type, city, circuit ID, description, notes, modification time, 3029 # private/restricted data, for a CIDR block or pool IP3095 # private/restricted data, and backup fields, for a CIDR block or pool IP 3030 3096 # Also returns SWIP status flag for CIDR blocks or pool netblock for IPs 3031 3097 # Takes the block ID or IP to look up and an optional flag to indicate a pool IP lookup … … 3045 3111 privdata, vlan, rdns); 3046 3112 my $bkfields = q(b.backup_id AS hasbk, b.bkbrand, b.bkmodel, b.bktype, b.bkport, b.bksrc, 3047 b.bkuser, b.bkvpass, b.bkepass );3113 b.bkuser, b.bkvpass, b.bkepass, b.bkip); 3048 3114 3049 3115 if ($type eq 'i') { … … 3051 3117 SELECT p.id, p.ip AS block, p.city, p.vrf, p.parent_id, p.master_id, $commonfields, 3052 3118 d.zone >> p.ip AS revavail, 3053 3119 $bkfields 3054 3120 FROM poolips p 3055 3121 LEFT JOIN dnsavail d ON p.master_id = d.parent_alloc AND p.ip << d.zone 3056 3122 LEFT JOIN backuplist b ON p.backup_id = b.backup_id 3057 3123 WHERE id = ? 3058 3124 ), undef, ($id) ); … … 3062 3128 SELECT a.id, a.cidr AS block, a.city, a.vrf, a.parent_id, a.master_id, swip, $commonfields, 3063 3129 f.cidr AS reserve, f.id as reserve_id, 3064 d.zone >>= a.cidr AS revavail, d.zone << a.cidr AS revpartial 3130 d.zone >>= a.cidr AS revavail, d.zone << a.cidr AS revpartial, 3131 $bkfields 3065 3132 FROM allocations a 3066 3133 LEFT JOIN freeblocks f ON a.id=f.reserve_for 3067 3134 LEFT JOIN dnsavail d ON a.master_id = d.parent_alloc AND (a.cidr <<= d.zone OR a.cidr >> d.zone) 3135 LEFT JOIN backuplist b ON a.backup_id = b.backup_id 3068 3136 WHERE a.id = ? 3069 3137 ), undef, ($id) ); -
trunk/cgi-bin/main.cgi
r793 r798 676 676 $page->param(bkepass => $q->escapeHTML($webvar{bkepass})); 677 677 $page->param(bkport => $webvar{bkport}); 678 $page->param(bkip => $webvar{bkip}); 678 679 } 679 680 … … 739 740 if ($webvar{backupfields}) { 740 741 $insert_args{backup} = 1; 741 $insert_args{bkbrand} = $webvar{bkbrand}; 742 $insert_args{bkmodel} = $webvar{bkmodel}; 743 for my $bkfield (qw(type src user vpass epass port)) { 742 for my $bkfield (@IPDB::backupfields) { 744 743 $insert_args{"bk$bkfield"} = ($webvar{"bk$bkfield"} ? $webvar{"bk$bkfield"} : ''); 745 744 } … … 908 907 # Backup fields. Minimal sanity checks. 909 908 for my $bkfield (qw(brand model)) { 910 if ($webvar{"bk$bkfield"} && $webvar{"bk$bkfield"} !~ /^[a-zA-Z0-9\s_.-]+$/) { 909 if (!$webvar{"bk$bkfield"}) { 910 $page->param(err => "Backup $bkfield must be filled in if IP/netblock is flagged for backup"); 911 return; 912 } 913 if ($webvar{"bk$bkfield"} !~ /^[a-zA-Z0-9\s_.-]+$/) { 911 914 $page->param(err => "Invalid characters in backup $bkfield"); 912 915 return; … … 924 927 if ($webvar{bkport} !~ /^\d+$/) { 925 928 $page->param(err => "Backup port must be numeric"); 929 return; 930 } 931 } 932 ##fixme: code review: should normalize $webvar{cidr} variants so we can 933 # check for non-/32 allocations having the backup IP field filled in here, 934 # instead of failing on the allocation or update attempt 935 if ($webvar{bkip}) { 936 $webvar{bkip} =~ s/^\s+//g; 937 $webvar{bkip} =~ s/\s+$//g; 938 if ($webvar{bkip} !~ /^[\da-fA-F:.]+$/) { 939 $page->param(err => "Backup IP must be an IP"); 926 940 return; 927 941 } … … 990 1004 if ($blockinfo->{hasbk}) { 991 1005 $page->param(hasbackup => $blockinfo->{hasbk}); 992 for my $bkfield ( qw(brand model src user vpass epass port)) {1006 for my $bkfield (@IPDB::backupfields) { 993 1007 $page->param("bk$bkfield" => $blockinfo->{"bk$bkfield"}); 994 1008 } … … 1125 1139 if ($IPDBacl{$authuser} =~ /s/) { 1126 1140 $updargs{privdata} = $webvar{privdata}; 1127 for my $bkfield ( qw(brand model type src user vpass epass port)) {1141 for my $bkfield (@IPDB::backupfields) { 1128 1142 $updargs{"bk$bkfield"} = $webvar{"bk$bkfield"}; 1129 1143 } 1130 $updargs{backup fields} = $webvar{backupfields};1144 $updargs{backup} = $webvar{backupfields}; 1131 1145 } else { 1132 1146 # If the user doesn't have permissions to monkey with NOC-things, pass … … 1203 1217 $page->param(nocling => 1); 1204 1218 $page->param(privdata => $webvar{privdata}); 1205 if ($ binfo->{hasbk}) {1206 $page->param(hasbackup => $binfo->{hasbk});1207 for my $bkfield (qw( brand model type src user vpass epass port)) {1208 $page->param("bk$bkfield" => $ binfo->{"bk$bkfield"});1219 if ($webvar{backupfields} && $webvar{backupfields} eq 'on') { 1220 $page->param(hasbackup => 1); 1221 for my $bkfield (qw(@IPDB::backupfields) { 1222 $page->param("bk$bkfield" => $webvar{"bk$bkfield"}); 1209 1223 } 1210 1224 } -
trunk/templates/assign.tmpl
r796 r798 175 175 <tr><td>Enable password:</td><td><input name="bkepass"></td></tr> 176 176 <tr><td>Alternate port (if required):</td><td><input name="bkport"></td></tr> 177 <tr><td>IP (if device is part of a netblock assignment):</td><td><input name="bkip"></td></tr> 177 178 </table> 178 179 </li></ul> -
trunk/templates/confirm.tmpl
r797 r798 129 129 <tr><td>Enable password:</td><td><TMPL_VAR NAME=bkepass></td></tr> 130 130 <tr><td>Port:</td><td><TMPL_VAR NAME=bkport></td></tr> 131 <tr><td>IP:</td><td><TMPL_VAR NAME=bkip></td></tr> 131 132 </table> 132 133 <TMPL_ELSE> … … 169 170 <input type="hidden" name="bkepass" value="<TMPL_VAR NAME=bkepass>"> 170 171 <input type="hidden" name="bkport" value="<TMPL_VAR NAME=bkport>"> 172 <input type="hidden" name="bkip" value="<TMPL_VAR NAME=bkip>"> 171 173 </TMPL_IF> 172 174 </TMPL_IF> -
trunk/templates/edit.tmpl
r796 r798 235 235 <tr><td>Enable password:</td><td><input name="bkepass" value="<TMPL_VAR NAME=bkepass>"></td></tr> 236 236 <tr><td>Alternate port (if required):</td><td><input name="bkport" value="<TMPL_VAR NAME=bkport>"></td></tr> 237 <tr><td>IP (if device is part of a netblock assignment):</td><td><input name="bkip" value="<TMPL_VAR NAME=bkip>"></td></tr> 237 238 </table> 238 239 -
trunk/templates/update.tmpl
r788 r798 80 80 <tr><td>Enable password:</td><td><TMPL_VAR NAME=bkepass></td></tr> 81 81 <tr><td>Port:</td><td><TMPL_VAR NAME=bkport></td></tr> 82 <tr><td>IP:</td><td><TMPL_VAR NAME=bkip></td></tr> 82 83 </table> 83 84 <TMPL_ELSE>
Note:
See TracChangeset
for help on using the changeset viewer.