Changeset 2056


Ignore:
Timestamp:
08/17/17 14:04:39 (7 years ago)
Author:
nanardon
Message:

Add support for netzone radius

Location:
trunk/LATMOS-Accounts/lib/LATMOS/Accounts
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/LATMOS-Accounts/lib/LATMOS/Accounts/Bases/Sql/Netzone.pm

    r1551 r2056  
    4141                formtype => 'CHECKBOX', 
    4242                ro => sub {  
    43                     $_[0] && ($_[0]->get_attributes('type') || '') eq 'dhcp' ? 0 : 1 
     43                    $_[0] && ($_[0]->get_attributes('type') || '') =~ /dhcp|radius/ ? 0 : 1 
    4444                }, 
    4545            }, 
     
    6666            type    => { 
    6767                mandatory => 1, 
    68                 can_values => sub { qw(dhcp dns reverse puppet) }, 
     68                can_values => sub { qw(dhcp dns reverse puppet radius) }, 
    6969            }, 
    7070            site   => { 
     
    7777                reference => 'netzone', 
    7878                ro => sub {  
    79                     $_[0] && ($_[0]->get_attributes('type') || '') eq 'dhcp' ? 0 : 1 
     79                    $_[0] && ($_[0]->get_attributes('type') || '') =~ /^(dhcp|radius)$/ ? 0 : 1 
    8080                }, 
    8181            }, 
     
    203203            domain => { }, 
    204204            description => { }, 
     205            hostParams => { multiple => 1 },  
    205206        } 
    206207    ) 
  • trunk/LATMOS-Accounts/lib/LATMOS/Accounts/Task/Buildnet.pm

    r2015 r2056  
    220220 
    221221 
    222     if ($ozone->get_attributes('type') ne 'dhcp' && $ozone->get_attributes('templateD')) { 
     222    if ($ozone->get_attributes('type') !~ /^(dhcp|radius)$/ && $ozone->get_attributes('templateD')) { 
    223223        my $template = $self->_template_file($ozone); 
    224224        my $output = $self->_output_file($ozone); 
     
    284284        $type eq 'dhcp'    ? $self->_gen_dhcp_zone($ozone, $header) : 
    285285        $type eq 'puppet'  ? $self->_gen_puppet_zone($ozone, $header) : 
     286        $type eq 'radius'  ? $self->_gen_radius_zone($ozone, $header) : 
    286287        undef; 
    287288 
     
    355356    my @output = (); 
    356357    my $com_prefix = 
    357         $ozone->get_attributes('type') =~ /^(dhcp|puppet)$/ ? '# ' : '; '; 
     358        $ozone->get_attributes('type') =~ /^(dhcp|puppet|radius)$/ ? '# ' : '; '; 
    358359    push @output, sprintf('Zone %s, type %s', $ozone->id, 
    359360        $ozone->get_attributes('type')); 
     
    754755 
    755756            my $desc = $self->_comment_nethost($obj); 
    756             foreach my $mac (grep { $_ } $obj->get_attributes('macaddr')) { 
     757            foreach my $mac (sort grep { $_ } $obj->get_attributes('macaddr')) { 
    757758                $output .= $desc 
    758759                ? '# ' . $desc . "\n" 
     
    763764                $output .= sprintf("    hardware ethernet %s;\n", $mac); 
    764765                $output .= "}\n\n"; 
     766            } 
     767        } 
     768    } 
     769 
     770    $output .= "# End of data from database\n"; 
     771    if (open(my $handle, '>', $self->_output_file($outzone))) { 
     772        print $handle $output; 
     773        close($handle); 
     774        la_log(LA_INFO, "zone %s written into %s", $outzone->id, 
     775            $self->_output_file($outzone)); 
     776    } else { 
     777        la_log(LA_ERR, "Can't open output file for dhcp zone %s (%s)", 
     778            $outzone->id, $!); 
     779        return; 
     780    } 
     781    1; 
     782} 
     783 
     784sub _gen_radius_zone { 
     785    my ($self, $ozone, $output) = @_; 
     786 
     787    my $outzone = $ozone; 
     788 
     789    my @net; 
     790    if ($outzone->get_attributes('net')) { 
     791        @net = (map { Net::IP->new($_) } $outzone->get_attributes('net')) or do { 
     792            la_log(LA_DEBUG, 'Cannot get Net::IP for zone %s (ip: %s)', $outzone->id, 
     793                join(', ', $outzone->get_attributes('net'))); 
     794            next; 
     795        }; 
     796    } 
     797 
     798    my @parameters = $outzone->get_attributes('hostParams'); 
     799 
     800    { 
     801        my $find = $self->_base->db->prepare(q{ 
     802            select * from nethost where exported = true and ikey in( 
     803            select okey from nethost_attributes where attr = 'macaddr' 
     804            intersect ( 
     805                select nethost_attributes_ips.okey from nethost_attributes_ips join 
     806                netzone_attributes 
     807                on netzone_attributes.attr = 'net' and 
     808                netzone_attributes.value::inet >>= nethost_attributes_ips.value::inet 
     809                join netzone on netzone.ikey = netzone_attributes.okey 
     810                where netzone.name = $1 
     811 
     812                except 
     813                select nethost_attributes_ips.okey from nethost_attributes_ips join 
     814                netzone_attributes 
     815                on netzone_attributes.attr = 'netExclude' and 
     816                netzone_attributes.value::inet >>= nethost_attributes_ips.value::inet 
     817                join netzone on netzone.ikey = netzone_attributes.okey 
     818                where netzone.name = $1 
     819                ) 
     820            ) 
     821            order by name 
     822 
     823            }); 
     824        $find->execute($ozone->id) or do { 
     825            la_log LA_ERR, "Cannot fetch host list: %s", 
     826                $self->_base->db->errstr; 
     827            return; 
     828        }; 
     829        while (my $res = $find->fetchrow_hashref) { 
     830            my $nethost = $res->{name}; 
     831 
     832            my $obj = $self->_base->get_object('nethost', $nethost) or do { 
     833                la_log LA_ERR, "Cannot fetch host %s", $res->{name}; 
     834                return; 
     835            }; 
     836 
     837            my $retainip; 
     838            if (@net) { 
     839                foreach my $inet (@net) { 
     840                    ($retainip) = grep { $_ && $inet->overlaps(Net::IP->new($_)) } $obj->get_attributes('ip') 
     841                        and last; 
     842                } 
     843            } 
     844 
     845            $obj->get_attributes('noDynamic') && !$retainip and next; 
     846 
     847            my $desc = $self->_comment_nethost($obj); 
     848            foreach my $mac (sort grep { $_ } $obj->get_attributes('macaddr')) { 
     849                $output .= $desc 
     850                ? '# ' . $desc . "\n" 
     851                : ''; 
     852                my $fmac = $mac; 
     853                $fmac =~ s/://g; 
     854                $output .= sprintf("%s Auth-Type := EAP, User-Password == \"%s\"\n", $nethost, lc($fmac)); 
     855                $output .= "    $_\n" foreach(@parameters); 
     856                $output .= "\n"; 
     857            } 
     858        } 
     859    } 
     860    if ($ozone->get_attributes('allow_dyn')) { 
     861        $output .= "\n# Host without IP:\n"; 
     862        my @dynfrom = grep { $_ } $ozone->get_attributes('dynFrom'); 
     863        my $find = $self->_base->db->prepare(q{ 
     864            select * from nethost where exported = true and ikey in( 
     865            select okey from nethost_attributes where attr = 'macaddr' 
     866            } . (@dynfrom ? q{ 
     867            intersect 
     868            ( 
     869                select ikey from nethost where ikey not in 
     870                    (select okey from nethost_attributes_ips) 
     871                union 
     872 
     873                ( 
     874                select nethost_attributes_ips.okey from nethost_attributes_ips join 
     875                netzone_attributes 
     876                on netzone_attributes.attr = 'net' and 
     877                   netzone_attributes.value::inet >>= 
     878                   nethost_attributes_ips.value::inet 
     879                   join netzone on netzone.ikey = netzone_attributes.okey 
     880                   where netzone.name = any(?) 
     881                except 
     882                select nethost_attributes_ips.okey from nethost_attributes_ips join 
     883                netzone_attributes 
     884                on netzone_attributes.attr = 'netExclude' and 
     885                   netzone_attributes.value::inet >>= 
     886                   nethost_attributes_ips.value::inet 
     887                   join netzone on netzone.ikey = netzone_attributes.okey 
     888                   where netzone.name = any(?) 
     889                ) 
     890            )} : '') . q{ 
     891            except 
     892            select nethost_attributes_ips.okey from nethost_attributes_ips join 
     893            netzone_attributes 
     894            on netzone_attributes.attr = 'net' and 
     895            netzone_attributes.value::inet >>= nethost_attributes_ips.value::inet 
     896            join netzone on netzone.ikey = netzone_attributes.okey 
     897            where netzone.name = ? 
     898            ) 
     899            order by name 
     900 
     901            }); 
     902        $find->execute((@dynfrom ? ([ @dynfrom ], [ @dynfrom ]) : ()), $ozone->id) or do { 
     903            la_log LA_ERR, "Cannot fetch host list: %s", 
     904                $self->_base->db->errstr; 
     905            return; 
     906        }; 
     907        while (my $res = $find->fetchrow_hashref) { 
     908            my $nethost = $res->{name}; 
     909 
     910            my $obj = $self->_base->get_object('nethost', $nethost); 
     911 
     912            $obj->get_attributes('noDynamic') and next; 
     913 
     914            my $desc = $self->_comment_nethost($obj); 
     915            foreach my $mac (sort grep { $_ } $obj->get_attributes('macaddr')) { 
     916                $output .= $desc 
     917                ? '# ' . $desc . "\n" 
     918                : ''; 
     919                my $fmac = $mac; 
     920                $fmac =~ s/://g; 
     921                $output .= sprintf("%s Auth-Type := EAP, User-Password == \"%s\"\n", $nethost, lc($fmac)); 
     922                $output .= "    $_\n" foreach(@parameters); 
     923                $output .= "\n"; 
    765924            } 
    766925        } 
Note: See TracChangeset for help on using the changeset viewer.