4 use base qw( FS::svc_Domain_Mixin
10 use vars qw( $DEBUG $me $conf $skip_fuzzyfiles
11 $dir_prefix @shells $usernamemin
12 $usernamemax $passwordmin $passwordmax
13 $username_ampersand $username_letter $username_letterfirst
14 $username_noperiod $username_nounderscore $username_nodash
15 $username_uppercase $username_percent $username_colon
16 $username_slash $username_equals $username_pound
18 $password_noampersand $password_noexclamation
19 $warning_template $warning_from $warning_subject $warning_mimetype
22 $radius_password $radius_ip
25 use Scalar::Util qw( blessed );
30 use Crypt::PasswdMD5 1.2;
31 use Digest::SHA 'sha1_base64';
32 use Digest::MD5 'md5_base64';
35 use Authen::Passphrase;
36 use FS::UID qw( datasrc driver_name );
38 use FS::Record qw( qsearch qsearchs fields dbh dbdef );
39 use FS::Msgcat qw(gettext);
40 use FS::UI::bytecount;
45 use FS::cust_main_invoice;
50 use FS::radius_usergroup;
61 $me = '[FS::svc_acct]';
63 #ask FS::UID to run this stuff for us later
64 FS::UID->install_callback( sub {
66 $dir_prefix = $conf->config('home');
67 @shells = $conf->config('shells');
68 $usernamemin = $conf->config('usernamemin') || 2;
69 $usernamemax = $conf->config('usernamemax');
70 $passwordmin = $conf->config('passwordmin'); # || 6;
72 $passwordmin = ( defined($passwordmin) && $passwordmin =~ /\d+/ )
75 $passwordmax = $conf->config('passwordmax') || 8;
76 $username_letter = $conf->exists('username-letter');
77 $username_letterfirst = $conf->exists('username-letterfirst');
78 $username_noperiod = $conf->exists('username-noperiod');
79 $username_nounderscore = $conf->exists('username-nounderscore');
80 $username_nodash = $conf->exists('username-nodash');
81 $username_uppercase = $conf->exists('username-uppercase');
82 $username_ampersand = $conf->exists('username-ampersand');
83 $username_percent = $conf->exists('username-percent');
84 $username_colon = $conf->exists('username-colon');
85 $username_slash = $conf->exists('username-slash');
86 $username_equals = $conf->exists('username-equals');
87 $username_pound = $conf->exists('username-pound');
88 $username_exclamation = $conf->exists('username-exclamation');
89 $password_noampersand = $conf->exists('password-noexclamation');
90 $password_noexclamation = $conf->exists('password-noexclamation');
91 $dirhash = $conf->config('dirhash') || 0;
92 if ( $conf->exists('warning_email') ) {
93 $warning_template = new Text::Template (
95 SOURCE => [ map "$_\n", $conf->config('warning_email') ]
96 ) or warn "can't create warning email template: $Text::Template::ERROR";
97 $warning_from = $conf->config('warning_email-from'); # || 'your-isp-is-dum'
98 $warning_subject = $conf->config('warning_email-subject') || 'Warning';
99 $warning_mimetype = $conf->config('warning_email-mimetype') || 'text/plain';
100 $warning_cc = $conf->config('warning_email-cc');
102 $warning_template = '';
104 $warning_subject = '';
105 $warning_mimetype = '';
108 $smtpmachine = $conf->config('smtpmachine');
109 $radius_password = $conf->config('radius-password') || 'Password';
110 $radius_ip = $conf->config('radius-ip') || 'Framed-IP-Address';
111 @pw_set = ( 'A'..'Z' ) if $conf->exists('password-generated-allcaps');
115 @saltset = ( 'a'..'z' , 'A'..'Z' , '0'..'9' , '.' , '/' );
116 @pw_set = ( 'a'..'z', 'A'..'Z', '0'..'9', '(', ')', '#', '.', ',' );
120 my ( $hashref, $cache ) = @_;
121 if ( $hashref->{'svc_acct_svcnum'} ) {
122 $self->{'_domsvc'} = FS::svc_domain->new( {
123 'svcnum' => $hashref->{'domsvc'},
124 'domain' => $hashref->{'svc_acct_domain'},
125 'catchall' => $hashref->{'svc_acct_catchall'},
132 FS::svc_acct - Object methods for svc_acct records
138 $record = new FS::svc_acct \%hash;
139 $record = new FS::svc_acct { 'column' => 'value' };
141 $error = $record->insert;
143 $error = $new_record->replace($old_record);
145 $error = $record->delete;
147 $error = $record->check;
149 $error = $record->suspend;
151 $error = $record->unsuspend;
153 $error = $record->cancel;
155 %hash = $record->radius;
157 %hash = $record->radius_reply;
159 %hash = $record->radius_check;
161 $domain = $record->domain;
163 $svc_domain = $record->svc_domain;
165 $email = $record->email;
167 $seconds_since = $record->seconds_since($timestamp);
171 An FS::svc_acct object represents an account. FS::svc_acct inherits from
172 FS::svc_Common. The following fields are currently supported:
178 Primary key (assigned automatcially for new accounts)
186 =item _password_encoding
188 plain, crypt, ldap (or empty for autodetection)
196 Point of presence (see L<FS::svc_acct_pop>)
208 set automatically if blank (and uid is not)
228 svcnum from svc_domain
232 Optional svcnum from svc_pbx
234 =item radius_I<Radius_Attribute>
236 I<Radius-Attribute> (reply)
238 =item rc_I<Radius_Attribute>
240 I<Radius-Attribute> (check)
250 Creates a new account. To add the account to the database, see L<"insert">.
257 'longname_plural' => 'Access accounts and mailboxes',
258 'sorts' => [ 'username', 'uid', 'seconds', 'last_login' ],
259 'display_weight' => 10,
260 'cancel_weight' => 50,
261 'ip_field' => 'slipip',
263 'dir' => 'Home directory',
266 def_info => 'set to fixed and blank for no UIDs',
269 'slipip' => 'IP address',
270 # 'popnum' => qq!<A HREF="$p/browse/svc_acct_pop.cgi/">POP number</A>!,
272 label => 'Access number',
274 select_table => 'svc_acct_pop',
275 select_key => 'popnum',
276 select_label => 'city',
282 disable_default => 1,
286 'password_selfchange' => { label => 'Password modification',
289 'password_recover' => { label => 'Password recovery',
293 label => 'Quota', #Mail storage limit
295 disable_inventory => 1,
299 label => 'File storage limit',
301 disable_inventory => 1,
305 label => 'Number of files limit',
307 disable_inventory => 1,
311 label => 'File size limit',
313 disable_inventory => 1,
316 '_password' => 'Password',
319 def_info => 'when blank, defaults to UID',
324 def_info => 'set to blank for no shell tracking',
326 #select_list => [ $conf->config('shells') ],
327 select_list => [ $conf ? $conf->config('shells') : () ],
328 disable_inventory => 1,
331 'finger' => 'Real name', # (GECOS)',
335 select_table => 'svc_domain',
336 select_key => 'svcnum',
337 select_label => 'domain',
338 disable_inventory => 1,
340 'pbxsvc' => { label => 'PBX',
341 type => 'select-svc_pbx.html',
342 disable_inventory => 1,
343 disable_select => 1, #UI wonky, pry works otherwise
345 'sectornum' => 'Tower sector',
347 label => 'RADIUS groups',
348 type => 'select-radius_group.html',
349 disable_inventory => 1,
353 'seconds' => { label => 'Seconds',
354 label_sort => 'with Time Remaining',
356 disable_inventory => 1,
358 disable_part_svc_column => 1,
360 'upbytes' => { label => 'Upload',
362 disable_inventory => 1,
364 'format' => \&FS::UI::bytecount::display_bytecount,
365 'parse' => \&FS::UI::bytecount::parse_bytecount,
366 disable_part_svc_column => 1,
368 'downbytes' => { label => 'Download',
370 disable_inventory => 1,
372 'format' => \&FS::UI::bytecount::display_bytecount,
373 'parse' => \&FS::UI::bytecount::parse_bytecount,
374 disable_part_svc_column => 1,
376 'totalbytes'=> { label => 'Total up and download',
378 disable_inventory => 1,
380 'format' => \&FS::UI::bytecount::display_bytecount,
381 'parse' => \&FS::UI::bytecount::parse_bytecount,
382 disable_part_svc_column => 1,
384 'seconds_threshold' => { label => 'Seconds threshold',
386 disable_inventory => 1,
388 disable_part_svc_column => 1,
390 'upbytes_threshold' => { label => 'Upload threshold',
392 disable_inventory => 1,
394 'format' => \&FS::UI::bytecount::display_bytecount,
395 'parse' => \&FS::UI::bytecount::parse_bytecount,
396 disable_part_svc_column => 1,
398 'downbytes_threshold' => { label => 'Download threshold',
400 disable_inventory => 1,
402 'format' => \&FS::UI::bytecount::display_bytecount,
403 'parse' => \&FS::UI::bytecount::parse_bytecount,
404 disable_part_svc_column => 1,
406 'totalbytes_threshold'=> { label => 'Total up and download threshold',
408 disable_inventory => 1,
410 'format' => \&FS::UI::bytecount::display_bytecount,
411 'parse' => \&FS::UI::bytecount::parse_bytecount,
412 disable_part_svc_column => 1,
415 label => 'Last login',
419 label => 'Last logout',
424 label => 'Communigate aliases',
426 disable_inventory => 1,
431 label => 'Communigate account type',
433 select_list => [qw( MultiMailbox TextMailbox MailDirMailbox AGrade BGrade CGrade )],
434 disable_inventory => 1,
437 'cgp_accessmodes' => {
438 label => 'Communigate enabled services',
439 type => 'communigate_pro-accessmodes',
440 disable_inventory => 1,
443 'cgp_rulesallowed' => {
444 label => 'Allowed mail rules',
446 select_list => [ '', 'No', 'Filter Only', 'All But Exec', 'Any' ],
447 disable_inventory => 1,
450 'cgp_rpopallowed' => { label => 'RPOP modifications',
453 'cgp_mailtoall' => { label => 'Accepts mail to "all"',
456 'cgp_addmailtrailer' => { label => 'Add trailer to sent mail',
459 'cgp_archiveafter' => {
460 label => 'Archive messages after',
463 -2 => 'default(730 days)',
470 1209600 => '2 weeks',
471 2592000 => '30 days',
472 7776000 => '90 days',
473 15552000 => '180 days',
474 31536000 => '365 days',
475 63072000 => '730 days',
477 disable_inventory => 1,
483 'cgp_deletemode' => {
484 label => 'Communigate message delete method',
486 select_list => [ 'Move To Trash', 'Immediately', 'Mark' ],
487 disable_inventory => 1,
490 'cgp_emptytrash' => {
491 label => 'Communigate on logout remove trash',
493 select_list => __PACKAGE__->cgp_emptytrash_values,
494 disable_inventory => 1,
498 label => 'Communigate language',
500 select_list => [ '', qw( English Arabic Chinese Dutch French German Hebrew Italian Japanese Portuguese Russian Slovak Spanish Thai ) ],
501 disable_inventory => 1,
505 label => 'Communigate time zone',
507 select_list => __PACKAGE__->cgp_timezone_values,
508 disable_inventory => 1,
512 label => 'Communigate layout',
514 select_list => [ '', '***', 'GoldFleece', 'Skin2' ],
515 disable_inventory => 1,
518 'cgp_prontoskinname' => {
519 label => 'Communigate Pronto style',
521 select_list => [ '', 'Pronto', 'Pronto-darkflame', 'Pronto-steel', 'Pronto-twilight', ],
522 disable_inventory => 1,
525 'cgp_sendmdnmode' => {
526 label => 'Communigate send read receipts',
528 select_list => [ '', 'Never', 'Manually', 'Automatically' ],
529 disable_inventory => 1,
540 sub table { 'svc_acct'; }
542 sub table_dupcheck_fields { ( 'username', 'domsvc' ); }
545 shift->_lastlog('in', @_);
549 shift->_lastlog('out', @_);
553 my( $self, $op, $time ) = @_;
555 if ( defined($time) ) {
556 warn "$me last_log$op called on svcnum ". $self->svcnum.
557 ' ('. $self->email. "): $time\n"
562 my $sql = "UPDATE svc_acct SET last_log$op = ? WHERE svcnum = ?";
566 my $sth = $dbh->prepare( $sql )
567 or die "Error preparing $sql: ". $dbh->errstr;
568 my $rv = $sth->execute($time, $self->svcnum);
569 die "Error executing $sql: ". $sth->errstr
571 die "Can't update last_log$op for svcnum". $self->svcnum
574 $self->{'Hash'}->{"last_log$op"} = $time;
576 $self->getfield("last_log$op");
580 =item search_sql STRING
582 Class method which returns an SQL fragment to search for the given string.
587 my( $class, $string ) = @_;
588 if ( $string =~ /^([^@]+)@([^@]+)$/ ) {
589 my( $username, $domain ) = ( $1, $2 );
590 my $q_username = dbh->quote($username);
591 my @svc_domain = qsearch('svc_domain', { 'domain' => $domain } );
593 "svc_acct.username = $q_username AND ( ".
594 join( ' OR ', map { "svc_acct.domsvc = ". $_->svcnum; } @svc_domain ).
599 } elsif ( $string =~ /^(\d{1,3}\.){3}\d{1,3}$/ ) {
601 $class->search_sql_field('slipip', $string ).
603 $class->search_sql_field('username', $string ).
606 $class->search_sql_field('username', $string);
610 =item label [ END_TIMESTAMP [ START_TIMESTAMP ] ]
612 Returns the "username@domain" string for this account.
614 END_TIMESTAMP and START_TIMESTAMP can optionally be passed when dealing with
624 =item label_long [ END_TIMESTAMP [ START_TIMESTAMP ] ]
626 Returns a longer string label for this acccount ("Real Name <username@domain>"
627 if available, or "username@domain").
629 END_TIMESTAMP and START_TIMESTAMP can optionally be passed when dealing with
636 my $label = $self->label(@_);
637 my $finger = $self->finger;
638 return $label unless $finger =~ /\S/;
639 my $maxlen = 40 - length($label) - length($self->cust_svc->part_svc->svc);
640 $finger = substr($finger, 0, $maxlen-3).'...' if length($finger) > $maxlen;
644 =item insert [ , OPTION => VALUE ... ]
646 Adds this account to the database. If there is an error, returns the error,
647 otherwise returns false.
649 The additional fields pkgnum and svcpart (see L<FS::cust_svc>) should be
650 defined. An FS::cust_svc record will be created and inserted.
652 The additional field I<usergroup> can optionally be defined; if so it should
653 contain an arrayref of group names. See L<FS::radius_usergroup>.
655 The additional field I<child_objects> can optionally be defined; if so it
656 should contain an arrayref of FS::tablename objects. They will have their
657 svcnum fields set and will be inserted after this record, but before any
658 exports are run. Each element of the array can also optionally be a
659 two-element array reference containing the child object and the name of an
660 alternate field to be filled in with the newly-inserted svcnum, for example
661 C<[ $svc_forward, 'srcsvc' ]>
663 Currently available options are: I<depend_jobnum>
665 If I<depend_jobnum> is set (to a scalar jobnum or an array reference of
666 jobnums), all provisioning jobs will have a dependancy on the supplied
667 jobnum(s) (they will not run until the specific job(s) complete(s)).
669 (TODOC: L<FS::queue> and L<freeside-queued>)
671 (TODOC: new exports!)
680 warn "[$me] insert called on $self: ". Dumper($self).
681 "\nwith options: ". Dumper(%options);
684 local $SIG{HUP} = 'IGNORE';
685 local $SIG{INT} = 'IGNORE';
686 local $SIG{QUIT} = 'IGNORE';
687 local $SIG{TERM} = 'IGNORE';
688 local $SIG{TSTP} = 'IGNORE';
689 local $SIG{PIPE} = 'IGNORE';
691 my $oldAutoCommit = $FS::UID::AutoCommit;
692 local $FS::UID::AutoCommit = 0;
696 my $error = $self->SUPER::insert( # usergroup is here
697 'jobnums' => \@jobnums,
698 'child_objects' => $self->child_objects,
702 $dbh->rollback if $oldAutoCommit;
706 unless ( $skip_fuzzyfiles ) {
707 $error = $self->queue_fuzzyfiles_update;
709 $dbh->rollback if $oldAutoCommit;
710 return "updating fuzzy search cache: $error";
714 my $cust_pkg = $self->cust_svc->cust_pkg;
717 my $cust_main = $cust_pkg->cust_main;
718 my $agentnum = $cust_main->agentnum;
720 if ( $conf->exists('emailinvoiceautoalways')
721 || $conf->exists('emailinvoiceauto')
722 && ! $cust_main->invoicing_list_emailonly
724 my @invoicing_list = $cust_main->invoicing_list;
725 push @invoicing_list, $self->email;
726 $cust_main->invoicing_list(\@invoicing_list);
730 my @welcome_exclude_svcparts = $conf->config('svc_acct_welcome_exclude');
731 unless ( grep { $_ eq $self->svcpart } @welcome_exclude_svcparts ) {
733 my $msgnum = $conf->config('welcome_msgnum', $agentnum);
735 my $msg_template = qsearchs('msg_template', { msgnum => $msgnum });
736 $error = $msg_template->send('cust_main' => $cust_main,
740 my ($to,$welcome_template,$welcome_from,$welcome_subject,$welcome_subject_template,$welcome_mimetype)
741 = ('','','','','','');
743 if ( $conf->exists('welcome_email', $agentnum) ) {
744 $welcome_template = new Text::Template (
746 SOURCE => [ map "$_\n", $conf->config('welcome_email', $agentnum) ]
747 ) or warn "can't create welcome email template: $Text::Template::ERROR";
748 $welcome_from = $conf->config('welcome_email-from', $agentnum);
749 # || 'your-isp-is-dum'
750 $welcome_subject = $conf->config('welcome_email-subject', $agentnum)
752 $welcome_subject_template = new Text::Template (
754 SOURCE => $welcome_subject,
755 ) or warn "can't create welcome email subject template: $Text::Template::ERROR";
756 $welcome_mimetype = $conf->config('welcome_email-mimetype', $agentnum)
759 if ( $welcome_template ) {
760 my $to = join(', ', grep { $_ !~ /^(POST|FAX)$/ } $cust_main->invoicing_list );
764 'custnum' => $self->custnum,
765 'username' => $self->username,
766 'password' => $self->_password,
767 'first' => $cust_main->first,
768 'last' => $cust_main->getfield('last'),
769 'pkg' => $cust_pkg->part_pkg->pkg,
771 my $wqueue = new FS::queue {
772 'svcnum' => $self->svcnum,
773 'job' => 'FS::svc_acct::send_email'
775 my $error = $wqueue->insert(
777 'from' => $welcome_from,
778 'subject' => $welcome_subject_template->fill_in( HASH => \%hash, ),
779 'mimetype' => $welcome_mimetype,
780 'body' => $welcome_template->fill_in( HASH => \%hash, ),
783 $dbh->rollback if $oldAutoCommit;
784 return "error queuing welcome email: $error";
787 if ( $options{'depend_jobnum'} ) {
788 warn "$me depend_jobnum found; adding to welcome email dependancies"
790 if ( ref($options{'depend_jobnum'}) ) {
791 warn "$me adding jobs ". join(', ', @{$options{'depend_jobnum'}} ).
792 "to welcome email dependancies"
794 push @jobnums, @{ $options{'depend_jobnum'} };
796 warn "$me adding job $options{'depend_jobnum'} ".
797 "to welcome email dependancies"
799 push @jobnums, $options{'depend_jobnum'};
803 foreach my $jobnum ( @jobnums ) {
804 my $error = $wqueue->depend_insert($jobnum);
806 $dbh->rollback if $oldAutoCommit;
807 return "error queuing welcome email job dependancy: $error";
813 } # if $welcome_template
818 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
822 # set usage fields and thresholds if unset but set in a package def
823 # AND the package already has a last bill date (otherwise they get double added)
824 sub preinsert_hook_first {
827 return '' unless $self->pkgnum;
829 my $cust_pkg = qsearchs( 'cust_pkg', { 'pkgnum' => $self->pkgnum } );
830 return '' unless $cust_pkg && $cust_pkg->last_bill;
832 my $part_pkg = $cust_pkg->part_pkg;
833 return '' unless $part_pkg && $part_pkg->can('usage_valuehash');
835 my %values = $part_pkg->usage_valuehash;
836 my $multiplier = $conf->exists('svc_acct-usage_threshold')
837 ? 1 - $conf->config('svc_acct-usage_threshold')/100
838 : 0.20; #doesn't matter
840 foreach ( keys %values ) {
841 next if $self->getfield($_);
842 $self->setfield( $_, $values{$_} );
843 $self->setfield( $_. '_threshold', int( $values{$_} * $multiplier ) )
844 if $conf->exists('svc_acct-usage_threshold');
852 Deletes this account from the database. If there is an error, returns the
853 error, otherwise returns false.
855 The corresponding FS::cust_svc record will be deleted as well.
857 (TODOC: new exports!)
864 return "can't delete system account" if $self->_check_system;
866 return "Can't delete an account which is a (svc_forward) source!"
867 if qsearch( 'svc_forward', { 'srcsvc' => $self->svcnum } );
869 return "Can't delete an account which is a (svc_forward) destination!"
870 if qsearch( 'svc_forward', { 'dstsvc' => $self->svcnum } );
872 return "Can't delete an account with (svc_www) web service!"
873 if qsearch( 'svc_www', { 'usersvc' => $self->svcnum } );
875 # what about records in session ? (they should refer to history table)
877 local $SIG{HUP} = 'IGNORE';
878 local $SIG{INT} = 'IGNORE';
879 local $SIG{QUIT} = 'IGNORE';
880 local $SIG{TERM} = 'IGNORE';
881 local $SIG{TSTP} = 'IGNORE';
882 local $SIG{PIPE} = 'IGNORE';
884 my $oldAutoCommit = $FS::UID::AutoCommit;
885 local $FS::UID::AutoCommit = 0;
888 foreach my $cust_main_invoice (
889 qsearch( 'cust_main_invoice', { 'dest' => $self->svcnum } )
891 unless ( defined($cust_main_invoice) ) {
892 warn "WARNING: something's wrong with qsearch";
895 my %hash = $cust_main_invoice->hash;
896 $hash{'dest'} = $self->email;
897 my $new = new FS::cust_main_invoice \%hash;
898 my $error = $new->replace($cust_main_invoice);
900 $dbh->rollback if $oldAutoCommit;
905 foreach my $svc_domain (
906 qsearch( 'svc_domain', { 'catchall' => $self->svcnum } )
908 my %hash = new FS::svc_domain->hash;
909 $hash{'catchall'} = '';
910 my $new = new FS::svc_domain \%hash;
911 my $error = $new->replace($svc_domain);
913 $dbh->rollback if $oldAutoCommit;
918 my $error = $self->SUPER::delete; # usergroup here
920 $dbh->rollback if $oldAutoCommit;
924 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
928 =item replace OLD_RECORD
930 Replaces OLD_RECORD with this one in the database. If there is an error,
931 returns the error, otherwise returns false.
933 The additional field I<usergroup> can optionally be defined; if so it should
934 contain an arrayref of group names. See L<FS::radius_usergroup>.
942 my $old = ( blessed($_[0]) && $_[0]->isa('FS::Record') )
946 warn "$me replacing $old with $new\n" if $DEBUG;
950 return "can't modify system account" if $old->_check_system;
953 #no warnings 'numeric'; #alas, a 5.006-ism
956 foreach my $xid (qw( uid gid )) {
958 return "Can't change $xid!"
959 if ! $conf->exists("svc_acct-edit_$xid")
960 && $old->$xid() != $new->$xid()
961 && $new->cust_svc->part_svc->part_svc_column($xid)->columnflag ne 'F'
966 return "can't change username"
967 if $old->username ne $new->username
968 && $conf->exists('svc_acct-no_edit_username');
970 #change homdir when we change username
971 $new->setfield('dir', '') if $old->username ne $new->username;
973 local $SIG{HUP} = 'IGNORE';
974 local $SIG{INT} = 'IGNORE';
975 local $SIG{QUIT} = 'IGNORE';
976 local $SIG{TERM} = 'IGNORE';
977 local $SIG{TSTP} = 'IGNORE';
978 local $SIG{PIPE} = 'IGNORE';
980 my $oldAutoCommit = $FS::UID::AutoCommit;
981 local $FS::UID::AutoCommit = 0;
984 $error = $new->SUPER::replace($old, @_); # usergroup here
986 $dbh->rollback if $oldAutoCommit;
987 return $error if $error;
990 if ( $new->username ne $old->username && ! $skip_fuzzyfiles ) {
991 $error = $new->queue_fuzzyfiles_update;
993 $dbh->rollback if $oldAutoCommit;
994 return "updating fuzzy search cache: $error";
998 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
1002 =item queue_fuzzyfiles_update
1004 Used by insert & replace to update the fuzzy search cache
1008 sub queue_fuzzyfiles_update {
1011 local $SIG{HUP} = 'IGNORE';
1012 local $SIG{INT} = 'IGNORE';
1013 local $SIG{QUIT} = 'IGNORE';
1014 local $SIG{TERM} = 'IGNORE';
1015 local $SIG{TSTP} = 'IGNORE';
1016 local $SIG{PIPE} = 'IGNORE';
1018 my $oldAutoCommit = $FS::UID::AutoCommit;
1019 local $FS::UID::AutoCommit = 0;
1022 my $queue = new FS::queue {
1023 'svcnum' => $self->svcnum,
1024 'job' => 'FS::svc_acct::append_fuzzyfiles'
1026 my $error = $queue->insert($self->username);
1028 $dbh->rollback if $oldAutoCommit;
1029 return "queueing job (transaction rolled back): $error";
1032 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
1040 Suspends this account by calling export-specific suspend hooks. If there is
1041 an error, returns the error, otherwise returns false.
1043 Called by the suspend method of FS::cust_pkg (see L<FS::cust_pkg>).
1049 return "can't suspend system account" if $self->_check_system;
1050 $self->SUPER::suspend(@_);
1055 Unsuspends this account by by calling export-specific suspend hooks. If there
1056 is an error, returns the error, otherwise returns false.
1058 Called by the unsuspend method of FS::cust_pkg (see L<FS::cust_pkg>).
1064 my %hash = $self->hash;
1065 if ( $hash{_password} =~ /^\*SUSPENDED\* (.*)$/ ) {
1066 $hash{_password} = $1;
1067 my $new = new FS::svc_acct ( \%hash );
1068 my $error = $new->replace($self);
1069 return $error if $error;
1072 $self->SUPER::unsuspend(@_);
1077 Called by the cancel method of FS::cust_pkg (see L<FS::cust_pkg>).
1079 If the B<auto_unset_catchall> configuration option is set, this method will
1080 automatically remove any references to the canceled service in the catchall
1081 field of svc_domain. This allows packages that contain both a svc_domain and
1082 its catchall svc_acct to be canceled in one step.
1087 # Only one thing to do at this level
1089 foreach my $svc_domain (
1090 qsearch( 'svc_domain', { catchall => $self->svcnum } ) ) {
1091 if($conf->exists('auto_unset_catchall')) {
1092 my %hash = $svc_domain->hash;
1093 $hash{catchall} = '';
1094 my $new = new FS::svc_domain ( \%hash );
1095 my $error = $new->replace($svc_domain);
1096 return $error if $error;
1098 return "cannot unprovision svc_acct #".$self->svcnum.
1099 " while assigned as catchall for svc_domain #".$svc_domain->svcnum;
1103 $self->SUPER::cancel(@_);
1109 Checks all fields to make sure this is a valid service. If there is an error,
1110 returns the error, otherwise returns false. Called by the insert and replace
1113 Sets any fixed values; see L<FS::part_svc>.
1120 my($recref) = $self->hashref;
1122 my $x = $self->setfixed;
1123 return $x unless ref($x);
1126 my $error = $self->ut_numbern('svcnum')
1127 #|| $self->ut_number('domsvc')
1128 || $self->ut_foreign_key( 'domsvc', 'svc_domain', 'svcnum' )
1129 || $self->ut_foreign_keyn('pbxsvc', 'svc_pbx', 'svcnum' )
1130 || $self->ut_foreign_keyn('sectornum','tower_sector','sectornum')
1131 || $self->ut_textn('sec_phrase')
1132 || $self->ut_snumbern('seconds')
1133 || $self->ut_snumbern('upbytes')
1134 || $self->ut_snumbern('downbytes')
1135 || $self->ut_snumbern('totalbytes')
1136 || $self->ut_snumbern('seconds_threshold')
1137 || $self->ut_snumbern('upbytes_threshold')
1138 || $self->ut_snumbern('downbytes_threshold')
1139 || $self->ut_snumbern('totalbytes_threshold')
1140 || $self->ut_enum('_password_encoding', ['',qw(plain crypt ldap)])
1141 || $self->ut_enum('password_selfchange', [ '', 'Y' ])
1142 || $self->ut_enum('password_recover', [ '', 'Y' ])
1144 || $self->ut_anything('cf_privatekey')
1146 || $self->ut_textn('cgp_accessmodes')
1147 || $self->ut_alphan('cgp_type')
1148 || $self->ut_textn('cgp_aliases' ) #well
1150 || $self->ut_alphasn('cgp_rulesallowed')
1151 || $self->ut_enum('cgp_rpopallowed', [ '', 'Y' ])
1152 || $self->ut_enum('cgp_mailtoall', [ '', 'Y' ])
1153 || $self->ut_enum('cgp_addmailtrailer', [ '', 'Y' ])
1154 || $self->ut_snumbern('cgp_archiveafter')
1156 || $self->ut_alphasn('cgp_deletemode')
1157 || $self->ut_enum('cgp_emptytrash', $self->cgp_emptytrash_values)
1158 || $self->ut_alphan('cgp_language')
1159 || $self->ut_textn('cgp_timezone')
1160 || $self->ut_textn('cgp_skinname')
1161 || $self->ut_textn('cgp_prontoskinname')
1162 || $self->ut_alphan('cgp_sendmdnmode')
1164 return $error if $error;
1167 local $username_letter = $username_letter;
1168 local $username_uppercase = $username_uppercase;
1169 if ($self->svcnum) {
1170 my $cust_svc = $self->cust_svc
1171 or return "no cust_svc record found for svcnum ". $self->svcnum;
1172 my $cust_pkg = $cust_svc->cust_pkg;
1174 if ($self->pkgnum) {
1175 $cust_pkg = qsearchs('cust_pkg', { 'pkgnum' => $self->pkgnum } );#complain?
1179 $conf->exists('username-letter', $cust_pkg->cust_main->agentnum);
1180 $username_uppercase =
1181 $conf->exists('username-uppercase', $cust_pkg->cust_main->agentnum);
1184 my $ulen = $usernamemax || $self->dbdef_table->column('username')->length;
1186 $recref->{username} =~ /^([a-z0-9_\-\.\&\%\:\/\=\#\!]{$usernamemin,$ulen})$/i
1187 or return gettext('illegal_username'). " ($usernamemin-$ulen): ". $recref->{username};
1188 $recref->{username} = $1;
1190 my $uerror = gettext('illegal_username'). ': '. $recref->{username};
1192 unless ( $username_uppercase ) {
1193 $recref->{username} =~ /[A-Z]/ and return $uerror;
1195 if ( $username_letterfirst ) {
1196 $recref->{username} =~ /^[a-z]/ or return $uerror;
1197 } elsif ( $username_letter ) {
1198 $recref->{username} =~ /[a-z]/ or return $uerror;
1200 if ( $username_noperiod ) {
1201 $recref->{username} =~ /\./ and return $uerror;
1203 if ( $username_nounderscore ) {
1204 $recref->{username} =~ /_/ and return $uerror;
1206 if ( $username_nodash ) {
1207 $recref->{username} =~ /\-/ and return $uerror;
1209 unless ( $username_ampersand ) {
1210 $recref->{username} =~ /\&/ and return $uerror;
1212 unless ( $username_percent ) {
1213 $recref->{username} =~ /\%/ and return $uerror;
1215 unless ( $username_colon ) {
1216 $recref->{username} =~ /\:/ and return $uerror;
1218 unless ( $username_slash ) {
1219 $recref->{username} =~ /\// and return $uerror;
1221 unless ( $username_equals ) {
1222 $recref->{username} =~ /\=/ and return $uerror;
1224 unless ( $username_pound ) {
1225 $recref->{username} =~ /\#/ and return $uerror;
1227 unless ( $username_exclamation ) {
1228 $recref->{username} =~ /\!/ and return $uerror;
1232 $recref->{popnum} =~ /^(\d*)$/ or return "Illegal popnum: ".$recref->{popnum};
1233 $recref->{popnum} = $1;
1234 return "Unknown popnum" unless
1235 ! $recref->{popnum} ||
1236 qsearchs('svc_acct_pop',{'popnum'=> $recref->{popnum} } );
1238 unless ( $part_svc->part_svc_column('uid')->columnflag eq 'F' ) {
1240 $recref->{uid} =~ /^(\d*)$/ or return "Illegal uid";
1241 $recref->{uid} = $1 eq '' ? $self->unique('uid') : $1;
1243 $recref->{gid} =~ /^(\d*)$/ or return "Illegal gid";
1244 $recref->{gid} = $1 eq '' ? $recref->{uid} : $1;
1245 #not all systems use gid=uid
1246 #you can set a fixed gid in part_svc
1248 return "Only root can have uid 0"
1249 if $recref->{uid} == 0
1250 && $recref->{username} !~ /^(root|toor|smtp)$/;
1252 unless ( $recref->{username} eq 'sync' ) {
1253 if ( grep $_ eq $recref->{shell}, @shells ) {
1254 $recref->{shell} = (grep $_ eq $recref->{shell}, @shells)[0];
1256 return "Illegal shell \`". $self->shell. "\'; ".
1257 "shells configuration value contains: @shells";
1260 $recref->{shell} = '/bin/sync';
1264 $recref->{gid} ne '' ?
1265 return "Can't have gid without uid" : ( $recref->{gid}='' );
1266 #$recref->{dir} ne '' ?
1267 # return "Can't have directory without uid" : ( $recref->{dir}='' );
1268 $recref->{shell} ne '' ?
1269 return "Can't have shell without uid" : ( $recref->{shell}='' );
1272 unless ( $part_svc->part_svc_column('dir')->columnflag eq 'F' ) {
1274 $recref->{dir} =~ /^([\/\w\-\.\&\:\#]*)$/
1275 or return "Illegal directory: ". $recref->{dir};
1276 $recref->{dir} = $1;
1277 return "Illegal directory"
1278 if $recref->{dir} =~ /(^|\/)\.+(\/|$)/; #no .. component
1279 return "Illegal directory"
1280 if $recref->{dir} =~ /\&/ && ! $username_ampersand;
1281 unless ( $recref->{dir} ) {
1282 $recref->{dir} = $dir_prefix . '/';
1283 if ( $dirhash > 0 ) {
1284 for my $h ( 1 .. $dirhash ) {
1285 $recref->{dir} .= substr($recref->{username}, $h-1, 1). '/';
1287 } elsif ( $dirhash < 0 ) {
1288 for my $h ( reverse $dirhash .. -1 ) {
1289 $recref->{dir} .= substr($recref->{username}, $h, 1). '/';
1292 $recref->{dir} .= $recref->{username};
1298 if ( $self->getfield('finger') eq '' ) {
1299 my $cust_pkg = $self->svcnum
1300 ? $self->cust_svc->cust_pkg
1301 : qsearchs('cust_pkg', { 'pkgnum' => $self->getfield('pkgnum') } );
1303 my $cust_main = $cust_pkg->cust_main;
1304 $self->setfield('finger', $cust_main->first.' '.$cust_main->get('last') );
1307 # $error = $self->ut_textn('finger');
1308 # return $error if $error;
1309 $self->getfield('finger') =~ /^([\w \,\.\-\'\&\t\!\@\#\$\%\(\)\+\;\"\?\/\*\<\>]*)$/
1310 or return "Illegal finger: ". $self->getfield('finger');
1311 $self->setfield('finger', $1);
1313 for (qw( quota file_quota file_maxsize )) {
1314 $recref->{$_} =~ /^(\w*)$/ or return "Illegal $_";
1317 $recref->{file_maxnum} =~ /^\s*(\d*)\s*$/ or return "Illegal file_maxnum";
1318 $recref->{file_maxnum} = $1;
1320 unless ( $part_svc->part_svc_column('slipip')->columnflag eq 'F' ) {
1321 if ( $recref->{slipip} eq '' ) {
1322 $recref->{slipip} = '';
1323 } elsif ( $recref->{slipip} eq '0e0' ) {
1324 $recref->{slipip} = '0e0';
1326 $recref->{slipip} =~ /^(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})$/
1327 or return "Illegal slipip: ". $self->slipip;
1328 $recref->{slipip} = $1;
1333 #arbitrary RADIUS stuff; allow ut_textn for now
1334 foreach ( grep /^radius_/, fields('svc_acct') ) {
1335 $self->ut_textn($_);
1338 # First, if _password is blank, generate one and set default encoding.
1339 if ( ! $recref->{_password} ) {
1340 $error = $self->set_password('');
1342 # But if there's a _password but no encoding, assume it's plaintext and
1343 # set it to default encoding.
1344 elsif ( ! $recref->{_password_encoding} ) {
1345 $error = $self->set_password($recref->{_password});
1347 return $error if $error;
1349 # Next, check _password to ensure compliance with the encoding.
1350 if ( $recref->{_password_encoding} eq 'ldap' ) {
1352 if ( $recref->{_password} =~ /^(\{[\w\-]+\})(!?.{0,64})$/ ) {
1353 $recref->{_password} = uc($1).$2;
1355 return 'Illegal (ldap-encoded) password: '. $recref->{_password};
1358 } elsif ( $recref->{_password_encoding} eq 'crypt' ) {
1360 if ( $recref->{_password} =~
1361 #/^(\$\w+\$.*|[\w\+\/]{13}|_[\w\+\/]{19}|\*)$/
1362 /^(!!?)?(\$\w+\$.*|[\w\+\/\.]{13}|_[\w\+\/\.]{19}|\*)$/
1365 $recref->{_password} = ( defined($1) ? $1 : '' ). $2;
1368 return 'Illegal (crypt-encoded) password: '. $recref->{_password};
1371 } elsif ( $recref->{_password_encoding} eq 'plain' ) {
1372 # Password randomization is now in set_password.
1373 # Strip whitespace characters, check length requirements, etc.
1374 if ( $recref->{_password} =~ /^([^\t\n]{$passwordmin,$passwordmax})$/ ) {
1375 $recref->{_password} = $1;
1377 return gettext('illegal_password'). " $passwordmin-$passwordmax ".
1378 FS::Msgcat::_gettext('illegal_password_characters').
1379 ": ". $recref->{_password};
1382 if ( $password_noampersand ) {
1383 $recref->{_password} =~ /\&/ and return gettext('illegal_password');
1385 if ( $password_noexclamation ) {
1386 $recref->{_password} =~ /\!/ and return gettext('illegal_password');
1390 return "invalid password encoding ('".$recref->{_password_encoding}."'";
1392 $self->SUPER::check;
1397 sub _password_encryption {
1399 my $encoding = lc($self->_password_encoding);
1400 return if !$encoding;
1401 return 'plain' if $encoding eq 'plain';
1402 if($encoding eq 'crypt') {
1403 my $pass = $self->_password;
1404 $pass =~ s/^\*SUSPENDED\* //;
1406 return 'md5' if $pass =~ /^\$1\$/;
1407 #return 'blowfish' if $self->_password =~ /^\$2\$/;
1408 return 'des' if length($pass) == 13;
1411 if($encoding eq 'ldap') {
1412 uc($self->_password) =~ /^\{([\w-]+)\}/;
1413 return 'crypt' if $1 eq 'CRYPT' or $1 eq 'DES';
1414 return 'plain' if $1 eq 'PLAIN' or $1 eq 'CLEARTEXT';
1415 return 'md5' if $1 eq 'MD5';
1416 return 'sha1' if $1 eq 'SHA' or $1 eq 'SHA-1';
1423 sub get_cleartext_password {
1425 if($self->_password_encryption eq 'plain') {
1426 if($self->_password_encoding eq 'ldap') {
1427 $self->_password =~ /\{\w+\}(.*)$/;
1431 return $self->_password;
1440 Set the cleartext password for the account. If _password_encoding is set, the
1441 new password will be encoded according to the existing method (including
1442 encryption mode, if it can be determined). Otherwise,
1443 config('default-password-encoding') is used.
1445 If no password is supplied (or a zero-length password when minimum password length
1446 is >0), one will be generated randomly.
1451 my( $self, $pass ) = ( shift, shift );
1453 warn "[$me] set_password (to $pass) called on $self: ". Dumper($self)
1456 my $failure = gettext('illegal_password'). " $passwordmin-$passwordmax ".
1457 FS::Msgcat::_gettext('illegal_password_characters').
1460 my( $encoding, $encryption ) = ('', '');
1462 if ( $self->_password_encoding ) {
1463 $encoding = $self->_password_encoding;
1464 # identify existing encryption method, try to use it.
1465 $encryption = $self->_password_encryption;
1467 # use the system default
1473 # set encoding to system default
1474 ($encoding, $encryption) =
1475 split(/-/, lc($conf->config('default-password-encoding') || ''));
1476 $encoding ||= 'legacy';
1477 $self->_password_encoding($encoding);
1480 if ( $encoding eq 'legacy' ) {
1482 # The legacy behavior from check():
1483 # If the password is blank, randomize it and set encoding to 'plain'.
1484 if(!defined($pass) or (length($pass) == 0 and $passwordmin)) {
1485 $pass = join('',map($pw_set[ int(rand $#pw_set) ], (0..7) ) );
1486 $self->_password_encoding('plain');
1488 # Prefix + valid-length password
1489 if ( $pass =~ /^((\*SUSPENDED\* |!!?)?)([^\t\n]{$passwordmin,$passwordmax})$/ ) {
1491 $self->_password_encoding('plain');
1492 # Prefix + crypt string
1493 } elsif ( $pass =~ /^((\*SUSPENDED\* |!!?)?)([\w\.\/\$\;\+]{13,64})$/ ) {
1495 $self->_password_encoding('crypt');
1496 # Various disabled crypt passwords
1497 } elsif ( $pass eq '*' || $pass eq '!' || $pass eq '!!' ) {
1498 $self->_password_encoding('crypt');
1504 $self->_password($pass);
1510 if $passwordmin && length($pass) < $passwordmin
1511 or $passwordmax && length($pass) > $passwordmax;
1513 if ( $encoding eq 'crypt' ) {
1514 if ($encryption eq 'md5') {
1515 $pass = unix_md5_crypt($pass);
1516 } elsif ($encryption eq 'des') {
1517 $pass = crypt($pass, $saltset[int(rand(64))].$saltset[int(rand(64))]);
1520 } elsif ( $encoding eq 'ldap' ) {
1521 if ($encryption eq 'md5') {
1522 $pass = md5_base64($pass);
1523 } elsif ($encryption eq 'sha1') {
1524 $pass = sha1_base64($pass);
1525 } elsif ($encryption eq 'crypt') {
1526 $pass = crypt($pass, $saltset[int(rand(64))].$saltset[int(rand(64))]);
1528 # else $encryption eq 'plain', do nothing
1529 $pass .= '=' x (4 - length($pass) % 4) #properly padded base64
1530 if $encryption eq 'md5' || $encryption eq 'sha1';
1531 $pass = '{'.uc($encryption).'}'.$pass;
1533 # else encoding eq 'plain'
1535 $self->_password($pass);
1541 Internal function to check the username against the list of system usernames
1542 from the I<system_usernames> configuration value. Returns true if the username
1543 is listed on the system username list.
1549 scalar( grep { $self->username eq $_ || $self->email eq $_ }
1550 $conf->config('system_usernames')
1554 =item _check_duplicate
1556 Internal method to check for duplicates usernames, username@domain pairs and
1559 If the I<global_unique-username> configuration value is set to B<username> or
1560 B<username@domain>, enforces global username or username@domain uniqueness.
1562 In all cases, check for duplicate uids and usernames or username@domain pairs
1563 per export and with identical I<svcpart> values.
1567 sub _check_duplicate {
1570 my $global_unique = $conf->config('global_unique-username') || 'none';
1571 return '' if $global_unique eq 'disabled';
1575 my $part_svc = qsearchs('part_svc', { 'svcpart' => $self->svcpart } );
1576 unless ( $part_svc ) {
1577 return 'unknown svcpart '. $self->svcpart;
1580 my @dup_user = grep { !$self->svcnum || $_->svcnum != $self->svcnum }
1581 qsearch( 'svc_acct', { 'username' => $self->username } );
1582 return gettext('username_in_use')
1583 if $global_unique eq 'username' && @dup_user;
1585 my @dup_userdomain = grep { !$self->svcnum || $_->svcnum != $self->svcnum }
1586 qsearch( 'svc_acct', { 'username' => $self->username,
1587 'domsvc' => $self->domsvc } );
1588 return gettext('username_in_use')
1589 if $global_unique eq 'username@domain' && @dup_userdomain;
1592 if ( $part_svc->part_svc_column('uid')->columnflag ne 'F'
1593 && $self->username !~ /^(toor|(hyla)?fax)$/ ) {
1594 @dup_uid = grep { !$self->svcnum || $_->svcnum != $self->svcnum }
1595 qsearch( 'svc_acct', { 'uid' => $self->uid } );
1600 if ( @dup_user || @dup_userdomain || @dup_uid ) {
1601 my $exports = FS::part_export::export_info('svc_acct');
1602 my %conflict_user_svcpart;
1603 my %conflict_userdomain_svcpart = ( $self->svcpart => 'SELF', );
1605 foreach my $part_export ( $part_svc->part_export ) {
1607 #this will catch to the same exact export
1608 my @svcparts = map { $_->svcpart } $part_export->export_svc;
1610 #this will catch to exports w/same exporthost+type ???
1611 #my @other_part_export = qsearch('part_export', {
1612 # 'machine' => $part_export->machine,
1613 # 'exporttype' => $part_export->exporttype,
1615 #foreach my $other_part_export ( @other_part_export ) {
1616 # push @svcparts, map { $_->svcpart }
1617 # qsearch('export_svc', { 'exportnum' => $part_export->exportnum });
1620 #my $nodomain = $exports->{$part_export->exporttype}{'nodomain'};
1621 #silly kludge to avoid uninitialized value errors
1622 my $nodomain = exists( $exports->{$part_export->exporttype}{'nodomain'} )
1623 ? $exports->{$part_export->exporttype}{'nodomain'}
1625 if ( $nodomain =~ /^Y/i ) {
1626 $conflict_user_svcpart{$_} = $part_export->exportnum
1629 $conflict_userdomain_svcpart{$_} = $part_export->exportnum
1634 foreach my $dup_user ( @dup_user ) {
1635 my $dup_svcpart = $dup_user->cust_svc->svcpart;
1636 if ( exists($conflict_user_svcpart{$dup_svcpart}) ) {
1637 return "duplicate username ". $self->username.
1638 ": conflicts with svcnum ". $dup_user->svcnum.
1639 " via exportnum ". $conflict_user_svcpart{$dup_svcpart};
1643 foreach my $dup_userdomain ( @dup_userdomain ) {
1644 my $dup_svcpart = $dup_userdomain->cust_svc->svcpart;
1645 if ( exists($conflict_userdomain_svcpart{$dup_svcpart}) ) {
1646 return "duplicate username\@domain ". $self->email.
1647 ": conflicts with svcnum ". $dup_userdomain->svcnum.
1648 " via exportnum ". $conflict_userdomain_svcpart{$dup_svcpart};
1652 foreach my $dup_uid ( @dup_uid ) {
1653 my $dup_svcpart = $dup_uid->cust_svc->svcpart;
1654 if ( exists($conflict_user_svcpart{$dup_svcpart})
1655 || exists($conflict_userdomain_svcpart{$dup_svcpart}) ) {
1656 return "duplicate uid ". $self->uid.
1657 ": conflicts with svcnum ". $dup_uid->svcnum.
1659 ( $conflict_user_svcpart{$dup_svcpart}
1660 || $conflict_userdomain_svcpart{$dup_svcpart} );
1672 Depriciated, use radius_reply instead.
1677 carp "FS::svc_acct::radius depriciated, use radius_reply";
1678 $_[0]->radius_reply;
1683 Returns key/value pairs, suitable for assigning to a hash, for any RADIUS
1684 reply attributes of this record.
1686 Note that this is now the preferred method for reading RADIUS attributes -
1687 accessing the columns directly is discouraged, as the column names are
1688 expected to change in the future.
1695 return %{ $self->{'radius_reply'} }
1696 if exists $self->{'radius_reply'};
1701 my($column, $attrib) = ($1, $2);
1702 #$attrib =~ s/_/\-/g;
1703 ( $FS::raddb::attrib{lc($attrib)}, $self->getfield($column) );
1704 } grep { /^radius_/ && $self->getfield($_) } fields( $self->table );
1706 if ( $self->slipip && $self->slipip ne '0e0' ) {
1707 $reply{$radius_ip} = $self->slipip;
1710 if ( $self->seconds !~ /^$/ ) {
1711 $reply{'Session-Timeout'} = $self->seconds;
1714 if ( $conf->exists('radius-chillispot-max') ) {
1715 #http://dev.coova.org/svn/coova-chilli/doc/dictionary.chillispot
1717 #hmm. just because sqlradius.pm says so?
1724 foreach my $what (qw( input output total )) {
1725 my $is = $whatis{$what}.'bytes';
1726 if ( $self->$is() =~ /\d/ ) {
1727 my $big = new Math::BigInt $self->$is();
1728 $big = new Math::BigInt '0' if $big->is_neg();
1729 my $att = "Chillispot-Max-\u$what";
1730 $reply{"$att-Octets"} = $big->copy->band(0xffffffff)->bstr;
1731 $reply{"$att-Gigawords"} = $big->copy->brsft(32)->bstr;
1742 Returns key/value pairs, suitable for assigning to a hash, for any RADIUS
1743 check attributes of this record.
1745 Note that this is now the preferred method for reading RADIUS attributes -
1746 accessing the columns directly is discouraged, as the column names are
1747 expected to change in the future.
1754 return %{ $self->{'radius_check'} }
1755 if exists $self->{'radius_check'};
1760 my($column, $attrib) = ($1, $2);
1761 #$attrib =~ s/_/\-/g;
1762 ( $FS::raddb::attrib{lc($attrib)}, $self->getfield($column) );
1763 } grep { /^rc_/ && $self->getfield($_) } fields( $self->table );
1766 my($pw_attrib, $password) = $self->radius_password;
1767 $check{$pw_attrib} = $password;
1769 my $cust_svc = $self->cust_svc;
1771 my $cust_pkg = $cust_svc->cust_pkg;
1772 if ( $cust_pkg && $cust_pkg->part_pkg->is_prepaid && $cust_pkg->bill ) {
1773 $check{'Expiration'} = time2str('%B %e %Y %T', $cust_pkg->bill ); #http://lists.cistron.nl/pipermail/freeradius-users/2005-January/040184.html
1776 warn "WARNING: no cust_svc record for svc_acct.svcnum ". $self->svcnum.
1777 "; can't set Expiration\n"
1785 =item radius_password
1787 Returns a key/value pair containing the RADIUS attribute name and value
1792 sub radius_password {
1796 if ( $self->_password_encoding eq 'ldap' ) {
1797 $pw_attrib = 'Password-With-Header';
1798 } elsif ( $self->_password_encoding eq 'crypt' ) {
1799 $pw_attrib = 'Crypt-Password';
1800 } elsif ( $self->_password_encoding eq 'plain' ) {
1801 $pw_attrib = $radius_password;
1803 $pw_attrib = length($self->_password) <= 12
1808 ($pw_attrib, $self->_password);
1814 This method instructs the object to "snapshot" or freeze RADIUS check and
1815 reply attributes to the current values.
1819 #bah, my english is too broken this morning
1820 #Of note is the "Expiration" attribute, which, for accounts in prepaid packages, is typically defined on-the-fly as the associated packages cust_pkg.bill. (This is used by
1821 #the FS::cust_pkg's replace method to trigger the correct export updates when
1822 #package dates change)
1827 $self->{$_} = { $self->$_() }
1828 foreach qw( radius_reply radius_check );
1832 =item forget_snapshot
1834 This methos instructs the object to forget any previously snapshotted
1835 RADIUS check and reply attributes.
1839 sub forget_snapshot {
1843 foreach qw( radius_reply radius_check );
1847 =item domain [ END_TIMESTAMP [ START_TIMESTAMP ] ]
1849 Returns the domain associated with this account.
1851 END_TIMESTAMP and START_TIMESTAMP can optionally be passed when dealing with
1858 die "svc_acct.domsvc is null for svcnum ". $self->svcnum unless $self->domsvc;
1859 my $svc_domain = $self->svc_domain(@_)
1860 or die "no svc_domain.svcnum for svc_acct.domsvc ". $self->domsvc;
1861 $svc_domain->domain;
1866 Returns the FS::cust_svc record for this account (see L<FS::cust_svc>).
1870 #inherited from svc_Common
1872 =item email [ END_TIMESTAMP [ START_TIMESTAMP ] ]
1874 Returns an email address associated with the account.
1876 END_TIMESTAMP and START_TIMESTAMP can optionally be passed when dealing with
1883 $self->username. '@'. $self->domain(@_);
1889 Returns an array of FS::acct_snarf records associated with the account.
1893 # unused as originally intended, but now by Communigate Pro "RPOP"
1897 'table' => 'acct_snarf',
1898 'hashref' => { 'svcnum' => $self->svcnum },
1899 #'order_by' => 'ORDER BY priority ASC',
1903 =item cgp_rpop_hashref
1905 Returns an arrayref of RPOP data suitable for Communigate Pro API commands.
1909 sub cgp_rpop_hashref {
1911 { map { $_->snarfname => $_->cgp_hashref } $self->acct_snarf };
1914 =item decrement_upbytes OCTETS
1916 Decrements the I<upbytes> field of this record by the given amount. If there
1917 is an error, returns the error, otherwise returns false.
1921 sub decrement_upbytes {
1922 shift->_op_usage('-', 'upbytes', @_);
1925 =item increment_upbytes OCTETS
1927 Increments the I<upbytes> field of this record by the given amount. If there
1928 is an error, returns the error, otherwise returns false.
1932 sub increment_upbytes {
1933 shift->_op_usage('+', 'upbytes', @_);
1936 =item decrement_downbytes OCTETS
1938 Decrements the I<downbytes> field of this record by the given amount. If there
1939 is an error, returns the error, otherwise returns false.
1943 sub decrement_downbytes {
1944 shift->_op_usage('-', 'downbytes', @_);
1947 =item increment_downbytes OCTETS
1949 Increments the I<downbytes> field of this record by the given amount. If there
1950 is an error, returns the error, otherwise returns false.
1954 sub increment_downbytes {
1955 shift->_op_usage('+', 'downbytes', @_);
1958 =item decrement_totalbytes OCTETS
1960 Decrements the I<totalbytes> field of this record by the given amount. If there
1961 is an error, returns the error, otherwise returns false.
1965 sub decrement_totalbytes {
1966 shift->_op_usage('-', 'totalbytes', @_);
1969 =item increment_totalbytes OCTETS
1971 Increments the I<totalbytes> field of this record by the given amount. If there
1972 is an error, returns the error, otherwise returns false.
1976 sub increment_totalbytes {
1977 shift->_op_usage('+', 'totalbytes', @_);
1980 =item decrement_seconds SECONDS
1982 Decrements the I<seconds> field of this record by the given amount. If there
1983 is an error, returns the error, otherwise returns false.
1987 sub decrement_seconds {
1988 shift->_op_usage('-', 'seconds', @_);
1991 =item increment_seconds SECONDS
1993 Increments the I<seconds> field of this record by the given amount. If there
1994 is an error, returns the error, otherwise returns false.
1998 sub increment_seconds {
1999 shift->_op_usage('+', 'seconds', @_);
2007 my %op2condition = (
2008 '-' => sub { my($self, $column, $amount) = @_;
2009 $self->$column - $amount <= 0;
2011 '+' => sub { my($self, $column, $amount) = @_;
2012 ($self->$column || 0) + $amount > 0;
2015 my %op2warncondition = (
2016 '-' => sub { my($self, $column, $amount) = @_;
2017 my $threshold = $column . '_threshold';
2018 $self->$column - $amount <= $self->$threshold + 0;
2020 '+' => sub { my($self, $column, $amount) = @_;
2021 ($self->$column || 0) + $amount > 0;
2026 my( $self, $op, $column, $amount ) = @_;
2028 warn "$me _op_usage called for $column on svcnum ". $self->svcnum.
2029 ' ('. $self->email. "): $op $amount\n"
2032 return '' unless $amount;
2034 local $SIG{HUP} = 'IGNORE';
2035 local $SIG{INT} = 'IGNORE';
2036 local $SIG{QUIT} = 'IGNORE';
2037 local $SIG{TERM} = 'IGNORE';
2038 local $SIG{TSTP} = 'IGNORE';
2039 local $SIG{PIPE} = 'IGNORE';
2041 my $oldAutoCommit = $FS::UID::AutoCommit;
2042 local $FS::UID::AutoCommit = 0;
2045 my $sql = "UPDATE svc_acct SET $column = ".
2046 " CASE WHEN $column IS NULL THEN 0 ELSE $column END ". #$column||0
2047 " $op ? WHERE svcnum = ?";
2051 my $sth = $dbh->prepare( $sql )
2052 or die "Error preparing $sql: ". $dbh->errstr;
2053 my $rv = $sth->execute($amount, $self->svcnum);
2054 die "Error executing $sql: ". $sth->errstr
2055 unless defined($rv);
2056 die "Can't update $column for svcnum". $self->svcnum
2059 #$self->snapshot; #not necessary, we retain the old values
2060 #create an object with the updated usage values
2061 my $new = qsearchs('svc_acct', { 'svcnum' => $self->svcnum });
2063 my $error = $new->replace($self);
2065 $dbh->rollback if $oldAutoCommit;
2066 return "Error replacing: $error";
2069 #overlimit_action eq 'cancel' handling
2070 my $cust_pkg = $self->cust_svc->cust_pkg;
2072 && $cust_pkg->part_pkg->option('overlimit_action', 1) eq 'cancel'
2073 && $op eq '-' && &{$op2condition{$op}}($self, $column, $amount)
2077 my $error = $cust_pkg->cancel; #XXX should have a reason
2079 $dbh->rollback if $oldAutoCommit;
2080 return "Error cancelling: $error";
2083 #nothing else is relevant if we're cancelling, so commit & return success
2084 warn "$me update successful; committing\n"
2086 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
2091 my $action = $op2action{$op};
2093 if ( &{$op2condition{$op}}($self, $column, $amount) &&
2094 ( $action eq 'suspend' && !$self->overlimit
2095 || $action eq 'unsuspend' && $self->overlimit )
2098 my $error = $self->_op_overlimit($action);
2100 $dbh->rollback if $oldAutoCommit;
2106 if ( $conf->exists("svc_acct-usage_$action")
2107 && &{$op2condition{$op}}($self, $column, $amount) ) {
2108 #my $error = $self->$action();
2109 my $error = $self->cust_svc->cust_pkg->$action();
2110 # $error ||= $self->overlimit($action);
2112 $dbh->rollback if $oldAutoCommit;
2113 return "Error ${action}ing: $error";
2117 if ($warning_template && &{$op2warncondition{$op}}($self, $column, $amount)) {
2118 my $wqueue = new FS::queue {
2119 'svcnum' => $self->svcnum,
2120 'job' => 'FS::svc_acct::reached_threshold',
2125 $to = $warning_cc if &{$op2condition{$op}}($self, $column, $amount);
2129 my $error = $wqueue->insert(
2130 'svcnum' => $self->svcnum,
2132 'column' => $column,
2136 $dbh->rollback if $oldAutoCommit;
2137 return "Error queuing threshold activity: $error";
2141 warn "$me update successful; committing\n"
2143 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
2149 my( $self, $action ) = @_;
2151 local $SIG{HUP} = 'IGNORE';
2152 local $SIG{INT} = 'IGNORE';
2153 local $SIG{QUIT} = 'IGNORE';
2154 local $SIG{TERM} = 'IGNORE';
2155 local $SIG{TSTP} = 'IGNORE';
2156 local $SIG{PIPE} = 'IGNORE';
2158 my $oldAutoCommit = $FS::UID::AutoCommit;
2159 local $FS::UID::AutoCommit = 0;
2162 my $cust_pkg = $self->cust_svc->cust_pkg;
2164 my @conf_overlimit =
2166 ? $conf->config('overlimit_groups', $cust_pkg->cust_main->agentnum )
2167 : $conf->config('overlimit_groups');
2169 foreach my $part_export ( $self->cust_svc->part_svc->part_export ) {
2171 my @groups = scalar(@conf_overlimit) ? @conf_overlimit
2172 : split(' ',$part_export->option('overlimit_groups'));
2173 next unless scalar(@groups);
2175 my $other = new FS::svc_acct $self->hashref;
2176 $other->usergroup(\@groups);
2179 if ($action eq 'suspend') {
2182 } else { # $action eq 'unsuspend'
2187 my $error = $part_export->export_replace($new, $old)
2188 || $self->overlimit($action);
2191 $dbh->rollback if $oldAutoCommit;
2192 return "Error replacing radius groups: $error";
2197 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
2203 my( $self, $valueref, %options ) = @_;
2205 warn "$me set_usage called for svcnum ". $self->svcnum.
2206 ' ('. $self->email. "): ".
2207 join(', ', map { "$_ => " . $valueref->{$_}} keys %$valueref) . "\n"
2210 local $SIG{HUP} = 'IGNORE';
2211 local $SIG{INT} = 'IGNORE';
2212 local $SIG{QUIT} = 'IGNORE';
2213 local $SIG{TERM} = 'IGNORE';
2214 local $SIG{TSTP} = 'IGNORE';
2215 local $SIG{PIPE} = 'IGNORE';
2217 local $FS::svc_Common::noexport_hack = 1;
2218 my $oldAutoCommit = $FS::UID::AutoCommit;
2219 local $FS::UID::AutoCommit = 0;
2224 if ( $options{null} ) {
2225 %handyhash = ( map { ( $_ => undef, $_."_threshold" => undef ) }
2226 qw( seconds upbytes downbytes totalbytes )
2229 foreach my $field (keys %$valueref){
2230 $reset = 1 if $valueref->{$field};
2231 $self->setfield($field, $valueref->{$field});
2232 $self->setfield( $field.'_threshold',
2233 int($self->getfield($field)
2234 * ( $conf->exists('svc_acct-usage_threshold')
2235 ? 1 - $conf->config('svc_acct-usage_threshold')/100
2240 $handyhash{$field} = $self->getfield($field);
2241 $handyhash{$field.'_threshold'} = $self->getfield($field.'_threshold');
2243 #my $error = $self->replace; #NO! we avoid the call to ->check for
2244 #die $error if $error; #services not explicity changed via the UI
2246 my $sql = "UPDATE svc_acct SET " .
2247 join (',', map { "$_ = ?" } (keys %handyhash) ).
2248 " WHERE svcnum = ". $self->svcnum;
2253 if (scalar(keys %handyhash)) {
2254 my $sth = $dbh->prepare( $sql )
2255 or die "Error preparing $sql: ". $dbh->errstr;
2256 my $rv = $sth->execute(values %handyhash);
2257 die "Error executing $sql: ". $sth->errstr
2258 unless defined($rv);
2259 die "Can't update usage for svcnum ". $self->svcnum
2263 #$self->snapshot; #not necessary, we retain the old values
2264 #create an object with the updated usage values
2265 my $new = qsearchs('svc_acct', { 'svcnum' => $self->svcnum });
2266 local($FS::Record::nowarn_identical) = 1;
2267 my $error = $new->replace($self); #call exports
2269 $dbh->rollback if $oldAutoCommit;
2270 return "Error replacing: $error";
2277 $error = $self->_op_overlimit('unsuspend')
2278 if $self->overlimit;;
2280 $error ||= $self->cust_svc->cust_pkg->unsuspend
2281 if $conf->exists("svc_acct-usage_unsuspend");
2284 $dbh->rollback if $oldAutoCommit;
2285 return "Error unsuspending: $error";
2290 warn "$me update successful; committing\n"
2292 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
2298 =item recharge HASHREF
2300 Increments usage columns by the amount specified in HASHREF as
2301 column=>amount pairs.
2306 my ($self, $vhash) = @_;
2309 warn "[$me] recharge called on $self: ". Dumper($self).
2310 "\nwith vhash: ". Dumper($vhash);
2313 my $oldAutoCommit = $FS::UID::AutoCommit;
2314 local $FS::UID::AutoCommit = 0;
2318 foreach my $column (keys %$vhash){
2319 $error ||= $self->_op_usage('+', $column, $vhash->{$column});
2323 $dbh->rollback if $oldAutoCommit;
2325 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
2330 =item is_rechargeable
2332 Returns true if this svc_account can be "recharged" and false otherwise.
2336 sub is_rechargable {
2338 $self->seconds ne ''
2339 || $self->upbytes ne ''
2340 || $self->downbytes ne ''
2341 || $self->totalbytes ne '';
2344 =item seconds_since TIMESTAMP
2346 Returns the number of seconds this account has been online since TIMESTAMP,
2347 according to the session monitor (see L<FS::Session>).
2349 TIMESTAMP is specified as a UNIX timestamp; see L<perlfunc/"time">. Also see
2350 L<Time::Local> and L<Date::Parse> for conversion functions.
2354 #note: POD here, implementation in FS::cust_svc
2357 $self->cust_svc->seconds_since(@_);
2360 =item seconds_since_sqlradacct TIMESTAMP_START TIMESTAMP_END
2362 Returns the numbers of seconds this account has been online between
2363 TIMESTAMP_START (inclusive) and TIMESTAMP_END (exclusive), according to an
2364 external SQL radacct table, specified via sqlradius export. Sessions which
2365 started in the specified range but are still open are counted from session
2366 start to the end of the range (unless they are over 1 day old, in which case
2367 they are presumed missing their stop record and not counted). Also, sessions
2368 which end in the range but started earlier are counted from the start of the
2369 range to session end. Finally, sessions which start before the range but end
2370 after are counted for the entire range.
2372 TIMESTAMP_START and TIMESTAMP_END are specified as UNIX timestamps; see
2373 L<perlfunc/"time">. Also see L<Time::Local> and L<Date::Parse> for conversion
2378 #note: POD here, implementation in FS::cust_svc
2379 sub seconds_since_sqlradacct {
2381 $self->cust_svc->seconds_since_sqlradacct(@_);
2384 =item attribute_since_sqlradacct TIMESTAMP_START TIMESTAMP_END ATTRIBUTE
2386 Returns the sum of the given attribute for all accounts (see L<FS::svc_acct>)
2387 in this package for sessions ending between TIMESTAMP_START (inclusive) and
2388 TIMESTAMP_END (exclusive).
2390 TIMESTAMP_START and TIMESTAMP_END are specified as UNIX timestamps; see
2391 L<perlfunc/"time">. Also see L<Time::Local> and L<Date::Parse> for conversion
2396 #note: POD here, implementation in FS::cust_svc
2397 sub attribute_since_sqlradacct {
2399 $self->cust_svc->attribute_since_sqlradacct(@_);
2402 =item get_session_history TIMESTAMP_START TIMESTAMP_END
2404 Returns an array of hash references of this customers login history for the
2405 given time range. (document this better)
2409 sub get_session_history {
2411 $self->cust_svc->get_session_history(@_);
2414 =item last_login_text
2416 Returns text describing the time of last login.
2420 sub last_login_text {
2422 $self->last_login ? ctime($self->last_login) : 'unknown';
2425 =item get_cdrs TIMESTAMP_START TIMESTAMP_END [ 'OPTION' => 'VALUE ... ]
2430 my($self, $start, $end, %opt ) = @_;
2432 my $did = $self->username; #yup
2434 my $prefix = $opt{'default_prefix'}; #convergent.au '+61'
2436 my $for_update = $opt{'for_update'} ? 'FOR UPDATE' : '';
2438 #SELECT $for_update * FROM cdr
2439 # WHERE calldate >= $start #need a conversion
2440 # AND calldate < $end #ditto
2441 # AND ( charged_party = "$did"
2442 # OR charged_party = "$prefix$did" #if length($prefix);
2443 # OR ( ( charged_party IS NULL OR charged_party = '' )
2445 # ( src = "$did" OR src = "$prefix$did" ) # if length($prefix)
2448 # AND ( freesidestatus IS NULL OR freesidestatus = '' )
2451 if ( length($prefix) ) {
2453 " AND ( charged_party = '$did'
2454 OR charged_party = '$prefix$did'
2455 OR ( ( charged_party IS NULL OR charged_party = '' )
2457 ( src = '$did' OR src = '$prefix$did' )
2463 " AND ( charged_party = '$did'
2464 OR ( ( charged_party IS NULL OR charged_party = '' )
2474 'select' => "$for_update *",
2477 #( freesidestatus IS NULL OR freesidestatus = '' )
2478 'freesidestatus' => '',
2480 'extra_sql' => $charged_or_src,
2486 # sub radius_groups has moved to svc_Radius_Mixin
2488 =item clone_suspended
2490 Constructor used by FS::part_export::_export_suspend fallback. Document
2495 sub clone_suspended {
2497 my %hash = $self->hash;
2498 $hash{_password} = join('',map($pw_set[ int(rand $#pw_set) ], (0..7) ) );
2499 new FS::svc_acct \%hash;
2502 =item clone_kludge_unsuspend
2504 Constructor used by FS::part_export::_export_unsuspend fallback. Document
2509 sub clone_kludge_unsuspend {
2511 my %hash = $self->hash;
2512 $hash{_password} = '';
2513 new FS::svc_acct \%hash;
2516 =item check_password
2518 Checks the supplied password against the (possibly encrypted) password in the
2519 database. Returns true for a successful authentication, false for no match.
2521 Currently supported encryptions are: classic DES crypt() and MD5
2525 sub check_password {
2526 my($self, $check_password) = @_;
2528 #remove old-style SUSPENDED kludge, they should be allowed to login to
2529 #self-service and pay up
2530 ( my $password = $self->_password ) =~ s/^\*SUSPENDED\* //;
2532 if ( $self->_password_encoding eq 'ldap' ) {
2534 $password =~ s/^{PLAIN}/{CLEARTEXT}/;
2535 my $auth = from_rfc2307 Authen::Passphrase $password;
2536 return $auth->match($check_password);
2538 } elsif ( $self->_password_encoding eq 'crypt' ) {
2540 my $auth = from_crypt Authen::Passphrase $self->_password;
2541 return $auth->match($check_password);
2543 } elsif ( $self->_password_encoding eq 'plain' ) {
2545 return $check_password eq $password;
2549 #XXX this could be replaced with Authen::Passphrase stuff
2551 if ( $password =~ /^(\*|!!?)$/ ) { #no self-service login
2553 } elsif ( length($password) < 13 ) { #plaintext
2554 $check_password eq $password;
2555 } elsif ( length($password) == 13 ) { #traditional DES crypt
2556 crypt($check_password, $password) eq $password;
2557 } elsif ( $password =~ /^\$1\$/ ) { #MD5 crypt
2558 unix_md5_crypt($check_password, $password) eq $password;
2559 } elsif ( $password =~ /^\$2a?\$/ ) { #Blowfish
2560 warn "Can't check password: Blowfish encryption not yet supported, ".
2561 "svcnum ". $self->svcnum. "\n";
2564 warn "Can't check password: Unrecognized encryption for svcnum ".
2565 $self->svcnum. "\n";
2573 =item crypt_password [ DEFAULT_ENCRYPTION_TYPE ]
2575 Returns an encrypted password, either by passing through an encrypted password
2576 in the database or by encrypting a plaintext password from the database.
2578 The optional DEFAULT_ENCRYPTION_TYPE parameter can be set to I<crypt> (classic
2579 UNIX DES crypt), I<md5> (md5 crypt supported by most modern Linux and BSD
2580 distrubtions), or (eventually) I<blowfish> (blowfish hashing supported by
2581 OpenBSD, SuSE, other Linux distibutions with pam_unix2, etc.). The default
2582 encryption type is only used if the password is not already encrypted in the
2587 sub crypt_password {
2590 if ( $self->_password_encoding eq 'ldap' ) {
2592 if ( $self->_password =~ /^\{(PLAIN|CLEARTEXT)\}(.+)$/ ) {
2595 #XXX this could be replaced with Authen::Passphrase stuff
2597 my $encryption = ( scalar(@_) && $_[0] ) ? shift : 'crypt';
2598 if ( $encryption eq 'crypt' ) {
2601 $saltset[int(rand(64))].$saltset[int(rand(64))]
2603 } elsif ( $encryption eq 'md5' ) {
2604 return unix_md5_crypt( $self->_password );
2605 } elsif ( $encryption eq 'blowfish' ) {
2606 croak "unknown encryption method $encryption";
2608 croak "unknown encryption method $encryption";
2611 } elsif ( $self->_password =~ /^\{CRYPT\}(.+)$/ ) {
2615 } elsif ( $self->_password_encoding eq 'crypt' ) {
2617 return $self->_password;
2619 } elsif ( $self->_password_encoding eq 'plain' ) {
2621 #XXX this could be replaced with Authen::Passphrase stuff
2623 my $encryption = ( scalar(@_) && $_[0] ) ? shift : 'crypt';
2624 if ( $encryption eq 'crypt' ) {
2627 $saltset[int(rand(64))].$saltset[int(rand(64))]
2629 } elsif ( $encryption eq 'md5' ) {
2630 return unix_md5_crypt( $self->_password );
2631 } elsif ( $encryption eq 'sha1_base64' ) { #for acct_sql
2632 my $pass = sha1_base64( $self->_password );
2633 $pass .= '=' x (4 - length($pass) % 4); #properly padded base64
2635 } elsif ( $encryption eq 'blowfish' ) {
2636 croak "unknown encryption method $encryption";
2638 croak "unknown encryption method $encryption";
2643 if ( length($self->_password) == 13
2644 || $self->_password =~ /^\$(1|2a?)\$/
2645 || $self->_password =~ /^(\*|NP|\*LK\*|!!?)$/
2651 #XXX this could be replaced with Authen::Passphrase stuff
2653 my $encryption = ( scalar(@_) && $_[0] ) ? shift : 'crypt';
2654 if ( $encryption eq 'crypt' ) {
2657 $saltset[int(rand(64))].$saltset[int(rand(64))]
2659 } elsif ( $encryption eq 'md5' ) {
2660 return unix_md5_crypt( $self->_password );
2661 } elsif ( $encryption eq 'blowfish' ) {
2662 croak "unknown encryption method $encryption";
2664 croak "unknown encryption method $encryption";
2673 =item ldap_password [ DEFAULT_ENCRYPTION_TYPE ]
2675 Returns an encrypted password in "LDAP" format, with a curly-bracked prefix
2676 describing the format, for example, "{PLAIN}himom", "{CRYPT}94pAVyK/4oIBk" or
2677 "{MD5}5426824942db4253f87a1009fd5d2d4".
2679 The optional DEFAULT_ENCRYPTION_TYPE is not yet used, but the idea is for it
2680 to work the same as the B</crypt_password> method.
2686 #eventually should check a "password-encoding" field
2688 if ( $self->_password_encoding eq 'ldap' ) {
2690 return $self->_password;
2692 } elsif ( $self->_password_encoding eq 'crypt' ) {
2694 if ( length($self->_password) == 13 ) { #crypt
2695 return '{CRYPT}'. $self->_password;
2696 } elsif ( $self->_password =~ /^\$1\$(.*)$/ && length($1) == 31 ) { #passwdMD5
2698 #} elsif ( $self->_password =~ /^\$2a?\$(.*)$/ ) { #Blowfish
2699 # die "Blowfish encryption not supported in this context, svcnum ".
2700 # $self->svcnum. "\n";
2702 warn "encryption method not (yet?) supported in LDAP context";
2703 return '{CRYPT}*'; #unsupported, should not auth
2706 } elsif ( $self->_password_encoding eq 'plain' ) {
2708 return '{PLAIN}'. $self->_password;
2710 #return '{CLEARTEXT}'. $self->_password; #?
2714 if ( length($self->_password) == 13 ) { #crypt
2715 return '{CRYPT}'. $self->_password;
2716 } elsif ( $self->_password =~ /^\$1\$(.*)$/ && length($1) == 31 ) { #passwdMD5
2718 } elsif ( $self->_password =~ /^\$2a?\$(.*)$/ ) { #Blowfish
2719 warn "Blowfish encryption not supported in this context, svcnum ".
2720 $self->svcnum. "\n";
2723 #are these two necessary anymore?
2724 } elsif ( $self->_password =~ /^(\w{48})$/ ) { #LDAP SSHA
2725 return '{SSHA}'. $1;
2726 } elsif ( $self->_password =~ /^(\w{64})$/ ) { #LDAP NS-MTA-MD5
2727 return '{NS-MTA-MD5}'. $1;
2730 return '{PLAIN}'. $self->_password;
2732 #return '{CLEARTEXT}'. $self->_password; #?
2734 #XXX this could be replaced with Authen::Passphrase stuff if it gets used
2735 #my $encryption = ( scalar(@_) && $_[0] ) ? shift : 'crypt';
2736 #if ( $encryption eq 'crypt' ) {
2737 # return '{CRYPT}'. crypt(
2739 # $saltset[int(rand(64))].$saltset[int(rand(64))]
2741 #} elsif ( $encryption eq 'md5' ) {
2742 # unix_md5_crypt( $self->_password );
2743 #} elsif ( $encryption eq 'blowfish' ) {
2744 # croak "unknown encryption method $encryption";
2746 # croak "unknown encryption method $encryption";
2754 =item domain_slash_username
2756 Returns $domain/$username/
2760 sub domain_slash_username {
2762 $self->domain. '/'. $self->username. '/';
2765 =item virtual_maildir
2767 Returns $domain/maildirs/$username/
2771 sub virtual_maildir {
2773 $self->domain. '/maildirs/'. $self->username. '/';
2778 =head1 CLASS METHODS
2782 =item search HASHREF
2784 Class method which returns a qsearch hash expression to search for parameters
2785 specified in HASHREF. Valid parameters are
2799 Arrayref of pkgparts
2805 Arrayref of additional WHERE clauses, will be ANDed together.
2816 my ($class, $params) = @_;
2819 ' LEFT JOIN cust_svc USING ( svcnum ) ',
2820 ' LEFT JOIN part_svc USING ( svcpart ) ',
2821 ' LEFT JOIN cust_pkg USING ( pkgnum ) ',
2822 ' LEFT JOIN cust_main USING ( custnum ) ',
2828 if ( $params->{'domain'} ) {
2829 my $svc_domain = qsearchs('svc_domain', { 'domain'=>$params->{'domain'} } );
2830 #preserve previous behavior & bubble up an error if $svc_domain not found?
2831 push @where, 'domsvc = '. $svc_domain->svcnum if $svc_domain;
2835 if ( $params->{'domsvc'} =~ /^(\d+)$/ ) {
2836 push @where, "domsvc = $1";
2840 push @where, 'pkgnum IS NULL' if $params->{'unlinked'};
2843 if ( $params->{'agentnum'} =~ /^(\d+)$/ and $1 ) {
2844 push @where, "cust_main.agentnum = $1";
2848 if ( $params->{'custnum'} =~ /^(\d+)$/ and $1 ) {
2849 push @where, "custnum = $1";
2853 if ( $params->{'cust_status'} =~ /^([a-z]+)$/ ) {
2854 push @where, FS::cust_main->cust_status_sql . " = '$1'";
2858 if ( $params->{'balance'} =~ /^\s*(\-?\d*(\.\d{1,2})?)\s*$/ && length($1) ) {
2862 if ( $params->{'balance_days'} =~ /^\s*(\d*(\.\d{1,3})?)\s*$/ && length($1) ) {
2863 $age = time - 86400 * $1;
2865 push @where, FS::cust_main->balance_date_sql($age) . " > $balance";
2869 if ( $params->{'payby'} && scalar(@{ $params->{'payby'} }) ) {
2870 my @payby = map "'$_'", grep /^(\w+)$/, @{ $params->{'payby'} };
2871 push @where, 'payby IN ('. join(',', @payby ). ')';
2875 if ( $params->{'pkgpart'} && scalar(@{ $params->{'pkgpart'} }) ) {
2876 #XXX untaint or sql quote
2878 'cust_pkg.pkgpart IN ('. join(',', @{ $params->{'pkgpart'} } ). ')';
2882 if ( $params->{'popnum'} =~ /^(\d+)$/ ) {
2883 push @where, "popnum = $1";
2887 if ( $params->{'svcpart'} ) {
2888 my @svcpart = ref( $params->{'svcpart'} )
2889 ? @{ $params->{'svcpart'} }
2890 : $params->{'svcpart'}
2891 ? ( $params->{'svcpart'} )
2893 @svcpart = grep /^(\d+)$/, @svcpart;
2894 push @where, 'svcpart IN ('. join(',', @svcpart ). ')' if @svcpart;
2897 if ( $params->{'exportnum'} =~ /^(\d+)$/ ) {
2898 push @from, ' LEFT JOIN export_svc USING ( svcpart )';
2899 push @where, "exportnum = $1";
2903 my @where_sector = $class->tower_sector_sql($params);
2904 if ( @where_sector ) {
2905 push @where, @where_sector;
2906 push @from, ' LEFT JOIN tower_sector USING ( sectornum )';
2909 # here is the agent virtualization
2910 #if ($params->{CurrentUser}) {
2912 # qsearchs('access_user', { username => $params->{CurrentUser} });
2914 # if ($access_user) {
2915 # push @where, $access_user->agentnums_sql('table'=>'cust_main');
2917 # push @where, "1=0";
2920 push @where, $FS::CurrentUser::CurrentUser->agentnums_sql(
2921 'table' => 'cust_main',
2922 'null_right' => 'View/link unlinked services',
2926 push @where, @{ $params->{'where'} } if $params->{'where'};
2928 my $addl_from = join(' ', @from);
2929 my $extra_sql = scalar(@where) ? ' WHERE '. join(' AND ', @where) : '';
2931 my $count_query = "SELECT COUNT(*) FROM svc_acct $addl_from $extra_sql";
2932 #if ( keys %svc_acct ) {
2933 # $count_query .= ' WHERE '.
2934 # join(' AND ', map "$_ = ". dbh->quote($svc_acct{$_}),
2940 'table' => 'svc_acct',
2941 'hashref' => {}, # \%svc_acct,
2942 'select' => join(', ',
2945 'cust_main.custnum',
2946 FS::UI::Web::cust_sql_fields($params->{'cust_fields'}),
2948 'addl_from' => $addl_from,
2949 'extra_sql' => $extra_sql,
2950 'order_by' => $params->{'order_by'},
2951 'count_query' => $count_query,
2964 This is the FS::svc_acct job-queue-able version. It still uses
2965 FS::Misc::send_email under-the-hood.
2972 eval "use FS::Misc qw(send_email)";
2975 $opt{mimetype} ||= 'text/plain';
2976 $opt{mimetype} .= '; charset="iso-8859-1"' unless $opt{mimetype} =~ /charset/;
2978 my $error = send_email(
2979 'from' => $opt{from},
2981 'subject' => $opt{subject},
2982 'content-type' => $opt{mimetype},
2983 'body' => [ map "$_\n", split("\n", $opt{body}) ],
2985 die $error if $error;
2988 =item check_and_rebuild_fuzzyfiles
2992 sub check_and_rebuild_fuzzyfiles {
2993 my $dir = $FS::UID::conf_dir. "/cache.". $FS::UID::datasrc;
2994 -e "$dir/svc_acct.username"
2995 or &rebuild_fuzzyfiles;
2998 =item rebuild_fuzzyfiles
3002 sub rebuild_fuzzyfiles {
3004 use Fcntl qw(:flock);
3006 my $dir = $FS::UID::conf_dir. "/cache.". $FS::UID::datasrc;
3010 open(USERNAMELOCK,">>$dir/svc_acct.username")
3011 or die "can't open $dir/svc_acct.username: $!";
3012 flock(USERNAMELOCK,LOCK_EX)
3013 or die "can't lock $dir/svc_acct.username: $!";
3015 my @all_username = map $_->getfield('username'), qsearch('svc_acct', {});
3017 open (USERNAMECACHE,">$dir/svc_acct.username.tmp")
3018 or die "can't open $dir/svc_acct.username.tmp: $!";
3019 print USERNAMECACHE join("\n", @all_username), "\n";
3020 close USERNAMECACHE or die "can't close $dir/svc_acct.username.tmp: $!";
3022 rename "$dir/svc_acct.username.tmp", "$dir/svc_acct.username";
3032 my $dir = $FS::UID::conf_dir. "/cache.". $FS::UID::datasrc;
3033 open(USERNAMECACHE,"<$dir/svc_acct.username")
3034 or die "can't open $dir/svc_acct.username: $!";
3035 my @array = map { chomp; $_; } <USERNAMECACHE>;
3036 close USERNAMECACHE;
3040 =item append_fuzzyfiles USERNAME
3044 sub append_fuzzyfiles {
3045 my $username = shift;
3047 &check_and_rebuild_fuzzyfiles;
3049 use Fcntl qw(:flock);
3051 my $dir = $FS::UID::conf_dir. "/cache.". $FS::UID::datasrc;
3053 open(USERNAME,">>$dir/svc_acct.username")
3054 or die "can't open $dir/svc_acct.username: $!";
3055 flock(USERNAME,LOCK_EX)
3056 or die "can't lock $dir/svc_acct.username: $!";
3058 print USERNAME "$username\n";
3060 flock(USERNAME,LOCK_UN)
3061 or die "can't unlock $dir/svc_acct.username: $!";
3068 =item reached_threshold
3070 Performs some activities when svc_acct thresholds (such as number of seconds
3071 remaining) are reached.
3075 sub reached_threshold {
3078 my $svc_acct = qsearchs('svc_acct', { 'svcnum' => $opt{'svcnum'} } );
3079 die "Cannot find svc_acct with svcnum " . $opt{'svcnum'} unless $svc_acct;
3081 if ( $opt{'op'} eq '+' ){
3082 $svc_acct->setfield( $opt{'column'}.'_threshold',
3083 int($svc_acct->getfield($opt{'column'})
3084 * ( $conf->exists('svc_acct-usage_threshold')
3085 ? $conf->config('svc_acct-usage_threshold')/100
3090 my $error = $svc_acct->replace;
3091 die $error if $error;
3092 }elsif ( $opt{'op'} eq '-' ){
3094 my $threshold = $svc_acct->getfield( $opt{'column'}.'_threshold' );
3095 return '' if ($threshold eq '' );
3097 $svc_acct->setfield( $opt{'column'}.'_threshold', 0 );
3098 my $error = $svc_acct->replace;
3099 die $error if $error; # email next time, i guess
3101 if ( $warning_template ) {
3102 eval "use FS::Misc qw(send_email)";
3105 my $cust_pkg = $svc_acct->cust_svc->cust_pkg;
3106 my $cust_main = $cust_pkg->cust_main;
3108 my $to = join(', ', grep { $_ !~ /^(POST|FAX)$/ }
3109 $cust_main->invoicing_list,
3110 ($opt{'to'} ? $opt{'to'} : ())
3113 my $mimetype = $warning_mimetype;
3114 $mimetype .= '; charset="iso-8859-1"' unless $opt{mimetype} =~ /charset/;
3116 my $body = $warning_template->fill_in( HASH => {
3117 'custnum' => $cust_main->custnum,
3118 'username' => $svc_acct->username,
3119 'password' => $svc_acct->_password,
3120 'first' => $cust_main->first,
3121 'last' => $cust_main->getfield('last'),
3122 'pkg' => $cust_pkg->part_pkg->pkg,
3123 'column' => $opt{'column'},
3124 'amount' => $opt{'column'} =~/bytes/
3125 ? FS::UI::bytecount::display_bytecount($svc_acct->getfield($opt{'column'}))
3126 : $svc_acct->getfield($opt{'column'}),
3127 'threshold' => $opt{'column'} =~/bytes/
3128 ? FS::UI::bytecount::display_bytecount($threshold)
3133 my $error = send_email(
3134 'from' => $warning_from,
3136 'subject' => $warning_subject,
3137 'content-type' => $mimetype,
3138 'body' => [ map "$_\n", split("\n", $body) ],
3140 die $error if $error;
3143 die "unknown op: " . $opt{'op'};
3151 The $recref stuff in sub check should be cleaned up.
3153 The suspend, unsuspend and cancel methods update the database, but not the
3154 current object. This is probably a bug as it's unexpected and
3157 insertion of RADIUS group stuff in insert could be done with child_objects now
3158 (would probably clean up export of them too)
3160 _op_usage and set_usage bypass the history... maybe they shouldn't
3164 L<FS::svc_Common>, edit/part_svc.cgi from an installed web interface,
3165 export.html from the base documentation, L<FS::Record>, L<FS::Conf>,
3166 L<FS::cust_svc>, L<FS::part_svc>, L<FS::cust_pkg>, L<FS::queue>,
3167 L<freeside-queued>), L<FS::svc_acct_pop>,
3168 schema.html from the base documentation.