summaryrefslogtreecommitdiff
path: root/FS/FS/part_export/broadband_sqlradius.pm
blob: bf95e234d3f63e4b35d2631f6d5217b4e65d93a2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
package FS::part_export::broadband_sqlradius;

use strict;
use vars qw($DEBUG @ISA @pw_set %options %info $conf);
use Tie::IxHash;
use FS::Conf;
use FS::Record qw( dbh str2time_sql ); #qsearch qsearchs );
use FS::part_export::sqlradius qw(sqlradius_connect);
use FS::Password_Mixin;
use NEXT;

FS::UID->install_callback(
  sub {
    $conf = new FS::Conf;
    @pw_set = FS::Password_Mixin->pw_set;
  }
);

@ISA = qw(FS::part_export::sqlradius);

$DEBUG = 0;

tie %options, 'Tie::IxHash',
  'datasrc'  => { label=>'DBI data source ' },
  'username' => { label=>'Database username' },
  'password' => { label=>'Database password' },
  'usergroup'=> { label   => 'Group table',
                  type    => 'select',
                  options => [qw( radusergroup usergroup )],
                },
  'ignore_accounting' => {
    type  => 'checkbox',
    label => 'Ignore accounting records from this database'
  },
# session report doesn't currently know about this export anyway
#  'hide_ip' => {
#    type  => 'checkbox',
#    label => 'Hide IP address on session reports',
#  },
  'mac_case' => {
    label => 'Export MAC address as',
    type  => 'select',
    options => [ qw(uppercase lowercase) ],
  },
  'mac_delimiter' => {
    label => 'Separate MAC address octets with',
    default => '-',
  },
  'mac_as_password' => { 
    type => 'checkbox',
    default => '1',
    label => 'Use MAC address as password',
  },
  'radius_password' => { label=>'Fixed password' },
  'ip_addr_as' => { label => 'Send IP address as',
                    default => 'Framed-IP-Address' },
  'export_attrs' => { 
    type => 'checkbox', 
    label => 'Export RADIUS group attributes to this database', 
  },
  ;

%info = (
  'svc'      => 'svc_broadband',
  'desc'     => 'Real-time export to SQL-backed RADIUS (such as FreeRadius) for broadband services',
  'options'  => \%options,
  'no_machine' => 1,
  'nas'      => 'Y',
  'notes'    => <<END,
Real-time export of <b>radcheck</b>, <b>radreply</b>, and <b>usergroup</b> 
tables to any SQL database for 
<a href="http://www.freeradius.org/">FreeRADIUS</a>
or <a href="http://radius.innercite.com/">ICRADIUS</a>.
<br><br>

This export is for broadband service access control based on MAC address.  
For a more typical RADIUS export, see sqlradius.
<br><br>

See the
<a href="http://search.cpan.org/dist/DBI/DBI.pm#connect">DBI documentation</a>
and the
<a href="http://search.cpan.org/search?mode=module&query=DBD%3A%3A">documentation for your DBD</a>
for the exact syntax of a DBI data source.

END
);

sub rebless { shift; }

sub export_username {
  my($self, $svc_broadband) = (shift, shift);
  $svc_broadband->mac_addr_formatted(
    $self->option('mac_case'), $self->option('mac_delimiter')
  );
}

sub radius_reply {
  my($self, $svc_broadband) = (shift, shift);
  # start with attributes the service wants
  my %reply = $self->NEXT::radius_reply($svc_broadband);
  # add export-specific stuff
  if (  length($self->option('ip_addr_as',1)) 
    and length($svc_broadband->ip_addr) ) {
    $reply{$self->option('ip_addr_as')} = $svc_broadband->ip_addr;
  }
  %reply;
}

sub radius_check {
  my($self, $svc_broadband) = (shift, shift);

  my %check = $self->SUPER::radius_check($svc_broadband);
  my $password_attrib = $conf->config('radius-password') || 'Password';
  if ( $self->option('mac_as_password') ) {
    $check{$password_attrib} = $self->export_username($svc_broadband);
  }
  elsif ( length( $self->option('radius_password',1)) ) {
    $check{$password_attrib} = $self->option('radius_password');
  }
  %check;
}

sub radius_check_suspended {
  my($self, $svc_broadband) = (shift, shift);

  return () unless $self->option('mac_as_password')
                || length( $self->option('radius_password',1));

  my $password_attrib = $conf->config('radius-password') || 'Password';
  (
    $password_attrib => join('',map($pw_set[ int(rand $#pw_set) ], (0..7) ) )
  );
}

#false laziness w/sqlradius.pm
sub _export_suspend {
  my( $self, $svc_broadband ) = (shift, shift);

  return '' if $self->option('skip_provisioning');

  local $SIG{HUP} = 'IGNORE';
  local $SIG{INT} = 'IGNORE';
  local $SIG{QUIT} = 'IGNORE';
  local $SIG{TERM} = 'IGNORE';
  local $SIG{TSTP} = 'IGNORE';
  local $SIG{PIPE} = 'IGNORE';

  my $oldAutoCommit = $FS::UID::AutoCommit;
  local $FS::UID::AutoCommit = 0;
  my $dbh = dbh;

  my @newgroups = $self->suspended_usergroups($svc_broadband);

  unless (@newgroups) { #don't change password if assigning to a suspended group

    my $err_or_queue = $self->sqlradius_queue(
       $svc_broadband->svcnum, 'insert',
      'check', $self->export_username($svc_broadband),
      $self->radius_check_suspended($svc_broadband)
    );
    unless ( ref($err_or_queue) ) {
      $dbh->rollback if $oldAutoCommit;
      return $err_or_queue;
    }

  }

  my $error =
    $self->sqlreplace_usergroups(
      $svc_broadband->svcnum,
      $self->export_username($svc_broadband),
      '',
      [ $svc_broadband->radius_groups('hashref') ],
      \@newgroups,
    );
  if ( $error ) {
    $dbh->rollback if $oldAutoCommit;
    return $error;
  }
  $dbh->commit or die $dbh->errstr if $oldAutoCommit;

  '';
}

sub update_svc {} #do nothing

1;