ICRADIUS export bugfix
[freeside.git] / bin / svc_acct.export
index 3f65a08..8bec4aa 100755 (executable)
@@ -1,5 +1,7 @@
 #!/usr/bin/perl -Tw
 #
+# $Id: svc_acct.export,v 1.4 2000-03-06 14:19:15 ivan Exp $
+#
 # Create and export password files: passwd, passwd.adjunct, shadow,
 # acp_passwd, acp_userinfo, acp_dialup, users
 #
 #
 # OOPS!  added arbitrary radius fields (pry 98-aug-16) but forgot to say so.
 # ivan@sisd.com 98-sep-18
+# 
+# $Log: svc_acct.export,v $
+# Revision 1.4  2000-03-06 14:19:15  ivan
+# ICRADIUS export bugfix
+#
+# Revision 1.2  1998/12/10 07:23:15  ivan
+# use FS::Conf, need user (for datasrc)
+#
 
 use strict;
+use vars qw($conf);
 use Fcntl qw(:flock);
-use FS::SSH qw(scp ssh);
-use FS::UID qw(adminsuidsetup);
+use IO::Handle;
+use FS::Conf;
+use FS::SSH qw(scp ssh sshopen2);
+use FS::UID qw(adminsuidsetup datasrc dbh);
 use FS::Record qw(qsearch fields);
+use FS::svc_acct;
 
