2 use base qw( FS::svc_Domain_Mixin FS::svc_PBX_Mixin
3 FS::svc_CGP_Mixin FS::svc_CGPRule_Mixin
11 use vars qw( $DEBUG $me $conf $skip_fuzzyfiles
12 $dir_prefix @shells $usernamemin
13 $usernamemax $passwordmin $passwordmax
14 $username_ampersand $username_letter $username_letterfirst
15 $username_noperiod $username_nounderscore $username_nodash
16 $username_uppercase $username_percent $username_colon
17 $username_slash $username_equals $username_pound
19 $password_noampersand $password_noexclamation
20 $warning_template $warning_from $warning_subject $warning_mimetype
23 $radius_password $radius_ip
26 use Scalar::Util qw( blessed );
31 use Crypt::PasswdMD5 1.2;
32 use Digest::SHA 'sha1_base64';
33 use Digest::MD5 'md5_base64';
36 use Authen::Passphrase;
37 use FS::UID qw( datasrc driver_name );
39 use FS::Record qw( qsearch qsearchs fields dbh dbdef );
40 use FS::Msgcat qw(gettext);
41 use FS::UI::bytecount;
43 use FS::PagedSearch qw( psearch ); # XXX in v4, replace with FS::Cursor
47 use FS::cust_main_invoice;
52 use FS::radius_usergroup;
62 $me = '[FS::svc_acct]';
64 #ask FS::UID to run this stuff for us later
65 FS::UID->install_callback( sub {
67 $dir_prefix = $conf->config('home');
68 @shells = $conf->config('shells');
69 $usernamemin = $conf->config('usernamemin') || 2;
70 $usernamemax = $conf->config('usernamemax');
71 $passwordmin = $conf->config('passwordmin'); # || 6;
73 $passwordmin = ( defined($passwordmin) && $passwordmin =~ /\d+/ )
76 $passwordmax = $conf->config('passwordmax') || 8;
77 $username_letter = $conf->exists('username-letter');
78 $username_letterfirst = $conf->exists('username-letterfirst');
79 $username_noperiod = $conf->exists('username-noperiod');
80 $username_nounderscore = $conf->exists('username-nounderscore');
81 $username_nodash = $conf->exists('username-nodash');
82 $username_uppercase = $conf->exists('username-uppercase');
83 $username_ampersand = $conf->exists('username-ampersand');
84 $username_percent = $conf->exists('username-percent');
85 $username_colon = $conf->exists('username-colon');
86 $username_slash = $conf->exists('username-slash');
87 $username_equals = $conf->exists('username-equals');
88 $username_pound = $conf->exists('username-pound');
89 $username_exclamation = $conf->exists('username-exclamation');
90 $password_noampersand = $conf->exists('password-noexclamation');
91 $password_noexclamation = $conf->exists('password-noexclamation');
92 $dirhash = $conf->config('dirhash') || 0;
93 if ( $conf->exists('warning_email') ) {
94 $warning_template = new Text::Template (
96 SOURCE => [ map "$_\n", $conf->config('warning_email') ]
97 ) or warn "can't create warning email template: $Text::Template::ERROR";
98 $warning_from = $conf->config('warning_email-from'); # || 'your-isp-is-dum'
99 $warning_subject = $conf->config('warning_email-subject') || 'Warning';
100 $warning_mimetype = $conf->config('warning_email-mimetype') || 'text/plain';
101 $warning_cc = $conf->config('warning_email-cc');
103 $warning_template = '';
105 $warning_subject = '';
106 $warning_mimetype = '';
109 $smtpmachine = $conf->config('smtpmachine');
110 $radius_password = $conf->config('radius-password') || 'Password';
111 $radius_ip = $conf->config('radius-ip') || 'Framed-IP-Address';
112 @pw_set = ( 'A'..'Z' ) if $conf->exists('password-generated-allcaps');
116 @saltset = ( 'a'..'z' , 'A'..'Z' , '0'..'9' , '.' , '/' );
117 @pw_set = ( 'a'..'z', 'A'..'Z', '0'..'9', '(', ')', '#', '.', ',' );
121 my ( $hashref, $cache ) = @_;
122 if ( $hashref->{'svc_acct_svcnum'} ) {
123 $self->{'_domsvc'} = FS::svc_domain->new( {
124 'svcnum' => $hashref->{'domsvc'},
125 'domain' => $hashref->{'svc_acct_domain'},
126 'catchall' => $hashref->{'svc_acct_catchall'},
133 FS::svc_acct - Object methods for svc_acct records
139 $record = new FS::svc_acct \%hash;
140 $record = new FS::svc_acct { 'column' => 'value' };
142 $error = $record->insert;
144 $error = $new_record->replace($old_record);
146 $error = $record->delete;
148 $error = $record->check;
150 $error = $record->suspend;
152 $error = $record->unsuspend;
154 $error = $record->cancel;
156 %hash = $record->radius;
158 %hash = $record->radius_reply;
160 %hash = $record->radius_check;
162 $domain = $record->domain;
164 $svc_domain = $record->svc_domain;
166 $email = $record->email;
168 $seconds_since = $record->seconds_since($timestamp);
172 An FS::svc_acct object represents an account. FS::svc_acct inherits from
173 FS::svc_Common. The following fields are currently supported:
179 Primary key (assigned automatcially for new accounts)
187 =item _password_encoding
189 plain, crypt, ldap (or empty for autodetection)
197 Point of presence (see L<FS::svc_acct_pop>)
209 set automatically if blank (and uid is not)
229 svcnum from svc_domain
233 Optional svcnum from svc_pbx
235 =item radius_I<Radius_Attribute>
237 I<Radius-Attribute> (reply)
239 =item rc_I<Radius_Attribute>
241 I<Radius-Attribute> (check)
251 Creates a new account. To add the account to the database, see L<"insert">.
258 'longname_plural' => 'Access accounts and mailboxes',
259 'sorts' => [ 'username', 'uid', 'seconds', 'last_login' ],
260 'display_weight' => 10,
261 'cancel_weight' => 50,
262 'ip_field' => 'slipip',
264 'dir' => 'Home directory',
267 def_info => 'set to fixed and blank for no UIDs',
270 'slipip' => 'IP address',
271 # 'popnum' => qq!<A HREF="$p/browse/svc_acct_pop.cgi/">POP number</A>!,
273 label => 'Access number',
275 select_table => 'svc_acct_pop',
276 select_key => 'popnum',
277 select_label => 'city',
283 disable_default => 1,
287 'password_selfchange' => { label => 'Password modification',
290 'password_recover' => { label => 'Password recovery',
294 label => 'Quota', #Mail storage limit
296 disable_inventory => 1,
299 label => 'File storage limit',
301 disable_inventory => 1,
304 label => 'Number of files limit',
306 disable_inventory => 1,
309 label => 'File size limit',
311 disable_inventory => 1,
313 '_password' => 'Password',
316 def_info => 'when blank, defaults to UID',
321 def_info => 'set to blank for no shell tracking',
323 #select_list => [ $conf->config('shells') ],
324 select_list => [ $conf ? $conf->config('shells') : () ],
325 disable_inventory => 1,
328 'finger' => 'Real name', # (GECOS)',
332 select_table => 'svc_domain',
333 select_key => 'svcnum',
334 select_label => 'domain',
335 disable_inventory => 1,
337 'pbxsvc' => { label => 'PBX',
338 type => 'select-svc_pbx.html',
339 disable_inventory => 1,
340 disable_select => 1, #UI wonky, pry works otherwise
342 'sectornum' => 'Tower sector',
344 label => 'RADIUS groups',
345 type => 'select-radius_group.html',
346 disable_inventory => 1,
350 'seconds' => { label => 'Seconds',
351 label_sort => 'with Time Remaining',
353 disable_inventory => 1,
355 disable_part_svc_column => 1,
357 'upbytes' => { label => 'Upload',
359 disable_inventory => 1,
361 'format' => \&FS::UI::bytecount::display_bytecount,
362 'parse' => \&FS::UI::bytecount::parse_bytecount,
363 disable_part_svc_column => 1,
365 'downbytes' => { label => 'Download',
367 disable_inventory => 1,
369 'format' => \&FS::UI::bytecount::display_bytecount,
370 'parse' => \&FS::UI::bytecount::parse_bytecount,
371 disable_part_svc_column => 1,
373 'totalbytes'=> { label => 'Total up and download',
375 disable_inventory => 1,
377 'format' => \&FS::UI::bytecount::display_bytecount,
378 'parse' => \&FS::UI::bytecount::parse_bytecount,
379 disable_part_svc_column => 1,
381 'seconds_threshold' => { label => 'Seconds threshold',
383 disable_inventory => 1,
385 disable_part_svc_column => 1,
387 'upbytes_threshold' => { label => 'Upload threshold',
389 disable_inventory => 1,
391 'format' => \&FS::UI::bytecount::display_bytecount,
392 'parse' => \&FS::UI::bytecount::parse_bytecount,
393 disable_part_svc_column => 1,
395 'downbytes_threshold' => { label => 'Download threshold',
397 disable_inventory => 1,
399 'format' => \&FS::UI::bytecount::display_bytecount,
400 'parse' => \&FS::UI::bytecount::parse_bytecount,
401 disable_part_svc_column => 1,
403 'totalbytes_threshold'=> { label => 'Total up and download threshold',
405 disable_inventory => 1,
407 'format' => \&FS::UI::bytecount::display_bytecount,
408 'parse' => \&FS::UI::bytecount::parse_bytecount,
409 disable_part_svc_column => 1,
412 label => 'Last login',
416 label => 'Last logout',
421 label => 'Communigate aliases',
423 disable_inventory => 1,
428 label => 'Communigate account type',
430 select_list => [qw( MultiMailbox TextMailbox MailDirMailbox AGrade BGrade CGrade )],
431 disable_inventory => 1,
434 'cgp_accessmodes' => {
435 label => 'Communigate enabled services',
436 type => 'communigate_pro-accessmodes',
437 disable_inventory => 1,
440 'cgp_rulesallowed' => {
441 label => 'Allowed mail rules',
443 select_list => [ '', 'No', 'Filter Only', 'All But Exec', 'Any' ],
444 disable_inventory => 1,
447 'cgp_rpopallowed' => { label => 'RPOP modifications',
450 'cgp_mailtoall' => { label => 'Accepts mail to "all"',
453 'cgp_addmailtrailer' => { label => 'Add trailer to sent mail',
456 'cgp_archiveafter' => {
457 label => 'Archive messages after',
460 -2 => 'default(730 days)',
467 1209600 => '2 weeks',
468 2592000 => '30 days',
469 7776000 => '90 days',
470 15552000 => '180 days',
471 31536000 => '365 days',
472 63072000 => '730 days',
474 disable_inventory => 1,
480 'cgp_deletemode' => {
481 label => 'Communigate message delete method',
483 select_list => [ 'Move To Trash', 'Immediately', 'Mark' ],
484 disable_inventory => 1,
487 'cgp_emptytrash' => {
488 label => 'Communigate on logout remove trash',
490 select_list => __PACKAGE__->cgp_emptytrash_values,
491 disable_inventory => 1,
495 label => 'Communigate language',
497 select_list => [ '', qw( English Arabic Chinese Dutch French German Hebrew Italian Japanese Portuguese Russian Slovak Spanish Thai ) ],
498 disable_inventory => 1,
502 label => 'Communigate time zone',
504 select_list => __PACKAGE__->cgp_timezone_values,
505 disable_inventory => 1,
509 label => 'Communigate layout',
511 select_list => [ '', '***', 'GoldFleece', 'Skin2' ],
512 disable_inventory => 1,
515 'cgp_prontoskinname' => {
516 label => 'Communigate Pronto style',
518 select_list => [ '', 'Pronto', 'Pronto-darkflame', 'Pronto-steel', 'Pronto-twilight', ],
519 disable_inventory => 1,
522 'cgp_sendmdnmode' => {
523 label => 'Communigate send read receipts',
525 select_list => [ '', 'Never', 'Manually', 'Automatically' ],
526 disable_inventory => 1,
537 sub table { 'svc_acct'; }
539 sub table_dupcheck_fields { ( 'username', 'domsvc' ); }
542 shift->_lastlog('in', @_);
546 shift->_lastlog('out', @_);
550 my( $self, $op, $time ) = @_;
552 if ( defined($time) ) {
553 warn "$me last_log$op called on svcnum ". $self->svcnum.
554 ' ('. $self->email. "): $time\n"
559 my $sql = "UPDATE svc_acct SET last_log$op = ? WHERE svcnum = ?";
563 my $sth = $dbh->prepare( $sql )
564 or die "Error preparing $sql: ". $dbh->errstr;
565 my $rv = $sth->execute($time, $self->svcnum);
566 die "Error executing $sql: ". $sth->errstr
568 die "Can't update last_log$op for svcnum". $self->svcnum
571 $self->{'Hash'}->{"last_log$op"} = $time;
573 $self->getfield("last_log$op");
577 =item search_sql STRING
579 Class method which returns an SQL fragment to search for the given string.
584 my( $class, $string ) = @_;
585 if ( $string =~ /^([^@]+)@([^@]+)$/ ) {
586 my( $username, $domain ) = ( $1, $2 );
587 my $q_username = dbh->quote($username);
588 my @svc_domain = qsearch('svc_domain', { 'domain' => $domain } );
590 "svc_acct.username = $q_username AND ( ".
591 join( ' OR ', map { "svc_acct.domsvc = ". $_->svcnum; } @svc_domain ).
596 } elsif ( $string =~ /^(\d{1,3}\.){3}\d{1,3}$/ ) {
598 $class->search_sql_field('slipip', $string ).
600 $class->search_sql_field('username', $string ).
603 $class->search_sql_field('username', $string);
607 =item label [ END_TIMESTAMP [ START_TIMESTAMP ] ]
609 Returns the "username@domain" string for this account.
611 END_TIMESTAMP and START_TIMESTAMP can optionally be passed when dealing with
621 =item label_long [ END_TIMESTAMP [ START_TIMESTAMP ] ]
623 Returns a longer string label for this acccount ("Real Name <username@domain>"
624 if available, or "username@domain").
626 END_TIMESTAMP and START_TIMESTAMP can optionally be passed when dealing with
633 my $label = $self->label(@_);
634 my $finger = $self->finger;
635 return $label unless $finger =~ /\S/;
636 my $maxlen = 40 - length($label) - length($self->cust_svc->part_svc->svc);
637 $finger = substr($finger, 0, $maxlen-3).'...' if length($finger) > $maxlen;
641 =item insert [ , OPTION => VALUE ... ]
643 Adds this account to the database. If there is an error, returns the error,
644 otherwise returns false.
646 The additional fields pkgnum and svcpart (see L<FS::cust_svc>) should be
647 defined. An FS::cust_svc record will be created and inserted.
649 The additional field I<usergroup> can optionally be defined; if so it should
650 contain an arrayref of group names. See L<FS::radius_usergroup>.
652 The additional field I<child_objects> can optionally be defined; if so it
653 should contain an arrayref of FS::tablename objects. They will have their
654 svcnum fields set and will be inserted after this record, but before any
655 exports are run. Each element of the array can also optionally be a
656 two-element array reference containing the child object and the name of an
657 alternate field to be filled in with the newly-inserted svcnum, for example
658 C<[ $svc_forward, 'srcsvc' ]>
660 Currently available options are: I<depend_jobnum>
662 If I<depend_jobnum> is set (to a scalar jobnum or an array reference of
663 jobnums), all provisioning jobs will have a dependancy on the supplied
664 jobnum(s) (they will not run until the specific job(s) complete(s)).
666 (TODOC: L<FS::queue> and L<freeside-queued>)
668 (TODOC: new exports!)
677 warn "[$me] insert called on $self: ". Dumper($self).
678 "\nwith options: ". Dumper(%options);
681 local $SIG{HUP} = 'IGNORE';
682 local $SIG{INT} = 'IGNORE';
683 local $SIG{QUIT} = 'IGNORE';
684 local $SIG{TERM} = 'IGNORE';
685 local $SIG{TSTP} = 'IGNORE';
686 local $SIG{PIPE} = 'IGNORE';
688 my $oldAutoCommit = $FS::UID::AutoCommit;
689 local $FS::UID::AutoCommit = 0;
693 my $error = $self->SUPER::insert( # usergroup is here
694 'jobnums' => \@jobnums,
695 'child_objects' => $self->child_objects,
699 $dbh->rollback if $oldAutoCommit;
703 unless ( $skip_fuzzyfiles ) {
704 $error = $self->queue_fuzzyfiles_update;
706 $dbh->rollback if $oldAutoCommit;
707 return "updating fuzzy search cache: $error";
711 my $cust_pkg = $self->cust_svc->cust_pkg;
714 my $cust_main = $cust_pkg->cust_main;
715 my $agentnum = $cust_main->agentnum;
717 if ( $conf->exists('emailinvoiceautoalways')
718 || $conf->exists('emailinvoiceauto')
719 && ! $cust_main->invoicing_list_emailonly
721 my @invoicing_list = $cust_main->invoicing_list;
722 push @invoicing_list, $self->email;
723 $cust_main->invoicing_list(\@invoicing_list);
727 my @welcome_exclude_svcparts = $conf->config('svc_acct_welcome_exclude');
728 unless ( grep { $_ eq $self->svcpart } @welcome_exclude_svcparts ) {
730 my $msgnum = $conf->config('welcome_msgnum', $agentnum);
732 my $msg_template = qsearchs('msg_template', { msgnum => $msgnum });
733 $error = $msg_template->send('cust_main' => $cust_main,
737 my ($to,$welcome_template,$welcome_from,$welcome_subject,$welcome_subject_template,$welcome_mimetype)
738 = ('','','','','','');
740 if ( $conf->exists('welcome_email', $agentnum) ) {
741 $welcome_template = new Text::Template (
743 SOURCE => [ map "$_\n", $conf->config('welcome_email', $agentnum) ]
744 ) or warn "can't create welcome email template: $Text::Template::ERROR";
745 $welcome_from = $conf->config('welcome_email-from', $agentnum);
746 # || 'your-isp-is-dum'
747 $welcome_subject = $conf->config('welcome_email-subject', $agentnum)
749 $welcome_subject_template = new Text::Template (
751 SOURCE => $welcome_subject,
752 ) or warn "can't create welcome email subject template: $Text::Template::ERROR";
753 $welcome_mimetype = $conf->config('welcome_email-mimetype', $agentnum)
756 if ( $welcome_template ) {
757 my $to = join(', ', grep { $_ !~ /^(POST|FAX)$/ } $cust_main->invoicing_list );
761 'custnum' => $self->custnum,
762 'username' => $self->username,
763 'password' => $self->_password,
764 'first' => $cust_main->first,
765 'last' => $cust_main->getfield('last'),
766 'pkg' => $cust_pkg->part_pkg->pkg,
768 my $wqueue = new FS::queue {
769 'svcnum' => $self->svcnum,
770 'job' => 'FS::svc_acct::send_email'
772 my $error = $wqueue->insert(
774 'from' => $welcome_from,
775 'subject' => $welcome_subject_template->fill_in( HASH => \%hash, ),
776 'mimetype' => $welcome_mimetype,
777 'body' => $welcome_template->fill_in( HASH => \%hash, ),
780 $dbh->rollback if $oldAutoCommit;
781 return "error queuing welcome email: $error";
784 if ( $options{'depend_jobnum'} ) {
785 warn "$me depend_jobnum found; adding to welcome email dependancies"
787 if ( ref($options{'depend_jobnum'}) ) {
788 warn "$me adding jobs ". join(', ', @{$options{'depend_jobnum'}} ).
789 "to welcome email dependancies"
791 push @jobnums, @{ $options{'depend_jobnum'} };
793 warn "$me adding job $options{'depend_jobnum'} ".
794 "to welcome email dependancies"
796 push @jobnums, $options{'depend_jobnum'};
800 foreach my $jobnum ( @jobnums ) {
801 my $error = $wqueue->depend_insert($jobnum);
803 $dbh->rollback if $oldAutoCommit;
804 return "error queuing welcome email job dependancy: $error";
810 } # if $welcome_template
815 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
819 # set usage fields and thresholds if unset but set in a package def
820 # AND the package already has a last bill date (otherwise they get double added)
821 sub preinsert_hook_first {
824 return '' unless $self->pkgnum;
826 my $cust_pkg = qsearchs( 'cust_pkg', { 'pkgnum' => $self->pkgnum } );
827 return '' unless $cust_pkg && $cust_pkg->last_bill;
829 my $part_pkg = $cust_pkg->part_pkg;
830 return '' unless $part_pkg && $part_pkg->can('usage_valuehash');
832 my %values = $part_pkg->usage_valuehash;
833 my $multiplier = $conf->exists('svc_acct-usage_threshold')
834 ? 1 - $conf->config('svc_acct-usage_threshold')/100
835 : 0.20; #doesn't matter
837 foreach ( keys %values ) {
838 next if $self->getfield($_);
839 $self->setfield( $_, $values{$_} );
840 $self->setfield( $_. '_threshold', int( $values{$_} * $multiplier ) )
841 if $conf->exists('svc_acct-usage_threshold');
849 Deletes this account from the database. If there is an error, returns the
850 error, otherwise returns false.
852 The corresponding FS::cust_svc record will be deleted as well.
854 (TODOC: new exports!)
861 return "can't delete system account" if $self->_check_system;
863 return "Can't delete an account which is a (svc_forward) source!"
864 if qsearch( 'svc_forward', { 'srcsvc' => $self->svcnum } );
866 return "Can't delete an account which is a (svc_forward) destination!"
867 if qsearch( 'svc_forward', { 'dstsvc' => $self->svcnum } );
869 return "Can't delete an account with (svc_www) web service!"
870 if qsearch( 'svc_www', { 'usersvc' => $self->svcnum } );
872 # what about records in session ? (they should refer to history table)
874 local $SIG{HUP} = 'IGNORE';
875 local $SIG{INT} = 'IGNORE';
876 local $SIG{QUIT} = 'IGNORE';
877 local $SIG{TERM} = 'IGNORE';
878 local $SIG{TSTP} = 'IGNORE';
879 local $SIG{PIPE} = 'IGNORE';
881 my $oldAutoCommit = $FS::UID::AutoCommit;
882 local $FS::UID::AutoCommit = 0;
885 foreach my $cust_main_invoice (
886 qsearch( 'cust_main_invoice', { 'dest' => $self->svcnum } )
888 unless ( defined($cust_main_invoice) ) {
889 warn "WARNING: something's wrong with qsearch";
892 my %hash = $cust_main_invoice->hash;
893 $hash{'dest'} = $self->email;
894 my $new = new FS::cust_main_invoice \%hash;
895 my $error = $new->replace($cust_main_invoice);
897 $dbh->rollback if $oldAutoCommit;
902 foreach my $svc_domain (
903 qsearch( 'svc_domain', { 'catchall' => $self->svcnum } )
905 my %hash = new FS::svc_domain->hash;
906 $hash{'catchall'} = '';
907 my $new = new FS::svc_domain \%hash;
908 my $error = $new->replace($svc_domain);
910 $dbh->rollback if $oldAutoCommit;
915 my $error = $self->SUPER::delete; # usergroup here
917 $dbh->rollback if $oldAutoCommit;
921 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
925 =item replace OLD_RECORD
927 Replaces OLD_RECORD with this one in the database. If there is an error,
928 returns the error, otherwise returns false.
930 The additional field I<usergroup> can optionally be defined; if so it should
931 contain an arrayref of group names. See L<FS::radius_usergroup>.
939 my $old = ( blessed($_[0]) && $_[0]->isa('FS::Record') )
943 warn "$me replacing $old with $new\n" if $DEBUG;
947 return "can't modify system account" if $old->_check_system;
950 #no warnings 'numeric'; #alas, a 5.006-ism
953 foreach my $xid (qw( uid gid )) {
955 return "Can't change $xid!"
956 if ! $conf->exists("svc_acct-edit_$xid")
957 && $old->$xid() != $new->$xid()
958 && $new->cust_svc->part_svc->part_svc_column($xid)->columnflag ne 'F'
963 return "can't change username"
964 if $old->username ne $new->username
965 && $conf->exists('svc_acct-no_edit_username');
967 #change homdir when we change username
968 $new->setfield('dir', '') if $old->username ne $new->username;
970 local $SIG{HUP} = 'IGNORE';
971 local $SIG{INT} = 'IGNORE';
972 local $SIG{QUIT} = 'IGNORE';
973 local $SIG{TERM} = 'IGNORE';
974 local $SIG{TSTP} = 'IGNORE';
975 local $SIG{PIPE} = 'IGNORE';
977 my $oldAutoCommit = $FS::UID::AutoCommit;
978 local $FS::UID::AutoCommit = 0;
981 $error = $new->SUPER::replace($old, @_); # usergroup here
983 $dbh->rollback if $oldAutoCommit;
984 return $error if $error;
987 if ( $new->username ne $old->username && ! $skip_fuzzyfiles ) {
988 $error = $new->queue_fuzzyfiles_update;
990 $dbh->rollback if $oldAutoCommit;
991 return "updating fuzzy search cache: $error";
995 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
999 =item queue_fuzzyfiles_update
1001 Used by insert & replace to update the fuzzy search cache
1005 sub queue_fuzzyfiles_update {
1008 local $SIG{HUP} = 'IGNORE';
1009 local $SIG{INT} = 'IGNORE';
1010 local $SIG{QUIT} = 'IGNORE';
1011 local $SIG{TERM} = 'IGNORE';
1012 local $SIG{TSTP} = 'IGNORE';
1013 local $SIG{PIPE} = 'IGNORE';
1015 my $oldAutoCommit = $FS::UID::AutoCommit;
1016 local $FS::UID::AutoCommit = 0;
1019 my $queue = new FS::queue {
1020 'svcnum' => $self->svcnum,
1021 'job' => 'FS::svc_acct::append_fuzzyfiles'
1023 my $error = $queue->insert($self->username);
1025 $dbh->rollback if $oldAutoCommit;
1026 return "queueing job (transaction rolled back): $error";
1029 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
1037 Suspends this account by calling export-specific suspend hooks. If there is
1038 an error, returns the error, otherwise returns false.
1040 Called by the suspend method of FS::cust_pkg (see L<FS::cust_pkg>).
1046 return "can't suspend system account" if $self->_check_system;
1047 $self->SUPER::suspend(@_);
1052 Unsuspends this account by by calling export-specific suspend hooks. If there
1053 is an error, returns the error, otherwise returns false.
1055 Called by the unsuspend method of FS::cust_pkg (see L<FS::cust_pkg>).
1061 my %hash = $self->hash;
1062 if ( $hash{_password} =~ /^\*SUSPENDED\* (.*)$/ ) {
1063 $hash{_password} = $1;
1064 my $new = new FS::svc_acct ( \%hash );
1065 my $error = $new->replace($self);
1066 return $error if $error;
1069 $self->SUPER::unsuspend(@_);
1074 Called by the cancel method of FS::cust_pkg (see L<FS::cust_pkg>).
1076 If the B<auto_unset_catchall> configuration option is set, this method will
1077 automatically remove any references to the canceled service in the catchall
1078 field of svc_domain. This allows packages that contain both a svc_domain and
1079 its catchall svc_acct to be canceled in one step.
1084 # Only one thing to do at this level
1086 foreach my $svc_domain (
1087 qsearch( 'svc_domain', { catchall => $self->svcnum } ) ) {
1088 if($conf->exists('auto_unset_catchall')) {
1089 my %hash = $svc_domain->hash;
1090 $hash{catchall} = '';
1091 my $new = new FS::svc_domain ( \%hash );
1092 my $error = $new->replace($svc_domain);
1093 return $error if $error;
1095 return "cannot unprovision svc_acct #".$self->svcnum.
1096 " while assigned as catchall for svc_domain #".$svc_domain->svcnum;
1100 $self->SUPER::cancel(@_);
1106 Checks all fields to make sure this is a valid service. If there is an error,
1107 returns the error, otherwise returns false. Called by the insert and replace
1110 Sets any fixed values; see L<FS::part_svc>.
1117 my($recref) = $self->hashref;
1119 my $x = $self->setfixed;
1120 return $x unless ref($x);
1123 my $error = $self->ut_numbern('svcnum')
1124 #|| $self->ut_number('domsvc')
1125 || $self->ut_foreign_key( 'domsvc', 'svc_domain', 'svcnum' )
1126 || $self->ut_foreign_keyn('pbxsvc', 'svc_pbx', 'svcnum' )
1127 || $self->ut_foreign_keyn('sectornum','tower_sector','sectornum')
1128 || $self->ut_foreign_keyn('routernum','router','routernum')
1129 || $self->ut_foreign_keyn('blocknum','addr_block','blocknum')
1130 || $self->ut_textn('sec_phrase')
1131 || $self->ut_snumbern('seconds')
1132 || $self->ut_snumbern('upbytes')
1133 || $self->ut_snumbern('downbytes')
1134 || $self->ut_snumbern('totalbytes')
1135 || $self->ut_snumbern('seconds_threshold')
1136 || $self->ut_snumbern('upbytes_threshold')
1137 || $self->ut_snumbern('downbytes_threshold')
1138 || $self->ut_snumbern('totalbytes_threshold')
1139 || $self->ut_enum('_password_encoding', ['',qw(plain crypt ldap)])
1140 || $self->ut_enum('password_selfchange', [ '', 'Y' ])
1141 || $self->ut_enum('password_recover', [ '', 'Y' ])
1143 || $self->ut_anything('cf_privatekey')
1145 || $self->ut_textn('cgp_accessmodes')
1146 || $self->ut_alphan('cgp_type')
1147 || $self->ut_textn('cgp_aliases' ) #well
1149 || $self->ut_alphasn('cgp_rulesallowed')
1150 || $self->ut_enum('cgp_rpopallowed', [ '', 'Y' ])
1151 || $self->ut_enum('cgp_mailtoall', [ '', 'Y' ])
1152 || $self->ut_enum('cgp_addmailtrailer', [ '', 'Y' ])
1153 || $self->ut_snumbern('cgp_archiveafter')
1155 || $self->ut_alphasn('cgp_deletemode')
1156 || $self->ut_enum('cgp_emptytrash', $self->cgp_emptytrash_values)
1157 || $self->ut_alphan('cgp_language')
1158 || $self->ut_textn('cgp_timezone')
1159 || $self->ut_textn('cgp_skinname')
1160 || $self->ut_textn('cgp_prontoskinname')
1161 || $self->ut_alphan('cgp_sendmdnmode')
1163 return $error if $error;
1165 # assign IP address, etc.
1166 if ( $conf->exists('svc_acct-ip_addr') ) {
1167 my $error = $self->svc_ip_check;
1168 return $error if $error;
1169 } else { # I think this is correct
1170 $self->routernum('');
1171 $self->blocknum('');
1175 local $username_letter = $username_letter;
1176 local $username_uppercase = $username_uppercase;
1177 if ($self->svcnum) {
1178 my $cust_svc = $self->cust_svc
1179 or return "no cust_svc record found for svcnum ". $self->svcnum;
1180 my $cust_pkg = $cust_svc->cust_pkg;
1182 if ($self->pkgnum) {
1183 $cust_pkg = qsearchs('cust_pkg', { 'pkgnum' => $self->pkgnum } );#complain?
1187 $conf->exists('username-letter', $cust_pkg->cust_main->agentnum);
1188 $username_uppercase =
1189 $conf->exists('username-uppercase', $cust_pkg->cust_main->agentnum);
1192 my $ulen = $usernamemax || $self->dbdef_table->column('username')->length;
1194 $recref->{username} =~ /^([a-z0-9_\-\.\&\%\:\/\=\#\!]{$usernamemin,$ulen})$/i
1195 or return gettext('illegal_username'). " ($usernamemin-$ulen): ". $recref->{username};
1196 $recref->{username} = $1;
1198 my $uerror = gettext('illegal_username'). ': '. $recref->{username};
1200 unless ( $username_uppercase ) {
1201 $recref->{username} =~ /[A-Z]/ and return $uerror;
1203 if ( $username_letterfirst ) {
1204 $recref->{username} =~ /^[a-z]/ or return $uerror;
1205 } elsif ( $username_letter ) {
1206 $recref->{username} =~ /[a-z]/ or return $uerror;
1208 if ( $username_noperiod ) {
1209 $recref->{username} =~ /\./ and return $uerror;
1211 if ( $username_nounderscore ) {
1212 $recref->{username} =~ /_/ and return $uerror;
1214 if ( $username_nodash ) {
1215 $recref->{username} =~ /\-/ and return $uerror;
1217 unless ( $username_ampersand ) {
1218 $recref->{username} =~ /\&/ and return $uerror;
1220 unless ( $username_percent ) {
1221 $recref->{username} =~ /\%/ and return $uerror;
1223 unless ( $username_colon ) {
1224 $recref->{username} =~ /\:/ and return $uerror;
1226 unless ( $username_slash ) {
1227 $recref->{username} =~ /\// and return $uerror;
1229 unless ( $username_equals ) {
1230 $recref->{username} =~ /\=/ and return $uerror;
1232 unless ( $username_pound ) {
1233 $recref->{username} =~ /\#/ and return $uerror;
1235 unless ( $username_exclamation ) {
1236 $recref->{username} =~ /\!/ and return $uerror;
1240 $recref->{popnum} =~ /^(\d*)$/ or return "Illegal popnum: ".$recref->{popnum};
1241 $recref->{popnum} = $1;
1242 return "Unknown popnum" unless
1243 ! $recref->{popnum} ||
1244 qsearchs('svc_acct_pop',{'popnum'=> $recref->{popnum} } );
1246 unless ( $part_svc->part_svc_column('uid')->columnflag eq 'F' ) {
1248 $recref->{uid} =~ /^(\d*)$/ or return "Illegal uid";
1249 $recref->{uid} = $1 eq '' ? $self->unique('uid') : $1;
1251 $recref->{gid} =~ /^(\d*)$/ or return "Illegal gid";
1252 $recref->{gid} = $1 eq '' ? $recref->{uid} : $1;
1253 #not all systems use gid=uid
1254 #you can set a fixed gid in part_svc
1256 return "Only root can have uid 0"
1257 if $recref->{uid} == 0
1258 && $recref->{username} !~ /^(root|toor|smtp)$/;
1260 unless ( $recref->{username} eq 'sync' ) {
1261 if ( grep $_ eq $recref->{shell}, @shells ) {
1262 $recref->{shell} = (grep $_ eq $recref->{shell}, @shells)[0];
1264 return "Illegal shell \`". $self->shell. "\'; ".
1265 "shells configuration value contains: @shells";
1268 $recref->{shell} = '/bin/sync';
1272 $recref->{gid} ne '' ?
1273 return "Can't have gid without uid" : ( $recref->{gid}='' );
1274 #$recref->{dir} ne '' ?
1275 # return "Can't have directory without uid" : ( $recref->{dir}='' );
1276 $recref->{shell} ne '' ?
1277 return "Can't have shell without uid" : ( $recref->{shell}='' );
1280 unless ( $part_svc->part_svc_column('dir')->columnflag eq 'F' ) {
1282 $recref->{dir} =~ /^([\/\w\-\.\&\:\#]*)$/
1283 or return "Illegal directory: ". $recref->{dir};
1284 $recref->{dir} = $1;
1285 return "Illegal directory"
1286 if $recref->{dir} =~ /(^|\/)\.+(\/|$)/; #no .. component
1287 return "Illegal directory"
1288 if $recref->{dir} =~ /\&/ && ! $username_ampersand;
1289 unless ( $recref->{dir} ) {
1290 $recref->{dir} = $dir_prefix . '/';
1291 if ( $dirhash > 0 ) {
1292 for my $h ( 1 .. $dirhash ) {
1293 $recref->{dir} .= substr($recref->{username}, $h-1, 1). '/';
1295 } elsif ( $dirhash < 0 ) {
1296 for my $h ( reverse $dirhash .. -1 ) {
1297 $recref->{dir} .= substr($recref->{username}, $h, 1). '/';
1300 $recref->{dir} .= $recref->{username};
1306 if ( $self->getfield('finger') eq '' ) {
1307 my $cust_pkg = $self->svcnum
1308 ? $self->cust_svc->cust_pkg
1309 : qsearchs('cust_pkg', { 'pkgnum' => $self->getfield('pkgnum') } );
1311 my $cust_main = $cust_pkg->cust_main;
1312 $self->setfield('finger', $cust_main->first.' '.$cust_main->get('last') );
1315 # $error = $self->ut_textn('finger');
1316 # return $error if $error;
1317 $self->getfield('finger') =~ /^([\w \,\.\-\'\&\t\!\@\#\$\%\(\)\+\;\"\?\/\*\<\>]*)$/
1318 or return "Illegal finger: ". $self->getfield('finger');
1319 $self->setfield('finger', $1);
1321 for (qw( quota file_quota file_maxsize )) {
1322 $recref->{$_} =~ /^(\w*)$/ or return "Illegal $_";
1325 $recref->{file_maxnum} =~ /^\s*(\d*)\s*$/ or return "Illegal file_maxnum";
1326 $recref->{file_maxnum} = $1;
1328 unless ( $part_svc->part_svc_column('slipip')->columnflag eq 'F' ) {
1329 if ( $recref->{slipip} eq '' ) {
1330 $recref->{slipip} = ''; # eh?
1331 } elsif ( $recref->{slipip} eq '0e0' ) {
1332 $recref->{slipip} = '0e0';
1334 $recref->{slipip} =~ /^(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})$/
1335 or return "Illegal slipip: ". $self->slipip;
1336 $recref->{slipip} = $1;
1340 #arbitrary RADIUS stuff; allow ut_textn for now
1341 foreach ( grep /^radius_/, fields('svc_acct') ) {
1342 $self->ut_textn($_);
1345 # First, if _password is blank, generate one and set default encoding.
1346 if ( ! $recref->{_password} ) {
1347 $error = $self->set_password('');
1349 # But if there's a _password but no encoding, assume it's plaintext and
1350 # set it to default encoding.
1351 elsif ( ! $recref->{_password_encoding} ) {
1352 $error = $self->set_password($recref->{_password});
1354 return $error if $error;
1356 # Next, check _password to ensure compliance with the encoding.
1357 if ( $recref->{_password_encoding} eq 'ldap' ) {
1359 if ( $recref->{_password} =~ /^(\{[\w\-]+\})(!?.{0,64})$/ ) {
1360 $recref->{_password} = uc($1).$2;
1362 return 'Illegal (ldap-encoded) password: '. $recref->{_password};
1365 } elsif ( $recref->{_password_encoding} eq 'crypt' ) {
1367 if ( $recref->{_password} =~
1368 #/^(\$\w+\$.*|[\w\+\/]{13}|_[\w\+\/]{19}|\*)$/
1369 /^(!!?)?(\$\w+\$.*|[\w\+\/\.]{13}|_[\w\+\/\.]{19}|\*)$/
1372 $recref->{_password} = ( defined($1) ? $1 : '' ). $2;
1375 return 'Illegal (crypt-encoded) password: '. $recref->{_password};
1378 } elsif ( $recref->{_password_encoding} eq 'plain' ) {
1379 # Password randomization is now in set_password.
1380 # Strip whitespace characters, check length requirements, etc.
1381 if ( $recref->{_password} =~ /^([^\t\n]{$passwordmin,$passwordmax})$/ ) {
1382 $recref->{_password} = $1;
1384 return gettext('illegal_password'). " $passwordmin-$passwordmax ".
1385 FS::Msgcat::_gettext('illegal_password_characters').
1386 ": ". $recref->{_password};
1389 if ( $password_noampersand ) {
1390 $recref->{_password} =~ /\&/ and return gettext('illegal_password');
1392 if ( $password_noexclamation ) {
1393 $recref->{_password} =~ /\!/ and return gettext('illegal_password');
1397 return "invalid password encoding ('".$recref->{_password_encoding}."'";
1400 $self->SUPER::check;
1405 sub _password_encryption {
1407 my $encoding = lc($self->_password_encoding);
1408 return if !$encoding;
1409 return 'plain' if $encoding eq 'plain';
1410 if($encoding eq 'crypt') {
1411 my $pass = $self->_password;
1412 $pass =~ s/^\*SUSPENDED\* //;
1414 return 'md5' if $pass =~ /^\$1\$/;
1415 #return 'blowfish' if $self->_password =~ /^\$2\$/;
1416 return 'des' if length($pass) == 13;
1419 if($encoding eq 'ldap') {
1420 uc($self->_password) =~ /^\{([\w-]+)\}/;
1421 return 'crypt' if $1 eq 'CRYPT' or $1 eq 'DES';
1422 return 'plain' if $1 eq 'PLAIN' or $1 eq 'CLEARTEXT';
1423 return 'md5' if $1 eq 'MD5';
1424 return 'sha1' if $1 eq 'SHA' or $1 eq 'SHA-1';
1431 sub get_cleartext_password {
1433 if($self->_password_encryption eq 'plain') {
1434 if($self->_password_encoding eq 'ldap') {
1435 $self->_password =~ /\{\w+\}(.*)$/;
1439 return $self->_password;
1448 Set the cleartext password for the account. If _password_encoding is set, the
1449 new password will be encoded according to the existing method (including
1450 encryption mode, if it can be determined). Otherwise,
1451 config('default-password-encoding') is used.
1453 If no password is supplied (or a zero-length password when minimum password length
1454 is >0), one will be generated randomly.
1459 my( $self, $pass ) = ( shift, shift );
1461 warn "[$me] set_password (to $pass) called on $self: ". Dumper($self)
1464 my $failure = gettext('illegal_password'). " $passwordmin-$passwordmax ".
1465 FS::Msgcat::_gettext('illegal_password_characters').
1468 my( $encoding, $encryption ) = ('', '');
1470 if ( $self->_password_encoding ) {
1471 $encoding = $self->_password_encoding;
1472 # identify existing encryption method, try to use it.
1473 $encryption = $self->_password_encryption;
1475 # use the system default
1481 # set encoding to system default
1482 ($encoding, $encryption) =
1483 split(/-/, lc($conf->config('default-password-encoding') || ''));
1484 $encoding ||= 'legacy';
1485 $self->_password_encoding($encoding);
1488 if ( $encoding eq 'legacy' ) {
1490 # The legacy behavior from check():
1491 # If the password is blank, randomize it and set encoding to 'plain'.
1492 if(!defined($pass) or (length($pass) == 0 and $passwordmin)) {
1493 $pass = join('',map($pw_set[ int(rand $#pw_set) ], (0..7) ) );
1494 $self->_password_encoding('plain');
1496 # Prefix + valid-length password
1497 if ( $pass =~ /^((\*SUSPENDED\* |!!?)?)([^\t\n]{$passwordmin,$passwordmax})$/ ) {
1499 $self->_password_encoding('plain');
1500 # Prefix + crypt string
1501 } elsif ( $pass =~ /^((\*SUSPENDED\* |!!?)?)([\w\.\/\$\;\+]{13,64})$/ ) {
1503 $self->_password_encoding('crypt');
1504 # Various disabled crypt passwords
1505 } elsif ( $pass eq '*' || $pass eq '!' || $pass eq '!!' ) {
1506 $self->_password_encoding('crypt');
1512 $self->_password($pass);
1518 if $passwordmin && length($pass) < $passwordmin
1519 or $passwordmax && length($pass) > $passwordmax;
1521 if ( $encoding eq 'crypt' ) {
1522 if ($encryption eq 'md5') {
1523 $pass = unix_md5_crypt($pass);
1524 } elsif ($encryption eq 'des') {
1525 $pass = crypt($pass, $saltset[int(rand(64))].$saltset[int(rand(64))]);
1528 } elsif ( $encoding eq 'ldap' ) {
1529 if ($encryption eq 'md5') {
1530 $pass = md5_base64($pass);
1531 } elsif ($encryption eq 'sha1') {
1532 $pass = sha1_base64($pass);
1533 } elsif ($encryption eq 'crypt') {
1534 $pass = crypt($pass, $saltset[int(rand(64))].$saltset[int(rand(64))]);
1536 # else $encryption eq 'plain', do nothing
1537 $pass .= '=' x (4 - length($pass) % 4) #properly padded base64
1538 if $encryption eq 'md5' || $encryption eq 'sha1';
1539 $pass = '{'.uc($encryption).'}'.$pass;
1541 # else encoding eq 'plain'
1543 $self->_password($pass);
1549 Internal function to check the username against the list of system usernames
1550 from the I<system_usernames> configuration value. Returns true if the username
1551 is listed on the system username list.
1557 scalar( grep { $self->username eq $_ || $self->email eq $_ }
1558 $conf->config('system_usernames')
1562 =item _check_duplicate
1564 Internal method to check for duplicates usernames, username@domain pairs and
1567 If the I<global_unique-username> configuration value is set to B<username> or
1568 B<username@domain>, enforces global username or username@domain uniqueness.
1570 In all cases, check for duplicate uids and usernames or username@domain pairs
1571 per export and with identical I<svcpart> values.
1575 sub _check_duplicate {
1578 my $global_unique = $conf->config('global_unique-username') || 'none';
1579 return '' if $global_unique eq 'disabled';
1583 my $part_svc = qsearchs('part_svc', { 'svcpart' => $self->svcpart } );
1584 unless ( $part_svc ) {
1585 return 'unknown svcpart '. $self->svcpart;
1588 my @dup_user = grep { !$self->svcnum || $_->svcnum != $self->svcnum }
1589 qsearch( 'svc_acct', { 'username' => $self->username } );
1590 return gettext('username_in_use')
1591 if $global_unique eq 'username' && @dup_user;
1593 my @dup_userdomain = grep { !$self->svcnum || $_->svcnum != $self->svcnum }
1594 qsearch( 'svc_acct', { 'username' => $self->username,
1595 'domsvc' => $self->domsvc } );
1596 return gettext('username_in_use')
1597 if $global_unique eq 'username@domain' && @dup_userdomain;
1600 if ( $part_svc->part_svc_column('uid')->columnflag ne 'F'
1601 && $self->username !~ /^(toor|(hyla)?fax)$/ ) {
1602 @dup_uid = grep { !$self->svcnum || $_->svcnum != $self->svcnum }
1603 qsearch( 'svc_acct', { 'uid' => $self->uid } );
1608 if ( @dup_user || @dup_userdomain || @dup_uid ) {
1609 my $exports = FS::part_export::export_info('svc_acct');
1610 my %conflict_user_svcpart;
1611 my %conflict_userdomain_svcpart = ( $self->svcpart => 'SELF', );
1613 foreach my $part_export ( $part_svc->part_export ) {
1615 #this will catch to the same exact export
1616 my @svcparts = map { $_->svcpart } $part_export->export_svc;
1618 #this will catch to exports w/same exporthost+type ???
1619 #my @other_part_export = qsearch('part_export', {
1620 # 'machine' => $part_export->machine,
1621 # 'exporttype' => $part_export->exporttype,
1623 #foreach my $other_part_export ( @other_part_export ) {
1624 # push @svcparts, map { $_->svcpart }
1625 # qsearch('export_svc', { 'exportnum' => $part_export->exportnum });
1628 #my $nodomain = $exports->{$part_export->exporttype}{'nodomain'};
1629 #silly kludge to avoid uninitialized value errors
1630 my $nodomain = exists( $exports->{$part_export->exporttype}{'nodomain'} )
1631 ? $exports->{$part_export->exporttype}{'nodomain'}
1633 if ( $nodomain =~ /^Y/i ) {
1634 $conflict_user_svcpart{$_} = $part_export->exportnum
1637 $conflict_userdomain_svcpart{$_} = $part_export->exportnum
1642 foreach my $dup_user ( @dup_user ) {
1643 my $dup_svcpart = $dup_user->cust_svc->svcpart;
1644 if ( exists($conflict_user_svcpart{$dup_svcpart}) ) {
1645 return "duplicate username ". $self->username.
1646 ": conflicts with svcnum ". $dup_user->svcnum.
1647 " via exportnum ". $conflict_user_svcpart{$dup_svcpart};
1651 foreach my $dup_userdomain ( @dup_userdomain ) {
1652 my $dup_svcpart = $dup_userdomain->cust_svc->svcpart;
1653 if ( exists($conflict_userdomain_svcpart{$dup_svcpart}) ) {
1654 return "duplicate username\@domain ". $self->email.
1655 ": conflicts with svcnum ". $dup_userdomain->svcnum.
1656 " via exportnum ". $conflict_userdomain_svcpart{$dup_svcpart};
1660 foreach my $dup_uid ( @dup_uid ) {
1661 my $dup_svcpart = $dup_uid->cust_svc->svcpart;
1662 if ( exists($conflict_user_svcpart{$dup_svcpart})
1663 || exists($conflict_userdomain_svcpart{$dup_svcpart}) ) {
1664 return "duplicate uid ". $self->uid.
1665 ": conflicts with svcnum ". $dup_uid->svcnum.
1667 ( $conflict_user_svcpart{$dup_svcpart}
1668 || $conflict_userdomain_svcpart{$dup_svcpart} );
1680 Depriciated, use radius_reply instead.
1685 carp "FS::svc_acct::radius depriciated, use radius_reply";
1686 $_[0]->radius_reply;
1691 Returns key/value pairs, suitable for assigning to a hash, for any RADIUS
1692 reply attributes of this record.
1694 Note that this is now the preferred method for reading RADIUS attributes -
1695 accessing the columns directly is discouraged, as the column names are
1696 expected to change in the future.
1703 return %{ $self->{'radius_reply'} }
1704 if exists $self->{'radius_reply'};
1709 my($column, $attrib) = ($1, $2);
1710 #$attrib =~ s/_/\-/g;
1711 ( $FS::raddb::attrib{lc($attrib)}, $self->getfield($column) );
1712 } grep { /^radius_/ && $self->getfield($_) } fields( $self->table );
1714 if ( $self->slipip && $self->slipip ne '0e0' ) {
1715 $reply{$radius_ip} = $self->slipip;
1718 if ( $self->seconds !~ /^$/ ) {
1719 $reply{'Session-Timeout'} = $self->seconds;
1722 if ( $conf->exists('radius-chillispot-max') ) {
1723 #http://dev.coova.org/svn/coova-chilli/doc/dictionary.chillispot
1725 #hmm. just because sqlradius.pm says so?
1732 foreach my $what (qw( input output total )) {
1733 my $is = $whatis{$what}.'bytes';
1734 if ( $self->$is() =~ /\d/ ) {
1735 my $big = new Math::BigInt $self->$is();
1736 $big = new Math::BigInt '0' if $big->is_neg();
1737 my $att = "Chillispot-Max-\u$what";
1738 $reply{"$att-Octets"} = $big->copy->band(0xffffffff)->bstr;
1739 $reply{"$att-Gigawords"} = $big->copy->brsft(32)->bstr;
1750 Returns key/value pairs, suitable for assigning to a hash, for any RADIUS
1751 check attributes of this record.
1753 Note that this is now the preferred method for reading RADIUS attributes -
1754 accessing the columns directly is discouraged, as the column names are
1755 expected to change in the future.
1762 return %{ $self->{'radius_check'} }
1763 if exists $self->{'radius_check'};
1768 my($column, $attrib) = ($1, $2);
1769 #$attrib =~ s/_/\-/g;
1770 ( $FS::raddb::attrib{lc($attrib)}, $self->getfield($column) );
1771 } grep { /^rc_/ && $self->getfield($_) } fields( $self->table );
1774 my($pw_attrib, $password) = $self->radius_password;
1775 $check{$pw_attrib} = $password;
1777 my $cust_svc = $self->cust_svc;
1779 my $cust_pkg = $cust_svc->cust_pkg;
1780 if ( $cust_pkg && $cust_pkg->part_pkg->is_prepaid && $cust_pkg->bill ) {
1781 $check{'Expiration'} = time2str('%B %e %Y %T', $cust_pkg->bill ); #http://lists.cistron.nl/pipermail/freeradius-users/2005-January/040184.html
1784 warn "WARNING: no cust_svc record for svc_acct.svcnum ". $self->svcnum.
1785 "; can't set Expiration\n"
1793 =item radius_password
1795 Returns a key/value pair containing the RADIUS attribute name and value
1800 sub radius_password {
1804 if ( $self->_password_encoding eq 'ldap' ) {
1805 $pw_attrib = 'Password-With-Header';
1806 } elsif ( $self->_password_encoding eq 'crypt' ) {
1807 $pw_attrib = 'Crypt-Password';
1808 } elsif ( $self->_password_encoding eq 'plain' ) {
1809 $pw_attrib = $radius_password;
1811 $pw_attrib = length($self->_password) <= 12
1816 ($pw_attrib, $self->_password);
1822 This method instructs the object to "snapshot" or freeze RADIUS check and
1823 reply attributes to the current values.
1827 #bah, my english is too broken this morning
1828 #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
1829 #the FS::cust_pkg's replace method to trigger the correct export updates when
1830 #package dates change)
1835 $self->{$_} = { $self->$_() }
1836 foreach qw( radius_reply radius_check );
1840 =item forget_snapshot
1842 This methos instructs the object to forget any previously snapshotted
1843 RADIUS check and reply attributes.
1847 sub forget_snapshot {
1851 foreach qw( radius_reply radius_check );
1855 =item domain [ END_TIMESTAMP [ START_TIMESTAMP ] ]
1857 Returns the domain associated with this account.
1859 END_TIMESTAMP and START_TIMESTAMP can optionally be passed when dealing with
1866 die "svc_acct.domsvc is null for svcnum ". $self->svcnum unless $self->domsvc;
1867 my $svc_domain = $self->svc_domain(@_)
1868 or die "no svc_domain.svcnum for svc_acct.domsvc ". $self->domsvc;
1869 $svc_domain->domain;
1874 Returns the FS::cust_svc record for this account (see L<FS::cust_svc>).
1878 #inherited from svc_Common
1880 =item email [ END_TIMESTAMP [ START_TIMESTAMP ] ]
1882 Returns an email address associated with the account.
1884 END_TIMESTAMP and START_TIMESTAMP can optionally be passed when dealing with
1891 $self->username. '@'. $self->domain(@_);
1897 Returns an array of FS::acct_snarf records associated with the account.
1901 # unused as originally intended, but now by Communigate Pro "RPOP"
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 last_login_text
2362 Returns text describing the time of last login.
2366 sub last_login_text {
2368 $self->last_login ? ctime($self->last_login) : 'unknown';
2371 =item psearch_cdrs OPTIONS
2373 Returns a paged search (L<FS::PagedSearch>) for Call Detail Records
2374 associated with this service. For svc_acct, "associated with" means that
2375 either the "src" or the "charged_party" field of the CDR matches the
2376 "username" field of the service.
2381 my($self, %options) = @_;
2386 my $did = dbh->quote($self->username);
2388 my $prefix = $options{'default_prefix'} || ''; #convergent.au '+61'
2389 my $prefixdid = dbh->quote($prefix . $self->username);
2391 my $for_update = $options{'for_update'} ? 'FOR UPDATE' : '';
2393 if ( $options{inbound} ) {
2394 # these will be selected under their DIDs
2395 push @where, "FALSE";
2399 if (!$options{'disable_charged_party'}) {
2401 "charged_party = $did",
2402 "charged_party = $prefixdid";
2404 if (!$options{'disable_src'}) {
2406 "src = $did AND charged_party IS NULL",
2407 "src = $prefixdid AND charged_party IS NULL";
2409 push @where, '(' . join(' OR ', @orwhere) . ')';
2411 # $options{'status'} = '' is meaningful; for the rest of them it's not
2412 if ( exists $options{'status'} ) {
2413 $hash{'freesidestatus'} = $options{'status'};
2415 if ( $options{'cdrtypenum'} ) {
2416 $hash{'cdrtypenum'} = $options{'cdrtypenum'};
2418 if ( $options{'calltypenum'} ) {
2419 $hash{'calltypenum'} = $options{'calltypenum'};
2421 if ( $options{'begin'} ) {
2422 push @where, 'startdate >= '. $options{'begin'};
2424 if ( $options{'end'} ) {
2425 push @where, 'startdate < '. $options{'end'};
2427 if ( $options{'nonzero'} ) {
2428 push @where, 'duration > 0';
2431 my $extra_sql = join(' AND ', @where);
2434 $extra_sql = " AND ".$extra_sql;
2436 $extra_sql = " WHERE ".$extra_sql;
2442 'hashref' => \%hash,
2443 'extra_sql' => $extra_sql,
2444 'order_by' => "ORDER BY startdate $for_update",
2448 =item get_cdrs (DEPRECATED)
2450 Like psearch_cdrs, but returns all the L<FS::cdr> objects at once, in a
2451 single list. Arguments are the same as for psearch_cdrs.
2457 my $psearch = $self->psearch_cdrs(@_);
2458 qsearch ( $psearch->{query} )
2461 # sub radius_groups has moved to svc_Radius_Mixin
2463 =item clone_suspended
2465 Constructor used by FS::part_export::_export_suspend fallback. Document
2470 sub clone_suspended {
2472 my %hash = $self->hash;
2473 $hash{_password} = join('',map($pw_set[ int(rand $#pw_set) ], (0..7) ) );
2474 new FS::svc_acct \%hash;
2477 =item clone_kludge_unsuspend
2479 Constructor used by FS::part_export::_export_unsuspend fallback. Document
2484 sub clone_kludge_unsuspend {
2486 my %hash = $self->hash;
2487 $hash{_password} = '';
2488 new FS::svc_acct \%hash;
2491 =item check_password
2493 Checks the supplied password against the (possibly encrypted) password in the
2494 database. Returns true for a successful authentication, false for no match.
2496 Currently supported encryptions are: classic DES crypt() and MD5
2500 sub check_password {
2501 my($self, $check_password) = @_;
2503 #remove old-style SUSPENDED kludge, they should be allowed to login to
2504 #self-service and pay up
2505 ( my $password = $self->_password ) =~ s/^\*SUSPENDED\* //;
2507 if ( $self->_password_encoding eq 'ldap' ) {
2509 $password =~ s/^{PLAIN}/{CLEARTEXT}/;
2510 my $auth = from_rfc2307 Authen::Passphrase $password;
2511 return $auth->match($check_password);
2513 } elsif ( $self->_password_encoding eq 'crypt' ) {
2515 my $auth = from_crypt Authen::Passphrase $self->_password;
2516 return $auth->match($check_password);
2518 } elsif ( $self->_password_encoding eq 'plain' ) {
2520 return $check_password eq $password;
2524 #XXX this could be replaced with Authen::Passphrase stuff
2526 if ( $password =~ /^(\*|!!?)$/ ) { #no self-service login
2528 } elsif ( length($password) < 13 ) { #plaintext
2529 $check_password eq $password;
2530 } elsif ( length($password) == 13 ) { #traditional DES crypt
2531 crypt($check_password, $password) eq $password;
2532 } elsif ( $password =~ /^\$1\$/ ) { #MD5 crypt
2533 unix_md5_crypt($check_password, $password) eq $password;
2534 } elsif ( $password =~ /^\$2a?\$/ ) { #Blowfish
2535 warn "Can't check password: Blowfish encryption not yet supported, ".
2536 "svcnum ". $self->svcnum. "\n";
2539 warn "Can't check password: Unrecognized encryption for svcnum ".
2540 $self->svcnum. "\n";
2548 =item crypt_password [ DEFAULT_ENCRYPTION_TYPE ]
2550 Returns an encrypted password, either by passing through an encrypted password
2551 in the database or by encrypting a plaintext password from the database.
2553 The optional DEFAULT_ENCRYPTION_TYPE parameter can be set to I<crypt> (classic
2554 UNIX DES crypt), I<md5> (md5 crypt supported by most modern Linux and BSD
2555 distrubtions), or (eventually) I<blowfish> (blowfish hashing supported by
2556 OpenBSD, SuSE, other Linux distibutions with pam_unix2, etc.). The default
2557 encryption type is only used if the password is not already encrypted in the
2562 sub crypt_password {
2565 if ( $self->_password_encoding eq 'ldap' ) {
2567 if ( $self->_password =~ /^\{(PLAIN|CLEARTEXT)\}(.+)$/ ) {
2570 #XXX this could be replaced with Authen::Passphrase stuff
2572 my $encryption = ( scalar(@_) && $_[0] ) ? shift : 'crypt';
2573 if ( $encryption eq 'crypt' ) {
2576 $saltset[int(rand(64))].$saltset[int(rand(64))]
2578 } elsif ( $encryption eq 'md5' ) {
2579 return unix_md5_crypt( $self->_password );
2580 } elsif ( $encryption eq 'blowfish' ) {
2581 croak "unknown encryption method $encryption";
2583 croak "unknown encryption method $encryption";
2586 } elsif ( $self->_password =~ /^\{CRYPT\}(.+)$/ ) {
2590 } elsif ( $self->_password_encoding eq 'crypt' ) {
2592 return $self->_password;
2594 } elsif ( $self->_password_encoding eq 'plain' ) {
2596 #XXX this could be replaced with Authen::Passphrase stuff
2598 my $encryption = ( scalar(@_) && $_[0] ) ? shift : 'crypt';
2599 if ( $encryption eq 'crypt' ) {
2602 $saltset[int(rand(64))].$saltset[int(rand(64))]
2604 } elsif ( $encryption eq 'md5' ) {
2605 return unix_md5_crypt( $self->_password );
2606 } elsif ( $encryption eq 'sha1_base64' ) { #for acct_sql
2607 my $pass = sha1_base64( $self->_password );
2608 $pass .= '=' x (4 - length($pass) % 4); #properly padded base64
2610 } elsif ( $encryption eq 'blowfish' ) {
2611 croak "unknown encryption method $encryption";
2613 croak "unknown encryption method $encryption";
2618 if ( length($self->_password) == 13
2619 || $self->_password =~ /^\$(1|2a?)\$/
2620 || $self->_password =~ /^(\*|NP|\*LK\*|!!?)$/
2626 #XXX this could be replaced with Authen::Passphrase stuff
2628 my $encryption = ( scalar(@_) && $_[0] ) ? shift : 'crypt';
2629 if ( $encryption eq 'crypt' ) {
2632 $saltset[int(rand(64))].$saltset[int(rand(64))]
2634 } elsif ( $encryption eq 'md5' ) {
2635 return unix_md5_crypt( $self->_password );
2636 } elsif ( $encryption eq 'blowfish' ) {
2637 croak "unknown encryption method $encryption";
2639 croak "unknown encryption method $encryption";
2648 =item ldap_password [ DEFAULT_ENCRYPTION_TYPE ]
2650 Returns an encrypted password in "LDAP" format, with a curly-bracked prefix
2651 describing the format, for example, "{PLAIN}himom", "{CRYPT}94pAVyK/4oIBk" or
2652 "{MD5}5426824942db4253f87a1009fd5d2d4".
2654 The optional DEFAULT_ENCRYPTION_TYPE is not yet used, but the idea is for it
2655 to work the same as the B</crypt_password> method.
2661 #eventually should check a "password-encoding" field
2663 if ( $self->_password_encoding eq 'ldap' ) {
2665 return $self->_password;
2667 } elsif ( $self->_password_encoding eq 'crypt' ) {
2669 if ( length($self->_password) == 13 ) { #crypt
2670 return '{CRYPT}'. $self->_password;
2671 } elsif ( $self->_password =~ /^\$1\$(.*)$/ && length($1) == 31 ) { #passwdMD5
2673 #} elsif ( $self->_password =~ /^\$2a?\$(.*)$/ ) { #Blowfish
2674 # die "Blowfish encryption not supported in this context, svcnum ".
2675 # $self->svcnum. "\n";
2677 warn "encryption method not (yet?) supported in LDAP context";
2678 return '{CRYPT}*'; #unsupported, should not auth
2681 } elsif ( $self->_password_encoding eq 'plain' ) {
2683 return '{PLAIN}'. $self->_password;
2685 #return '{CLEARTEXT}'. $self->_password; #?
2689 if ( length($self->_password) == 13 ) { #crypt
2690 return '{CRYPT}'. $self->_password;
2691 } elsif ( $self->_password =~ /^\$1\$(.*)$/ && length($1) == 31 ) { #passwdMD5
2693 } elsif ( $self->_password =~ /^\$2a?\$(.*)$/ ) { #Blowfish
2694 warn "Blowfish encryption not supported in this context, svcnum ".
2695 $self->svcnum. "\n";
2698 #are these two necessary anymore?
2699 } elsif ( $self->_password =~ /^(\w{48})$/ ) { #LDAP SSHA
2700 return '{SSHA}'. $1;
2701 } elsif ( $self->_password =~ /^(\w{64})$/ ) { #LDAP NS-MTA-MD5
2702 return '{NS-MTA-MD5}'. $1;
2705 return '{PLAIN}'. $self->_password;
2707 #return '{CLEARTEXT}'. $self->_password; #?
2709 #XXX this could be replaced with Authen::Passphrase stuff if it gets used
2710 #my $encryption = ( scalar(@_) && $_[0] ) ? shift : 'crypt';
2711 #if ( $encryption eq 'crypt' ) {
2712 # return '{CRYPT}'. crypt(
2714 # $saltset[int(rand(64))].$saltset[int(rand(64))]
2716 #} elsif ( $encryption eq 'md5' ) {
2717 # unix_md5_crypt( $self->_password );
2718 #} elsif ( $encryption eq 'blowfish' ) {
2719 # croak "unknown encryption method $encryption";
2721 # croak "unknown encryption method $encryption";
2729 =item domain_slash_username
2731 Returns $domain/$username/
2735 sub domain_slash_username {
2737 $self->domain. '/'. $self->username. '/';
2740 =item virtual_maildir
2742 Returns $domain/maildirs/$username/
2746 sub virtual_maildir {
2748 $self->domain. '/maildirs/'. $self->username. '/';
2753 =head1 CLASS METHODS
2757 =item search HASHREF
2759 Class method which returns a qsearch hash expression to search for parameters
2760 specified in HASHREF. Valid parameters are
2774 Arrayref of pkgparts
2780 Arrayref of additional WHERE clauses, will be ANDed together.
2791 my( $class, $params, $from, $where ) = @_;
2793 #these two should probably move to svc_Domain_Mixin ?
2796 if ( $params->{'domain'} ) {
2797 my $svc_domain = qsearchs('svc_domain', { 'domain'=>$params->{'domain'} } );
2798 #preserve previous behavior & bubble up an error if $svc_domain not found?
2799 push @$where, 'domsvc = '. $svc_domain->svcnum if $svc_domain;
2803 if ( $params->{'domsvc'} =~ /^(\d+)$/ ) {
2804 push @$where, "domsvc = $1";
2809 if ( $params->{'popnum'} =~ /^(\d+)$/ ) {
2810 push @$where, "popnum = $1";
2814 #and these in svc_Tower_Mixin, or maybe we never should have done svc_acct
2815 # towers (or, as mark thought, never should have done svc_broadband)
2818 my @where_sector = $class->tower_sector_sql($params);
2819 if ( @where_sector ) {
2820 push @$where, @where_sector;
2821 push @$from, ' LEFT JOIN tower_sector USING ( sectornum )';
2834 This is the FS::svc_acct job-queue-able version. It still uses
2835 FS::Misc::send_email under-the-hood.
2842 eval "use FS::Misc qw(send_email)";
2845 $opt{mimetype} ||= 'text/plain';
2846 $opt{mimetype} .= '; charset="iso-8859-1"' unless $opt{mimetype} =~ /charset/;
2848 my $error = send_email(
2849 'from' => $opt{from},
2851 'subject' => $opt{subject},
2852 'content-type' => $opt{mimetype},
2853 'body' => [ map "$_\n", split("\n", $opt{body}) ],
2855 die $error if $error;
2858 =item check_and_rebuild_fuzzyfiles
2862 sub check_and_rebuild_fuzzyfiles {
2863 my $dir = $FS::UID::conf_dir. "/cache.". $FS::UID::datasrc;
2864 -e "$dir/svc_acct.username"
2865 or &rebuild_fuzzyfiles;
2868 =item rebuild_fuzzyfiles
2872 sub rebuild_fuzzyfiles {
2874 use Fcntl qw(:flock);
2876 my $dir = $FS::UID::conf_dir. "/cache.". $FS::UID::datasrc;
2880 open(USERNAMELOCK,">>$dir/svc_acct.username")
2881 or die "can't open $dir/svc_acct.username: $!";
2882 flock(USERNAMELOCK,LOCK_EX)
2883 or die "can't lock $dir/svc_acct.username: $!";
2885 my @all_username = map $_->getfield('username'), qsearch('svc_acct', {});
2887 open (USERNAMECACHE,">$dir/svc_acct.username.tmp")
2888 or die "can't open $dir/svc_acct.username.tmp: $!";
2889 print USERNAMECACHE join("\n", @all_username), "\n";
2890 close USERNAMECACHE or die "can't close $dir/svc_acct.username.tmp: $!";
2892 rename "$dir/svc_acct.username.tmp", "$dir/svc_acct.username";
2902 my $dir = $FS::UID::conf_dir. "/cache.". $FS::UID::datasrc;
2903 open(USERNAMECACHE,"<$dir/svc_acct.username")
2904 or die "can't open $dir/svc_acct.username: $!";
2905 my @array = map { chomp; $_; } <USERNAMECACHE>;
2906 close USERNAMECACHE;
2910 =item append_fuzzyfiles USERNAME
2914 sub append_fuzzyfiles {
2915 my $username = shift;
2917 &check_and_rebuild_fuzzyfiles;
2919 use Fcntl qw(:flock);
2921 my $dir = $FS::UID::conf_dir. "/cache.". $FS::UID::datasrc;
2923 open(USERNAME,">>$dir/svc_acct.username")
2924 or die "can't open $dir/svc_acct.username: $!";
2925 flock(USERNAME,LOCK_EX)
2926 or die "can't lock $dir/svc_acct.username: $!";
2928 print USERNAME "$username\n";
2930 flock(USERNAME,LOCK_UN)
2931 or die "can't unlock $dir/svc_acct.username: $!";
2938 =item reached_threshold
2940 Performs some activities when svc_acct thresholds (such as number of seconds
2941 remaining) are reached.
2945 sub reached_threshold {
2948 my $svc_acct = qsearchs('svc_acct', { 'svcnum' => $opt{'svcnum'} } );
2949 die "Cannot find svc_acct with svcnum " . $opt{'svcnum'} unless $svc_acct;
2951 if ( $opt{'op'} eq '+' ){
2952 $svc_acct->setfield( $opt{'column'}.'_threshold',
2953 int($svc_acct->getfield($opt{'column'})
2954 * ( $conf->exists('svc_acct-usage_threshold')
2955 ? $conf->config('svc_acct-usage_threshold')/100
2960 my $error = $svc_acct->replace;
2961 die $error if $error;
2962 }elsif ( $opt{'op'} eq '-' ){
2964 my $threshold = $svc_acct->getfield( $opt{'column'}.'_threshold' );
2965 return '' if ($threshold eq '' );
2967 $svc_acct->setfield( $opt{'column'}.'_threshold', 0 );
2968 my $error = $svc_acct->replace;
2969 die $error if $error; # email next time, i guess
2971 if ( $warning_template ) {
2972 eval "use FS::Misc qw(send_email)";
2975 my $cust_pkg = $svc_acct->cust_svc->cust_pkg;
2976 my $cust_main = $cust_pkg->cust_main;
2978 my $to = join(', ', grep { $_ !~ /^(POST|FAX)$/ }
2979 $cust_main->invoicing_list,
2980 ($opt{'to'} ? $opt{'to'} : ())
2983 my $mimetype = $warning_mimetype;
2984 $mimetype .= '; charset="iso-8859-1"' unless $opt{mimetype} =~ /charset/;
2986 my $body = $warning_template->fill_in( HASH => {
2987 'custnum' => $cust_main->custnum,
2988 'username' => $svc_acct->username,
2989 'password' => $svc_acct->_password,
2990 'first' => $cust_main->first,
2991 'last' => $cust_main->getfield('last'),
2992 'pkg' => $cust_pkg->part_pkg->pkg,
2993 'column' => $opt{'column'},
2994 'amount' => $opt{'column'} =~/bytes/
2995 ? FS::UI::bytecount::display_bytecount($svc_acct->getfield($opt{'column'}))
2996 : $svc_acct->getfield($opt{'column'}),
2997 'threshold' => $opt{'column'} =~/bytes/
2998 ? FS::UI::bytecount::display_bytecount($threshold)
3003 my $error = send_email(
3004 'from' => $warning_from,
3006 'subject' => $warning_subject,
3007 'content-type' => $mimetype,
3008 'body' => [ map "$_\n", split("\n", $body) ],
3010 die $error if $error;
3013 die "unknown op: " . $opt{'op'};
3021 The $recref stuff in sub check should be cleaned up.
3023 The suspend, unsuspend and cancel methods update the database, but not the
3024 current object. This is probably a bug as it's unexpected and
3027 insertion of RADIUS group stuff in insert could be done with child_objects now
3028 (would probably clean up export of them too)
3030 _op_usage and set_usage bypass the history... maybe they shouldn't
3034 L<FS::svc_Common>, edit/part_svc.cgi from an installed web interface,
3035 export.html from the base documentation, L<FS::Record>, L<FS::Conf>,
3036 L<FS::cust_svc>, L<FS::part_svc>, L<FS::cust_pkg>, L<FS::queue>,
3037 L<freeside-queued>), L<FS::svc_acct_pop>,
3038 schema.html from the base documentation.