(RADIATOR) Converting from Livingston user files to Radmin

Nacho Paredes iparedes at eurocomercial.es
Sun Jun 19 12:12:24 CDT 2005


Hi all,

I've written the following script for converting from Livingston user files
to a Radmin SQL Database.
I have a very bad accent speaking perl, so be prepared to see some awful
coding style, but hey, it worked for me :)
I hope somebody find it useful.

Regards

############################################################################
####################

#!/usr/bin/perl

# This script parses a Livingston users file and populates a Radmin Database
# Construct an array of user hashes
# Dumps every item in the array to the Radmin database including check and
reply items

# Needs: the Livingston filename like in:
# ./Living2Radmin users.txt

# Author: Ignacio Paredes. (iparedes at gmail.com)


use DBI;

# Change it to suit your system
my $dbsource = 'dbi:mysql:radmin';
my $dbusername = 'radmin';
my $dbauth = 'yaknowit';

# Global vars
my @REGS; # Users array
my $dbh;

my ($fichero)=@ARGV;

# Reads users
sub lee_registros{
        open(F,"<$fichero");
        my $rem,$user, at replies, at checks,$pass,$ip;

        while(<F>){
                # Grabs remarks, maybe you could do something useful with
them
                if (/^#(.*)$/){
                        $rem=substr $1,0,100;
                        $rem=~ tr/\"//d;
                }
                # Username, password and check-items
                elsif (/^(\S+)(\s+)(.*)$/){
                        my @c,$l,$x;
                        # Init attribute arrays
                        @replies=();
                        @checks=();
                        $pass="";
                        $ip="";
                        $user="$1";
                        #print "$user\n";
                        $l=$3;
                        # Extracts check-items
                        @c=split(/, */,$l);
                        # Process check-items, but...
                        # ...if finds a Password, put it into RADUSERS
                        while(@c){
                                $x=shift(@c);
                                $x =~ /(\S+)(\s*)=(\s*)(\S+)/;
                                my $atrib;
                                my $valor;
                                # Gets the pair attribute=valor
                                $atrib=$1;
                                $valor=$4;
                                # tear out quotes
                                $valor =~ tr/\"//d;
                                if ($atrib =~ /Password/){ $pass=$valor; }
                                # Translates Login-Time to Time
                                elsif ($atrib =~ /Login-Time/){
                                        $x="Time=$valor";
                                        push @checks,$x;
                                }
                                else{ 
                                        $x="$atrib=$valor";
                                        push @checks,$x;
                                }
                        }
                }
                # Process Reply-items
                elsif (/^([\t| ]+)(.*)$/){
                        my $temp=$2;
                        my $temp2;
                        my $n=index($temp,",");
                        my $atrib, $valor;
                        if ($n>=0){
                                $temp2=substr($temp,0,$n);
                                # Static IPs goes to RADUSERS table
                                $temp2 =~ /(\S+)(\s*)=(\s*)(\S+)/;
                                $atrib=$1;
                                $valor=$4;
                                # tear out quotes
                                $valor =~tr/\"//d;
                                if ($atrib =~ /Framed-IP-Address/){
$ip=$valor;}
                                # We don't need this Fall-Through attribute
                                elsif ($atrib !~ /Fall-Through/){
                                        my $x="$atrib=$valor";
                                        push @replies,$x; 
                                }
                        }
                        else{ # Last reply-item
                                $temp =~ /(.+)(\s*)=(\s*)(.+)/;
                                $atrib=$1;
                                $valor=$4;
                                # Tear out quotes
                                $valor =~tr/\"//d;
                                # Static IPs goes to RADUSERS table
                                if ($atrib =~ /Framed-IP-Address/){
$ip=$valor;}
                                elsif ($atrib !~ /Fall-Through/){
                                        my $x="atrib=$valor";
                                        push @replies,$x;
                                }

                                # Lets construct the hash
                                my %hash_usr=(
                                   usr  => $user,
                                   psw  => $pass,
                                   ip   => $ip,
                                   rep  => [@replies],
                                   chk  => [@checks],
                                );
                                # Lets put the user hash in the users array
                                push @REGS,{%hash_usr};
                        }
                }
        }
        close G;
}

# Print user records
sub imprime_registros{

        foreach(@REGS){
                imprime_registro($_);
        }
}

# Print user record
sub imprime_registro{
        my ($h,$FH)=@_;
        my $user=$h->{usr};
        my $pass=$h->{psw};
        my $ip=$h->{ip};
        my @c=@{$h->{chk}};
        my @r=@{$h->{rep}};

        if ($FH =~ /^$/){ $FH=STDOUT;}
        print {$FH} "\n\n# $rem\n";
        print {$FH} "$user \/ $pass\n";
        print {$FH} "Checks:\n-------\n";
        foreach(@c){
                print {$FH} "$_\n";
        }
        print {$FH} "Replies:\n-------\n";
        print {$FH} "IP: $ip\n";
        foreach(@r){
                print {$FH} "$_\n";
        }
}