-my($fshellmachines)="/var/spool/freeside/conf/shellmachines";
-my(@shellmachines);
-if ( -e $fshellmachines ) {
-  open(SHELLMACHINES,$fshellmachines);
-  @shellmachines=map {
-    /^(.*)$/ or die "Illegal line in conf/shellmachines"; #we trust the file
-    $1;
-  } grep $_ !~ /^(#|$)/, <SHELLMACHINES>;
-  close SHELLMACHINES;
-}
+my $user = shift or die &usage;
+adminsuidsetup $user;
 
-my($fbsdshellmachines)="/var/spool/freeside/conf/bsdshellmachines";
-my(@bsdshellmachines);
-if ( -e $fbsdshellmachines ) {
-  open(BSDSHELLMACHINES,$fbsdshellmachines);
-  @bsdshellmachines=map {
-    /^(.*)$/ or die "Illegal line in conf/bsdshellmachines"; #we trust the file
-    $1;
-  } grep $_ !~ /^(#|$)/, <BSDSHELLMACHINES>;
-  close BSDSHELLMACHINES;
-}
+$conf = new FS::Conf;
 
-my($fnismachines)="/var/spool/freeside/conf/nismachines";
-my(@nismachines);
-if ( -e $fnismachines ) {
-  open(NISMACHINES,$fnismachines);
-  @nismachines=map {
-    /^(.*)$/ or die "Illegal line in conf/nismachines"; #we trust the file
-    $1;
-  } grep $_ !~ /^(#|$)/, <NISMACHINES>;
-  close NISMACHINES;
-}
+my @shellmachines = $conf->config('shellmachines')
+  if $conf->exists('shellmachines');
 
-my($ferpcdmachines)="/var/spool/freeside/conf/erpcdmachines";
-my(@erpcdmachines);
-if ( -e $ferpcdmachines ) {
-  open(ERPCDMACHINES,$ferpcdmachines);
-  @erpcdmachines=map {
-    /^(.*)$/ or die "Illegal line in conf/erpcdmachines"; #we trust the file
-    $1;
-  } grep $_ !~ /^(#|$)/, <ERPCDMACHINES>;
-  close ERPCDMACHINES;
-}
+my @bsdshellmachines = $conf->config('bsdshellmachines')
+  if $conf->exists('bsdshellmachines');
 
-my($fradiusmachines)="/var/spool/freeside/conf/radiusmachines";
-my(@radiusmachines);
-if ( -e $fradiusmachines ) {
-  open(RADIUSMACHINES,$fradiusmachines);
-  @radiusmachines=map {
-    /^(.*)$/ or die "Illegal line in conf/radiusmachines"; #we trust the file
-    $1;
-  } grep $_ !~ /^(#|$)/, <RADIUSMACHINES>;
-  close RADIUSMACHINES;
-}
+my @nismachines = $conf->config('nismachines')
+  if $conf->exists('nismachines');
+
+my @erpcdmachines = $conf->config('erpcdmachines')
+  if $conf->exists('erpcdmachines');
 
-my($spooldir)="/var/spool/freeside/export";
-my($spoollock)="/var/spool/freeside/svc_acct.export.lock";
+my @radiusmachines = $conf->config('radiusmachines')
+  if $conf->exists('radiusmachines');
 
-adminsuidsetup;
+my $icradiusmachines = $conf->exists('icradiusmachines');
+my @icradiusmachines = $conf->config('icradiusmachines') if $icradiusmachines;
+my $icradius_mysqldest =
+  $conf->config('icradius_mysqldest') || "/usr/local/var/radius"
+    if $icradiusmachines;
+my $icradius_mysqlsource =
+  $conf->config('icradius_mysqlsource') || "/usr/local/var/freeside"
+    if $icradiusmachines;
+my $icradius_dbh = dbh; #could eventually get it from a config file if you're
+                        #not running MySQL for your Freeside database
 
 my(@saltset)= ( 'a'..'z' , 'A'..'Z' , '0'..'9' , '.' , '/' );
-srand(time|$$);
+require 5.004; #srand(time|$$);
+
+my $spooldir = "/usr/local/etc/freeside/export.". datasrc;
+my $spoollock = "/usr/local/etc/freeside/svc_acct.export.lock.". datasrc;
 
 open(EXPORT,"+>>$spoollock") or die "Can't open $spoollock: $!";
 select(EXPORT); $|=1; select(STDOUT);
@@ -146,6 +134,11 @@ chmod 0600, "$spooldir/master.passwd",
             "$spooldir/users",
 ;
 
+if ( $icradiusmachines ) {
+  my $sth = $icradius_dbh->prepare("DELETE FROM radcheck");
+  $sth->execute or die "Can't reset radcheck table: ". $sth->errstr;
+}
+
 setpriority(0,0,10);
 
 my($svc_acct);
@@ -253,6 +246,21 @@ foreach $svc_acct (@svc_acct) {
       print USERS qq(\n\n);
     }
 
+    ###
+    # ICRADIUS export
+    if ( $icradiusmachines ) {
+      my $sth = $icradius_dbh->prepare(
+        "INSERT INTO radcheck ( id, UserName, Attribute, Value ) VALUES ( ".
+        join(", ", map { $icradius_dbh->quote( $_ ) } qw (
+          $svc_acct->svcnum
+          $svc_acct->username
+          "Password"
+          $svc_acct->_password
+        ) ). " )"
+      );
+      $sth->execute or die "Can't insert into radcheck table: ". $sth->errstr;
+   }
+
   }
 
 }
@@ -345,7 +353,26 @@ foreach $radiusmachine (@radiusmachines) {
     == 0 or die "ssh error: $!";
 }
 
+foreach my $icradiusmachine ( @icradiusmachines ) {
+  my( $machine, $db, $user, $pass ) = split(/\s+/, $icradiusmachine);
+  chdir $icradius_mysqlsource or die "Can't cd $icradius_mysqlsource: $!";
+  my($reader,$writer)=(new IO::Handle, new IO::Handle);
+  sshopen2("root\@$machine", $reader, $writer, "mysql --user=$user -p $db");
+  print $writer "$pass\nLOCK TABLES radcheck WRITE;\n";
+  foreach my $file ( glob("radcheck.*") ) {
+    scp($file,"root\@$icradiusmachine:$icradius_mysqldest/$db/$file");
+  }
+  close $writer;
+  close $reader;
+}
+
 unlink $spoollock;
 flock(EXPORT,LOCK_UN);
 close EXPORT;
 
+#
+
+sub usage {
+  die "Usage:\n\n  svc_acct.export user\n";
+}
+