3 # users2mysql.pl -- a script to parse a RADIUS users file and fill
4 # a freeradius mysql database...
7 # Script developed by Rich Puhek, Znet Telecom
9 # last change: Aug 8th, 2002.
14 #Modify to suit your db.
16 $hostname="localhost";
21 #location of source users file:
22 $users_file="/etc/raddb_cistron_backup/users";
25 #The following are defaults from freeradius 0.7
26 # ...shouldn't have to change.
27 $groups_table="usergroup";
28 $check_table="radcheck";
29 $reply_table="radreply";
35 #open the users file, and the db.
36 open USERS, $users_file or die "ERROR: Unable to open $users_file $!\n";
37 $database = DBI->connect("DBI:mysql:$database:$hostname",$user, $password) or die "ERROR: Unable to connect to $database on $hostname $!\n";
41 if (!defined($_[0]) or !defined($_[1])) {
42 print "undefined parameter!\n";
49 if ($attr !~ /Password|Framed-IP-Address|Framed-IP-Netmask|Framed-IP-Routing|Framed-Routing|Framed-IP-Route|Password|Simultaneous-Use|Idle-Timeout|Auth-Type|Service-Type|Netmask|Framed-Protocol/ ) {
50 print "unrecognized attribute: $attr\n" if $debug>1;
54 return undef if ( (! defined($val) ) or
55 ( ($attr =~ /Simultaneous\-Use/i) && ( $val !~ /^[0-9]*$/ ) )
57 print "attribs ok!\n" if $debug>3;
62 #clean up variables: strip leading/trailing spaces and trailing commas...
73 #push values into db...
79 print "inserting \"$dattrib\", \"$dval\" for \"$duser\" in rad$dtable\n" if ( $dtable !~ /group/ and $debug>2);
80 print "inserting \"$duser\" into usergroup table as member of \"$dattrib\"\n" if ( $dtable =~ /group/ and $debug>2);
82 if ( $dtable =~ /group/ ) {
84 } elsif ( $dtable =~ /check/ ) {
86 } elsif ( $dtable =~ /reply/ ) {
89 die "argh! what table is $dtable?\n";
93 if ( $table =~ /usergroup/ ) {
94 if ( $dattrib =~ /static/ ) {
95 #Delete the "dynamic" entry...
96 $return = $database->do ("DELETE FROM `$table` WHERE `UserName`='$duser' LIMIT 1");
98 $return = $database->do ("INSERT INTO `$table` SET `UserName`='$duser',`GroupName`='$dattrib'");
101 $return = $database->do ("INSERT INTO `$table` SET `UserName`='$duser',`Attribute`='$dattrib',`Value`='$dval', `op`=':='");
110 #Skip comment lines and blank lines...
115 if ( /^[a-zA-Z0-9]+/ ) {
116 print "located a user entry: $_\n" if $debug>6;
117 ($user,$rest) = split /\s/, $_, 2;
118 #Put user into usergroup as dynamic, if the user's attributes
119 # include an IP address, the script will change that later...
120 user_attribute("group",$user,"dynamic","");
121 @attribs = split /,/, $rest;
123 # Already found the user, now finding attributes...
127 foreach $attr (@attribs) {
128 ($attrib,$value) = split /=/, $attr, 2;
129 #TODO: insert sanity checks here!
130 $value = cleanup($value) if (defined($value));
131 $attrib = cleanup($attrib) if (defined($attrib));
132 unless (check_attribs($attrib,$value)) {
133 print "ERROR: something bad with line $.: \"$attrib\", \"$value\"\n";
136 print "attrib: $attrib has value: $value\n" if $debug>8;
138 if ( $attrib =~ /Framed-IP-Address/ ) {
139 #user is a static IP user...
141 user_attribute("group",$user,"static","");
144 if ( $attrib =~ /Password|Simultaneous-Use/ ) {
145 #This is an individual check attribute, so we'll pass it along...
146 user_attribute("check",$user,$attrib,$value);
148 if ( $attrib =~ /Framed-IP-Address|Framed-IP-Routing|Framed-Routing/ ) {
149 #This is an individual reply attribute, so we'll pass this along...
150 user_attribute("reply",$user,$attrib,$value);
157 exit($database->disconnect);