4 use vars qw(%part_svc %domain_part_svc);
7 use Term::Query qw(query);
8 use FS::UID qw(adminsuidsetup); #datasrc
9 use FS::Record qw(qsearch qsearchs);
14 my $user = shift or die &usage;
17 #push @FS::svc_acct::shells, qw(/bin/sync /sbin/shutdown /bin/halt /sbin/halt); #others?
19 $FS::svc_Common::noexport_hack = 1;
20 $FS::svc_domain::whois_hack = 1;
24 %part_svc=map { $_->svcpart, $_ } qsearch('part_svc',{'svcdb'=>'svc_acct'});
26 die "No services with svcdb svc_acct!\n" unless %part_svc;
28 print "\n\n", &menu_svc, "\n", <<END;
29 Enter part number to import.
31 my $sqlradius_svcpart = &getpart;
33 %domain_part_svc = map { $_->svcpart, $_ }
34 qsearch('part_svc', { 'svcdb' => 'svc_domain'} );
36 die "No services with svcdb svc_domain!\n" unless %domain_part_svc;
38 print "\n\n", &menu_domain_svc, "\n", <<END;
39 Enter part number for domains.
41 my $domain_svcpart = &getdomainpart;
43 my $datasrc = &getvalue("\n\nEnter the DBI datasource:");
44 my $db_user = &getvalue("\n\nEnter the database user:");
45 my $db_pass = &getvalue("\n\nEnter the database password:");
48 ( join "\n", map "$_: ".$part_svc{$_}->svc, sort keys %part_svc ). "\n";
51 ( join "\n", map "$_: ".$domain_part_svc{$_}->svc, sort keys %domain_part_svc ). "\n";
54 $^W=0; # Term::Query isn't -w-safe
55 my $return = query "Enter part number:", 'irk', [ keys %part_svc ];
60 $^W=0; # Term::Query isn't -w-safe
61 my $return = query "Enter part number:", 'irk', [ keys %domain_part_svc ];
67 $^W=0; # Term::Query isn't -w-safe
68 my $return = query $prompt, '';
77 my $dbh = DBI->connect( $datasrc, $db_user, $db_pass )
80 my $sth = $dbh->prepare('SELECT DISTINCT UserName, Realm FROM radcheck')
82 $sth->execute or die $sth->errstr;
85 while ( defined ( $row = $sth->fetchrow_arrayref ) ) {
86 my( $r_username, $realm ) = @$row;
88 my( $username, $domain );
89 if ( $r_username =~ /^([^@]+)\@([^@]+)$/ ) {
93 $username = $r_username;
96 my $svc_domain = qsearchs('svc_domain', { 'domain' => $domain } )
97 || new FS::svc_domain {
99 'svcpart' => $domain_svcpart,
102 unless ( $svc_domain->svcnum ) {
103 die "new domain? wtf";
104 my $error = $svc_domain->insert;
106 die "can't insert domain $domain: $error\n";
110 #my( $password, $finger, $group ) = ( '', '', '' );
111 my( $password, $group ) = ( '', '', '' );
113 my $rc_sth = $dbh->prepare(
114 'SELECT Attribute, Value, Name, GroupName'.
116 ' WHERE UserName = ? and Realm = ?'
117 ) or die $dbh->errstr;
118 $rc_sth->execute($r_username, $realm) or die $rc_sth->errstr;
120 foreach my $rc_row ( @{$rc_sth->fetchall_arrayref} ) {
121 my($attribute, $value, $name, $groupname) = @$rc_row;
122 if ( $attribute =~ /^((Crypt|User)-)?Password$/ ) {
127 #handle other params!
131 my @svc_acct = grep { $_->cust_svc->svcpart == $sqlradius_svcpart }
132 qsearch('svc_acct', { 'username' => $username,
133 'domsvc' => $svc_domain->svcnum, } );
135 #print "$r_username / $realm: $password / $finger: ";
136 print "$r_username / $realm: $password: ";
137 if ( scalar(@svc_acct) == 0 ) {
140 } elsif ( scalar(@svc_acct) > 1 ) {
141 print "multiple matches found?!?!\n";
144 #print "correcting password and name\n";
145 print "correcting password\n";
148 my $svc_acct = $svc_acct[0];
149 #my $new = new FS::svc_acct { $svc_acct->hash, '_password' => $password, 'finger' => $finger };
150 my $new = new FS::svc_acct { $svc_acct->hash, '_password' => $password };
151 my $error = $new->replace($svc_acct);
152 #my $error = $new->check;
153 die "$r_username / $realm: $error" if $error;
158 die "Usage:\n\n sqlradius.reimport user\n";