# Gets data attributes by attribute name
sub ids_atributo{
        my ($atrib)=@_;

        my $q="select ATTR_ID,VENDOR_ID,TYPE from RADATTRS where
NAME=\'$atrib\'";
        my ($attr_id,$vendor_id,$type)=$dbh->selectrow_array($q);
        return($attr_id,$vendor_id,$type);
}

# checks if the user already exists in RADUSERS
sub existe_usuario{
        my ($u)=@_;
        my @v=$dbh->selectrow_array("select USERNAME from RADUSERS where
USERNAME=\'$u\'");
        if (@v){ return(1);}
        else{ return(0);}
}

# Process and inserts items
# Needs the item array and the item type:
# type=0 => check-items
# type=1 => reply-items
sub procesa_items{
        my ($refa,$user,$itype)=@_;
        
        #my @r=@{$h->{rep}};
        #my $itype=10000;
        @r=@{$refa};
        if ($itype!=0) { $itype=10000;}

        foreach(@r){
                /(.*)(\s*)=(\s*)(.*)/;
                my $atrib=$1;
                my $value=$4;
                my $q;

                my ($attr_id,$vendor_id,$type)=ids_atributo($atrib);
                if ($attr_id=="" and $vendor_id="" and $type=""){
                        print "Atributo desconocido $atrib\n";
                        next;
                }

                # According to the type:
                # Type=1 -> string - dont touch it
                # Type=2 -> number - if value is number ok, else is a label
whose value we must search
                # Type=3 -> IP     - like a string, dont touch it
                if ($type==2){
                        # If value is not numeric, is a label
                        if($value !~ /^(\d+)$/){
                                my ($value2)=$dbh->selectrow_array("select
IVALUE from RADVALUES where  \
                                 ATTR_ID=\'$attr_id\' and NAME=\'$value\'
and VENDOR_ID=\'$vendor_id\'");
                                if ($value2 == ""){ print "Unknown Value
$value\n";}
                                $value=$value2;
                        }
                        $q="insert into RADCONFIG (ATTR_ID, ITEM_TYPE,
IVALUE, NAME, VENDOR_ID) values  \
                            (\'$attr_id\', \'$itype\', \'$value\',
\'$user\', \'$vendor_id\')";
                }
                else{
                        $q="insert into RADCONFIG (ATTR_ID, ITEM_TYPE, NAME,
SVALUE, VENDOR_ID) values \
                            (\'$attr_id\', \'$itype\', \'$user\',
\'$value\', \'$vendor_id\')";
                }
        $itype=$itype+1;
        $dbh->do($q);
        }

}

# ************** START ******************** 

# Lets track duplicate names
open(REPS,">dups.txt");

$dbh = DBI->connect($dbsource, $dbusername, $dbauth)
                || die "connect failed: $DBI::errstr";

# Read the file
lee_registros();

#imprime_registros();
#exit();

# We have put all the users in the file into REGS, lets write then to Radmin
my $cont=0;
foreach(@REGS){
        my $h=$_;

        my $user=$h->{usr};
        my $pass=$h->{psw};
        my $ip=$h->{ip};
        my @r, at c;

        my $b=existe_usuario($user);
        if ($b == 1){
                printf("Duplicate $user!\n");
                imprime_registro($h,REPS);
        }
        else{
                $cont=$cont+1;
                printf("$cont - $user\n");
		    # We use validfrom:an arbitray date and validto:forever
                my $q="insert into RADUSERS (USERNAME, PASS_WORD, VALIDFROM,
VALIDTO, STATICADDRESS) values (\'$user\', \'$pass\', 1114898400,
2147483647, \'$ip\')";

                $dbh->do($q);
                # Procesa reply-items
                @r=@{$h->{rep}};
                procesa_items(\@r,$user,1);
                # Procesa check-items
                @c=@{$h->{chk}};
                procesa_items(\@c,$user,0);
        }
}
$dbh->disconnect();
close(REPS); 

############################################################################
####################

> -----Original Message-----
> From: owner-radiator at open.com.au 
> [mailto:owner-radiator at open.com.au] On Behalf Of Nacho Paredes
> Sent: jueves, 05 de mayo de 2005 11:19
> To: radiator at open.com.au
> Subject: RE: (RADIATOR) Converting from Livingston user files 
> to Radmin
> 
> Hello,
> 
> Thanks four your reply Hugh.
> 
> buildSQL works great, but convert just deals with Username 
> and Password, and I need to process check and reply items for 
> each user, so I'll try to do this part myself.
> 
> I've been digging in Radmin code and structure, and I've 
> found out some issues I would like you to tell me if I am 
> right about them:
> 
> I'm starting with a SUBSCRIBERS table where I have the 
> following columns:
> User, Password, Check-items and Reply-items I want to 
> redistribute this information over the Radmin structure, so 
> I'll do this:
> 
> For each row in SUBSCRIBERS
> 	With User and Password I can populate the USERNAME and 
> PASS_WORD columns in RADUSERS.
> 	For each element in Check-items (where an element is 
> something like
> Attribute=Value)
> 		I have to populate de RADCONFIG table, so I will:
> 		Lookup in RADATTRS: ATTR_ID and VENDOR_ID where 
> NAME=Attribute
> 		I've seen that ITEM_TYPE in RADCONFIG is a 
> sequencial number where (0-9999) means check-item and 
> (10000-) means reply-item
> 		At this point I can populate ATTR_ID, 
> ITEM_TYPE, NAME, VENDOR_ID in RADCONFIG, so I just have to 
> set the value of the attribute. I guess that the IVALUE 
> column means a numeric value and the SVALUE column means a 
> string value, but is this related to the TYPE column in RADATTRS?
> (what does mean that TYPE column?)
> 	For each element in Reply-items (I'll do the same in 
> the previous
> step)
> 		
> This is roughly what I want to do, but I've seen some special 
> cases, for example, if I have a Framed-IP-Address in the 
> Reply-items of a SUBSCRIBERS record, I could put it in the 
> STATICADDRESS of RADUSERS instead of putting it in RADCONFIG. 
> Is this true or am I missing something?
> 
> Please, tell me if my guessings are right.
> 
> Is in some place documented the Radmin SQL structure and its usage? 
> 
> Thanks for your attention.
> 
> Regards
> 
> -----Original Message-----
> From: Hugh Irvine [mailto:hugh at open.com.au]
> Sent: jueves, 05 de mayo de 2005 3:37
> To: Nacho Paredes
> Cc: radiator at open.com.au
> Subject: Re: (RADIATOR) Converting from Livingston user files 
> to Radmin
> 
> 
> Hello Nacho -
> 
> There is the "buildSQL" utility included with Radiator and 
> the "convert"
> utility included with Radmin.
> 
> I have in the past used the "buildSQL" utility to load a 
> Livingston user
> file (or a password file) into a Radiator database, then used 
> the "convert"
> script from Radiator to Radmin. Alternatively you could modify the
> "buildSQL" utility to load the data directly into the Radmin database.
> 
> regards
> 
> Hugh
> 
> 
> 
> On 4 May 2005, at 20:41, Nacho Paredes wrote:
> 
> > Hi all,
> >
> > I've got a bunch of Livingston user files that I should convert to 
> > Radmin format, and before I start coding a conversion 
> script I would 
> > like to know if somebody has made something similar before or there 
> > exists any tool or starting point that helps me.
> >
> > Thanks on advance.
> >
> > Regards.
> >
> > --------------------------------------------------------------------
> > Ignacio Paredes               |  email: iparedes at eurocomercial.es
> > Eurocomercial I&C, S.A.       |  Tel: +34 98 5195703
> > Ezcurdia, 194 - Gijon (AS)    |  Fax: +34 98 5132596
> > --------------------------------------------------------------------
> >
> >
> > --
> > Archive at http://www.open.com.au/archives/radiator/
> > Announcements on radiator-announce at open.com.au To 
> unsubscribe, email 
> > 'majordomo at open.com.au' with 'unsubscribe radiator' in the 
> body of the 
> > message.
> >
> >
> 
> NB:
> 
> Have you read the reference manual ("doc/ref.html")?
> Have you searched the mailing list archive
> (www.open.com.au/archives/radiator)?
> Have you had a quick look on Google (www.google.com)?
> Have you included a copy of your configuration file (no 
> secrets), together
> with a trace 4 debug showing what is happening?
> 
> --
> Radiator: the most portable, flexible and configurable RADIUS server
> anywhere. Available on *NIX, *BSD, Windows, MacOS X.
> -
> Nets: internetwork inventory and management - graphical, extensible,
> flexible with hardware, software, platform and database independence.
> -
> CATool: Private Certificate Authority for Unix and Unix-like systems.
> 
> 
> 
> --
> Archive at http://www.open.com.au/archives/radiator/
> Announcements on radiator-announce at open.com.au
> To unsubscribe, email 'majordomo at open.com.au' with
> 'unsubscribe radiator' in the body of the message.
> 
> 

--
Archive at http://www.open.com.au/archives/radiator/
Announcements on radiator-announce at open.com.au
To unsubscribe, email 'majordomo at open.com.au' with
'unsubscribe radiator' in the body of the message.


More information about the radiator mailing list